Matable的MatcolumnDef和Matcelldef如何工作?

发布于 2025-02-09 09:47:20 字数 136 浏览 2 评论 0原文

我希望从现有的页面中构建可重复使用的表组件。 我很天真地期望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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野侃 2025-02-16 09:47:20

我找到了解决方案。我是Angular的新手,我不愿使用“复杂”内置组件。

原来,您可以使用Vuejs的示波器(例如功能)来创建组件。

  1. 在表HTML页面中,您定义:

     < table>
      < tr>
        < th *ngfor =“让列的属性| keyValue”>
          < ng-container
             [ngtemplateoutlet] =“ columnheader”
             [ngtemplateoutcontext] =“ {field:property.key,text:property.value}”
          ></ng-container>
        </th>
      </tr>
      < tr *ngfor =“ let data of datas> gt;
        < td *ngfor =“让列的属性| keyValue”>
          < ng-container
             [ngtemplateoutlet] =“ columnbody”
             [ngtemplateOutletContext] =“ {$ ntimit:property.key}”
          ></ng-container>
        </td>
      </tr>
    </table>
     

    在这里,ng-container传递传递templateref,然后传递到该templateref通过ngtemplate outletletletcontext((与$隐式键一样,键值对或单个值。

  2. 关联的打字稿文件是:

     导出类TableComponent oninit {
      @input()列!:{[键:字符串]:string};
    
      @contentChild()columnheader!:templateref< any gt;;;
      @contentChild()columnbody!:templateref< any gt;;;
    }
     
  3. ,父组件可以按以下方式调用并传递数据:

     < app-table
      url =“/api/translation/v1/jsons”
      [列] =“ {
        Col1:“第一栏”,
        COL2:“第二列”
      }”
    >
      < ng-template #columnheader let-field =“ field” let-text =“ text”>
        {{field}} {{text}}}
      </ng-template>
    
      < ng-template #columnbody let-field>
        {{场地}}
      </ng-template>
    </app-table>
     

这样,我可以创建一个可重复使用的表组件而无需使用库

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.

  1. In the table HTML page, you define:

    <table>
      <tr>
        <th *ngFor="let property of columns | keyvalue">
          <ng-container
             [ngTemplateOutlet]="columnHeader"
             [ngTemplateOutletContext]="{field: property.key, text: property.value}"
          ></ng-container>
        </th>
      </tr>
      <tr *ngFor="let data of datas">
        <td *ngFor="let property of columns | keyvalue">
          <ng-container
             [ngTemplateOutlet]="columnBody"
             [ngTemplateOutletContext]="{$implicit: property.key}"
          ></ng-container>
        </td>
      </tr>
    </table>
    

    Here, ng-container outputs passed TemplateRef, and pass to that templateRef the data through ngTemplateOutletContext (either key-value pairs or single value as with $implicit key).

  2. The associated typescript file is:

    export class TableComponent implements OnInit {
      @Input() columns!: {[key: string]: string};
    
      @ContentChild() columnHeader!: TemplateRef<any>;
      @ContentChild() columnBody!: TemplateRef<any>;
    }
    
  3. And the parent component can call and pass data as follows:

    <app-table
      url="/api/translation/v1/jsons"
      [columns]="{
        col1: 'Column One',
        col2: 'Column Two'
      }"
    >
      <ng-template #columnHeader  let-field="field" let-text="text">
        {{field}} {{text}}
      </ng-template>
    
      <ng-template #columnBody let-field>
        {{field}}
      </ng-template>
    </app-table>
    

This way, I could create a reusable table component without using libraries

裸钻 2025-02-16 09:47:20

确切的指令是*MatheaderCellDef*MatcellDefmatcolumnDef

最后一个是将列与显示的列匹配。没什么疯狂的。

让我们关注前两个:注意每个之前的*

这是创建所谓的结构指令的快捷方式。

在Angular中,您有两种类型的指令:属性和结构。

属性指令是简单指令,称为标签属性,并修改给定元素的行为。

另一端的结构指令是旨在操纵DOM树的指令。

它们在这里用来将DOM元素注入表中,以代替其声明的单元格。

这是相当复杂的,老实说,您可以找到更简单的方法,但是如果您希望继续沿着这条路,这是文档

The exact directives are *matHeaderCellDef, *matCellDef and matColumnDef.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文