【问题标题】:How would I filter by string when returning list items from sharepoint list?从共享点列表返回列表项时,如何按字符串过滤?
【发布时间】:2021-01-29 20:12:24
【问题描述】:

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

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

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

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'
              
            });
            
          }
        });
         
    }   `


任何帮助自然不胜感激。

【问题讨论】:

    标签: javascript filter sharepoint-online spfx


    【解决方案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)。

    【讨论】:

    • 您好,我刚刚在我的环境中进行了测试,并对原始答案进行了更改。要清楚(对不起,第一次回答不清楚)。我的整个通话看起来像这样:await sp.web.lists.getById(this.props.ListId).items.filter(substringof('Heads',Title)).get();
    • 能否请您粘贴您的呼叫代码(或使用 codepen)? .filter 中的所有内容都必须是字符串 .filter(`string expression`)
    • 谢谢,问题是您在 substringof 表达式中缺少列名 -> .filter(substringof('sid)) => .filter(`substringof('sid',NAMEOFTHECOLUMN)` )
    • ` 反引号只是为了更轻松地构建字符串 :-) 你可以使用任何东西 ", ', 来构建该表达式,但它必须以过滤器的有效字符串表达式结尾。在那次调用中 .filter('string ') 它被翻译成 api 调用,如 filter= ....
    • 是的,搜索子串应该用'或"包围,搜索列的名称没有
    【解决方案2】:

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

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

    【讨论】:

    • 同意,这是一个更简单的解决方案,但它需要从列表中加载每个项目,然后您可以对其进行过滤。解决方案取决于用例。您可以过滤“在客户端”或“在服务器上”。两种解决方案都各有利弊。
    猜你喜欢
    • 2011-07-19
    • 2021-06-24
    • 2010-11-26
    • 2018-12-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    相关资源
    最近更新 更多