如何在IBM Carbon(Angular)中对表格应用搜索过滤器?

huangapple go评论89阅读模式
英文:

How to apply search filter on table in IBM Carbon (Angular)?

问题

我是新手使用IBM碳设计系统。以下是来自IBM Carbon Angular文档的表格演示,表格组件的HTML代码如下:

<ibm-table-container>
    <ibm-table-header>
        <h4 ibmTableHeaderTitle>DASHBOARD</h4>
    </ibm-table-header>

    <ibm-table-toolbar [model]="model">
        <ibm-table-toolbar-content>
            <ibm-table-toolbar-search [expandable]="true" placeholder="在仪表板中搜索"></ibm-table-toolbar-search>
        </ibm-table-toolbar-content>
    </ibm-table-toolbar>

    <ibm-table [model]="model"></ibm-table>
    <ibm-pagination [model]="model" (selectPage)="selectPage($event)"></ibm-pagination>
</ibm-table-container>

表格组件的TS文件如下:

this.model.header = [
    new TableHeaderItem({data: "Name"}), new TableHeaderItem({data: "Address"})
];

this.model.data = [
    [new TableItem({data: "Data1"}), new TableItem({data: "India"})],
    [new TableItem({data: "Data2"}), new TableItem({data: "USA"})],
    [new TableItem({data: "Data3"}), new TableItem({data: "Canada"})],
    [new TableItem({data: "Data5"}), new TableItem({data: "France"})],
];

我参考的文档链接如下:
Storybook IBM Carbon tableAngular Carbon docsgithub angular carbon design

这是表格UI的截图

请问是否有人可以为我展示一个表格搜索过滤器的实时示例,可能在stackblitz上?

Stackblitz编辑器链接

英文:

I am new to IBM carbon design system
Below is table demo from IBM Carbon Angular documentation,
The table.component.html

   &lt;ibm-table-container&gt;

&lt;ibm-table-header&gt;
	&lt;h4 ibmTableHeaderTitle&gt;DASHBOARD&lt;/h4&gt;
&lt;/ibm-table-header&gt;

&lt;ibm-table-toolbar [model]=&quot;model&quot; &gt;
 &lt;ibm-table-toolbar-content&gt;
		&lt;ibm-table-toolbar-search [expandable]=&quot;true&quot; 
		placeholder=&quot; Search in Dashboard &quot;&gt;
	    &lt;/ibm-table-toolbar-search&gt;
 &lt;/ibm-table-toolbar-content&gt;
&lt;/ibm-table-toolbar&gt;

&lt;ibm-table [model]=&quot;model&quot; &gt; &lt;/ibm-table&gt;
&lt;ibm-pagination [model]=&quot;model&quot; (selectPage)=&quot;selectPage($event)&quot;&gt;&lt;/ibm-pagination&gt;

</ibm-table-container>

The table.component.ts file

    this.model.header = [
  new TableHeaderItem({data: &quot;Name&quot;}), new TableHeaderItem({data: &quot;Address&quot; })
];

this.model.data = [
       [new TableItem({data: &quot;Data1&quot;}), new TableItem({data: &quot;India&quot;})],
       [new TableItem({data: &quot;Data2&quot;}), new TableItem({data: &quot;USA&quot;})],
       [new TableItem({data: &quot;Data3&quot;}), new TableItem({data: &quot;Canada&quot;})],
       [new TableItem({data: &quot;Data5&quot;}), new TableItem({data: &quot;France&quot;})],
         
];

The documentation links that I am referring to:
Storybook IBM Carbon table , Angular Carbon docs , github angular carbon design

This is the screenshot of the table UI

can someone please show me a live example for search filter on table possibly on stackblitz ?

Stackblitz editor link

答案1

得分: 0

首先获取搜索值的更改:

<ibm-table-toolbar-search [expandable]="true" placeholder="在仪表板中搜索" (change)="searchValueChange($event.value)"></ibm-table-toolbar-search>

然后使用该值筛选数据:

searchValueChange(value: string) {
  this.model.data = this.model.data.filter(
    (item) => item[0].data.includes(value) || item[1].data.includes(value)
  );
}

现在的问题是,当您更改搜索栏的值时,它会尝试从先前筛选的值中进行筛选。为了解决这个问题,您可以将初始数据存储在一个名为 initialData 的属性中,然后筛选 initialData 而不是 filteredData。如果您需要帮助,请在评论中告诉我。

英文:

Firstly get search value changes:

&lt;ibm-table-toolbar-search [expandable]=&quot;true&quot; placeholder=&quot;Search in Dashboard&quot; (change)=&quot;searchValueChange($event.value)&quot;&gt;&lt;/ibm-table-toolbar-search&gt;

Then filter data with the value:

searchValueChange(value: string) {
  this.model.data = this.model.data.filter(
    (item) =&gt; item[0].data.includes(e.value) || item[1].data.includes(e.value)
  )
}

Problem now is when you'll change the value of the searchbar it will try to filter from the previous filtered values. For fixing this you may store initial data into a initialData property and filtering initialData instead filteredData. (If you need help for that tell me in comments).

huangapple
  • 本文由 发表于 2020年1月3日 15:27:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定