筛选器详细信息列表

时间:2018-08-15 22:00:40

标签: office-ui-fabric

有人可以让我知道如何过滤DetailsList吗?例如,假设我有以下列表:

list

如何过滤列表,以便在键入“拒绝”时仅显示状态为“拒绝”的项目

这是我尝试过的代码(来自文档https://developer.microsoft.com/en-us/fabric#/components/detailslist):

private _onChangeText = (text: any) => {
    this.setState({ items: text ? this.state.items.filter(i => 
    i.Status.indexOf(text) > -1) : this.state.items });
}

<TextField
  label="Filter by name:"
  onChanged={this._onChangeText}
/>

谢谢!

1 个答案:

答案 0 :(得分:1)

这里是Codepen,在这里我要过滤一组项目以查看文本是否出现在任何项目的值中(不区分大小写)。它类似于您在原始问题中链接的文档示例。希望对您有所帮助!

let COLUMNS = [
  { 
    key: "name", 
    name: "Name", 
    fieldName: "Name", 
    minWidth: 20, 
    maxWidth: 300,
  },
  { 
    key: "status", 
    name: "Status",
    fieldName: 'Status',
    minWidth: 20, 
    maxWidth: 300
  }
];


const ITEMS = [
  {
    Name: 'xyz',
    Status: 'Approve'
  },
  {
    Name: 'abc',
    Status: 'Approve'
  },
  {
    Name: 'mno',
    Status: 'Reject'
  },
  {
    Name: 'pqr',
    Status: 'Reject'
  }
]

const includesText = (i, text): boolean => {
  return Object.values(i).some((txt) => txt.toLowerCase().indexOf(text.toLowerCase()) > -1);
}

const filter = (text: string): any[] => {
  return ITEMS.filter(i => includesText(i, text)) || ITEMS;
}

class Content extends React.Component {

  constructor(props: any) {
    super(props);

    this.state = {
      items: ITEMS
    }
  }

  private _onChange(ev: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, text: string) {
    let items = filter(text);
    this.setState({ items: items });
  }

  public render() {
    const { items } = this.state;
    return (
      <Fabric.Fabric>
          <Fabric.TextField label="Filter" onChange={this._onChange.bind(this)} />
          <Fabric.DetailsList 
            items={ items } 
            columns={ COLUMNS }              
          />        
      </Fabric.Fabric>
    );
  }

}

ReactDOM.render( 
  <Content />,
  document.getElementById('content')
);