Matable的MatcolumnDef和Matcelldef如何工作?
我希望从现有的页面中构建可重复使用的表组件。 我很天真地期望Angular具有像示例插槽,插槽道具一样的Vuejs。但是我找不到其他解决方案。
然后我遇到了Mat-table的例子。
我想知道我可以构建如此灵活的组件来构建桌子吗?
I was hoping to build reusable table component from my existing pages with tables.
I was naively expecting Angular to have Vuejs like features as scoped slots, slot props. However I could not find any other solution.
Then I came across with the example of mat-table.
I ma wondering could I build such a flexible component to build tables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了解决方案。我是Angular的新手,我不愿使用“复杂”内置组件。
原来,您可以使用Vuejs的示波器(例如功能)来创建组件。
在表HTML页面中,您定义:
在这里,
ng-container
传递传递templateref
,然后传递到该templateref
通过ngtemplate outletletletcontext
((与$隐式
键一样,键值对或单个值。关联的打字稿文件是:
,父组件可以按以下方式调用并传递数据:
这样,我可以创建一个可重复使用的表组件而无需使用库
I found solution to it. I am new to Angular, and I was reluctant/afraid to use 'complex' built-in components.
Turns out you can use Vuejs' scoped slots like feature to create your components.
In the table HTML page, you define:
Here,
ng-container
outputs passedTemplateRef
, and pass to thattemplateRef
the data throughngTemplateOutletContext
(either key-value pairs or single value as with$implicit
key).The associated typescript file is:
And the parent component can call and pass data as follows:
This way, I could create a reusable table component without using libraries
确切的指令是
*MatheaderCellDef
,*MatcellDef
和matcolumnDef
。最后一个是将列与显示的列匹配。没什么疯狂的。
让我们关注前两个:注意每个之前的
*
?这是创建所谓的结构指令的快捷方式。
在Angular中,您有两种类型的指令:属性和结构。
属性指令是简单指令,称为标签属性,并修改给定元素的行为。
另一端的结构指令是旨在操纵DOM树的指令。
它们在这里用来将DOM元素注入表中,以代替其声明的单元格。
这是相当复杂的,老实说,您可以找到更简单的方法,但是如果您希望继续沿着这条路,这是文档
The exact directives are
*matHeaderCellDef
,*matCellDef
andmatColumnDef
.The last one is only to match the column with the displayed columns. Nothing crazy.
Let's focus on the first two : notice the
*
before each ?This is a shortcut to create what is called structural directives.
In angular, you have two type of directives : attributes and structural.
Attributes directives are simple directives that are declared as tag attributes, and modify the behavior of a given element.
Structural directives on the other end, are directives meant to manipulate the DOM tree.
They're used here to inject (or remove) DOM elements into the table, in place of the cells they're declared on.
This is fairly complex and you honestly can find simpler ways of doing so, but if you wish to continue down this path, here is the documentation