在我的 mat-table 中有 6 列,当任何一列没有更多单词时,它看起来像 Image-1,但是当任何列有更多单词时,UI 看起来像 Image-2,所以如何设置 UI 像 Image- 1 当任何列在angular 6 中有更多单词时?
图像-1

图像-2

user.component.html
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <ng-container matColumnDef="userimage"> <th mat-header-cell *matHeaderCellDef> # </th> <td mat-cell *matCellDef="let element"> <img src="{{commonUrlObj.commonUrl}}/images/{{element.userimage}}" style="height: 40px;width: 40px;"/> </td> </ng-container> <ng-container matColumnDef="username"> <th mat-header-cell *matHeaderCellDef> Full Name </th> <td mat-cell *matCellDef="let element"> {{element.username}} ( {{element.usertype}} )</td> </ng-container> <ng-container matColumnDef="emailid"> <th mat-header-cell *matHeaderCellDef> EmailId </th> <td mat-cell *matCellDef="let element"> {{element.emailid}} </td> </ng-container> <ng-container matColumnDef="contactno"> <th mat-header-cell *matHeaderCellDef> Contact No. </th> <td mat-cell *matCellDef="let element"> {{element.contactno}} </td> </ng-container> <ng-container matColumnDef="enabled"> <th mat-header-cell *matHeaderCellDef> Enabled </th> <td mat-cell *matCellDef="let element" style="color: blue"> <ng-container *ngIf="element.enabled == 'true'; else otherss">Enabled</ng-container> <ng-template #otherss>Disabled</ng-template> </td> </ng-container> <ng-container matColumnDef="action"> <th mat-header-cell *matHeaderCellDef> Action </th> <td mat-cell *matCellDef="let element" fxLayoutGap="5px"> <button mat-mini-fab color="primary" routerLink="/base/editUserDetails/{{element.userid}}"><mat-icon>edit</mat-icon></button> <button mat-mini-fab color="primary" routerLink="/base/viewUserDetails/{{element.userid}}"><mat-icon>pageview</mat-icon></button> </td> </ng-container> <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr> <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr> </table> <mat-paginator [pageSizeOptions]="[5, 10, 20, 50 ,100]" showFirstLastButtons></mat-paginator> |
使用 css 我们可以调整我在下面的代码中输入的特定列宽。
user.component.css
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | width: 100%; } .mat-column-username { word-wrap: break-word !important; white-space: unset !important; flex: 0 0 28% !important; width: 28% !important; overflow-wrap: break-word; word-wrap: break-word; word-break: break-word; -ms-hyphens: auto; -moz-hyphens: auto; -webkit-hyphens: auto; hyphens: auto; } .mat-column-emailid { word-wrap: break-word !important; white-space: unset !important; flex: 0 0 25% !important; width: 25% !important; overflow-wrap: break-word; word-wrap: break-word; word-break: break-word; -ms-hyphens: auto; -moz-hyphens: auto; -webkit-hyphens: auto; hyphens: auto; } .mat-column-contactno { word-wrap: break-word !important; white-space: unset !important; flex: 0 0 17% !important; width: 17% !important; overflow-wrap: break-word; word-wrap: break-word; word-break: break-word; -ms-hyphens: auto; -moz-hyphens: auto; -webkit-hyphens: auto; hyphens: auto; } .mat-column-userimage { word-wrap: break-word !important; white-space: unset !important; flex: 0 0 8% !important; width: 8% !important; overflow-wrap: break-word; word-wrap: break-word; word-break: break-word; -ms-hyphens: auto; -moz-hyphens: auto; -webkit-hyphens: auto; hyphens: auto; } .mat-column-userActivity { word-wrap: break-word !important; white-space: unset !important; flex: 0 0 10% !important; width: 10% !important; overflow-wrap: break-word; word-wrap: break-word; word-break: break-word; -ms-hyphens: auto; -moz-hyphens: auto; -webkit-hyphens: auto; hyphens: auto; } |
如果您在样式中使用 scss,则可以使用 mixin 来帮助生成代码。如果你每次都把所有的属性都放好,你的风格很快就会失控。
这是一个非常简单的示例——实际上只是一个概念证明,您可以根据需要使用多个属性和规则对其进行扩展。
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | { .mat-column- { @each $colName, $props in $columns { $width: map-get($props, 'width'); &#{$colName} { flex: $width; min-width: $width; @if map-has-key($props, 'color') { color: map-get($props, 'color'); } } } } } |
然后在定义表格的组件中执行以下操作:
2 3 4 5 6 7 | orderid: (width: 6rem, color: gray), date: (width: 9rem), items: (width: 20rem) )); |
这会生成如下内容:
2 3 4 5 6 7 8 | flex: 6rem; min-width: 6rem; color: gray; } .mat-column-date[_ngcontent-c15] { flex: 9rem; min-width: 9rem; } |
在此版本中,
对于您的具体示例,您可以添加
正如我已经实施的那样,它运行良好。您只需要使用
添加列宽
例如:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <ng-container matColumnDef="productId"> <mat-header-cell *matHeaderCellDef>product ID</mat-header-cell> <mat-cell *matCellDef="let product">{{product.id}}</mat-cell> </ng-container> <ng-container matColumnDef="productName"> <mat-header-cell *matHeaderCellDef>Name</mat-header-cell> <mat-cell *matCellDef="let product">{{product.name}}</mat-cell> </ng-container> <ng-container matColumnDef="actions"> <mat-header-cell *matHeaderCellDef>Actions</mat-header-cell> <mat-cell *matCellDef="let product"> <button (click)="view(product)"> <mat-icon>visibility</mat-icon> </button> </mat-cell> </ng-container> <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row> <mat-row *matRowDef="let row; columns: displayedColumns"></mat-row> </mat-table> |
这里
现在我们通过
应用宽度
造型
2 3 4 5 6 | flex: 0 0 10%; } .mat-column-productName { flex: 0 0 50%; } |
剩余的宽度平均分配给其他列
你可以很容易地做到这一点。在每一列中,您将获得一个字段名称以 mat-column 为前缀的类,因此该类将类似于 mat-column-yourFieldName。因此,您可以设置如下样式
2 3 4 | flex: none; width: 100px; } |
所以我们可以根据我们的要求给列固定宽度。
希望这对某人有所帮助。
你可以使用下面的 CSS 来做到这一点:
2 3 4 5 6 7 8 9 10 11 | width: 100%; table-layout: fixed; } th, td { overflow: hidden; width: 200px; text-overflow: ellipsis; white-space: nowrap; } |
这是一个带有示例数据
的
这是解决问题的另一种方法:
与其尝试"在帖子中修复它",为什么不在表格需要尝试将其放入其列之前截断描述?我是这样做的:
2 3 4 5 6 | <th mat-header-cell *matHeaderCellDef> {{ 'Parts.description' | translate }} </th> <td mat-cell *matCellDef="let element"> {{(element.description.length > 80) ? ((element.description).slice(0, 80) + '...') : element.description}} </td> </ng-container> |
所以我首先检查数组是否大于某个长度,如果是,则截断并添加 \\'...\\' 否则按原样传递值。这使我们仍然可以从表格的自动间距中受益 :)

我们可以直接将属性宽度添加到th
例如:
2 3 4 | <th mat-header-cell *matHeaderCellDef width ="20%"> No. </th> <td mat-cell *matCellDef="let element"> {{element.position}} </td> </ng-container> |
在此处输入图像描述我有一个有四列的表格。请看截图。在 scss 文件中,我包含以下代码:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 | width: 100%; } .mat-column-id { width: 15% !important; } .mat-column-question { width: 55% !important; } .mat-column-view { width: 15% !important; } .mat-column-reply { width: 15% !important; } |
只需要更新
2 3 | width: 100px; } |
如果您想为特定列提供固定宽度,您可以将
您可以使用
fixedLayout : 是否使用固定的表格布局。启用此选项将强制使用一致的列宽并优化本机表的呈现粘性样式。弹性表的无操作。
只需将
2 3 4 | <th style="width:5% !important;" mat-header-cell *matHeaderCellDef> Full Name </th> <td style="width:5% !important;" mat-cell *matCellDef="let element"> {{element.username}} ( {{element.usertype}} )</td> </ng-container> |
只需在标签表中添加类
例如
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我需要一个表,其中行实际上是2行表,一个嵌套表是..我怎样才能在Prawn中做到这一点?也许我需要延期..但哪一个? 最佳答案 现在支持子表:Prawn::Document.generate("subtable.pdf")do|pdf|subtable=pdf.make_table([["sub"],["table"]])pdf.table([[subtable,"original"]])end 关于ruby-on-rails-PrawnPDF:Ineedtogeneratenested