从共享点列表返回列表项时如何按字符串过滤?

时间:2021-01-15 14:33:50

标签: javascript filter sharepoint-online spfx

我创建了一个过滤器文本字段,它成功地过滤了 webpart 上的列表。它目前只接受一个数字,我希望它能够按文本过滤。 SharePoint 中的列称为 PPName。 我了解如何实现 JavaScript 进行过滤,但在这种情况下,它需要应用于 sp.web.lists.getByTitle().items.filter().get() 而我无法弄清楚。

这是文本字段上的过滤器功能:

如您所见,我已使用 let _item2 过滤并返回结果,这实际上对我在字段中输入的内容进行了正确过滤,但它不会按字母过滤,仅按精确过滤匹配。我想不出如何在它下面的 _item2 中使用这个过滤后的 sp.web.lists...

private _filter = (ev: React.FormEvent<HTMLInputElement>, newValue?: string) => {
 
        this.setState({
          filterValue: newValue,
       
        }, () => {
          const sid = this.state.filterValue;
          let _item = this.state.Items.filter((item) => {return item.Id == sid; });
          let _item2 = this.state.Items.filter((item) => {return item.PPName == sid; });
          
           if (_item && _item.length > 0) {
             sp.web.lists.getByTitle("MyList").items.filter("Id eq '" + sid + "'").get()
             .then((items: any[]) => {
               let returnedItems: IEIAItem[] = items.map((item) => { return new EIAItem(item); });
               console.log(returnedItems, 'returnedItems, for ID');
               this.setState({
                 ItemsForPages: returnedItems
        
               });
             });
            
           } else if(_item && _item.length === 0){
             this._getListItems();
             var AllItems = this.state.Items;
             this.setState({
               ItemsForPages: AllItems,
               isFiltered: 'false'
              
             });
            
          }
          if (_item2 && _item2.length > 0) {
    
         sp.web.lists.getByTitle("MyList").items.filter("PPName eq '" + sid + "'").get()
            .then((items: any[]) => {
           
              let returnedItems: IEIAItem[] = items.map((item) => { return new EIAItem(item); });
           
              this.setState({
                ItemsForPages: returnedItems
        
              });
            });
            
          } else if(_item && _item.length === 0){
            this._getListItems();
            var AllItems2 = this.state.Items;
            this.setState({
              ItemsForPages: AllItems2,
              isFiltered: 'false'
              
            });
            
          }
        });
         
    }   `


自然感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

试试这个 .filter(`substringof('SUBSTRING',NAMEOFTHEFIELD)`).

在 .filter('HERE') 中,您可以放置​​此处描述的任何表达式:https://docs.microsoft.com/en-us/previous-versions/dynamicsnav-2016/hh169248(v=nav.90)

如果您想使用多个过滤器(此示例使用 OR): .filter("substringof('" + searchString + "',Title) 或 substringof('" + searchString + "',ColumnName)")

注意过滤器中的 OR - 对于大列表,您可能会超过阈值限制 (https://github.com/pnp/pnpjs/issues/677)。

答案 1 :(得分:0)

我找到了一个更简单更好的解决方案:

 var textToSearch = sid;
          let _item3 = this.state.Items.filter((item) => {return item.PPName.toLowerCase().indexOf(textToSearch) >= 0;});
           console.log(_item3, 'item3');
           
          if (_item3 && _item3.length > 0) {
            
            this.setState({
              ItemsForPages: _item3
      
            });