如何在IBM Carbon(Angular)上的表格中应用filterdata后获取初始表格数据?

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

How to get initial table data after filterdata is applied on table in IBM-carbon (Angular)?

问题

搜索栏:

<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)
  )
}

在应用搜索过滤器后,清除搜索栏上的内容后,无法获取初始数据。

英文:

The search bar:

&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;

The search value change function:

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

After applying the search filter I am unable to get intial data on clearing the search bar.

答案1

得分: 0

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

}

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

答案2

得分: 0

尝试这个:

import { Component, Input, OnInit } from "@angular/core";
import {
  TableModel,
  TableItem,
  TableHeaderItem
} from "carbon-components-angular";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  name = "Angular";
  copyData = [];
  @Input() model = new TableModel();

  ngOnInit() {
    this.model.header = [
      new TableHeaderItem({ data: "姓名" }),
      new TableHeaderItem({ data: "地址" })
    ];

    this.model.data = [
      [new TableItem({ data: "数据1" }), new TableItem({ data: "印度" })],
      [new TableItem({ data: "数据2" }), new TableItem({ data: "美国" })],
      [new TableItem({ data: "数据3" }), new TableItem({ data: "加拿大" })],
      [new TableItem({ data: "数据4" }), new TableItem({ data: "中国" })],
      [new TableItem({ data: "数据5" }), new TableItem({ data: "意大利" })]
    ];

    this.copyData = [...this.model.data];
  }

  searchValueChange(value: string) {
    if (value) {
      this.model.data = this.model.data.filter(
        item =>
          item[0].data.toLowerCase().includes(value.toLowerCase()) ||
          item[1].data.toLowerCase().includes(value.toLowerCase())
      );
    } else {
      this.model.data = this.copyData;
    }
  }
}
英文:

Try this:

import { Component, Input, OnInit } from &quot;@angular/core&quot;;
import {
TableModel,
TableItem,
TableHeaderItem
} from &quot;carbon-components-angular&quot;;
@Component({
selector: &quot;my-app&quot;,
templateUrl: &quot;./app.component.html&quot;,
styleUrls: [&quot;./app.component.css&quot;]
})
export class AppComponent {
name = &quot;Angular&quot;;
copyData = [];
@Input() model = new TableModel();
ngOnInit() {
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;Data4&quot; }), new TableItem({ data: &quot;China&quot; })],
[new TableItem({ data: &quot;Data5&quot; }), new TableItem({ data: &quot;Italy&quot; })]
];
this.copyData = [...this.model.data];
}
searchValueChange(value: string) {
if (value) {
this.model.data = this.model.data.filter(
item =&gt;
item[0].data.toLowerCase().includes(value.toLowerCase()) ||
item[1].data.toLowerCase().includes(value.toLowerCase())
);
} else {
this.model.data = this.copyData;
}
}
}

huangapple
  • 本文由 发表于 2020年1月6日 17:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609649.html
匿名

发表评论

匿名网友

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

确定