简体   繁体   English

如何在ui-grid中使用多个值进行过滤? (angularjs)

[英]How to filter with multiple values in ui-grid? (angularjs)

I'm trying to make a filter with passing multiple values for the filter but have only a single return. 我正在尝试通过传递多个值来创建一个过滤器,但只有一个返回值。

field: 'name',
    filter: {
       condition: function(searchTerm, cellValue) {
        var strippedValue = (searchTerm).split(';');
        //console.log(strippedValue);
        for (i = 0; i < strippedValue.length; i++){
          if (cellValue.indexOf(strippedValue[i]) == -1)
            return false;    
          return true;
        }
        //return cellValue.indexOf(strippedValue) >= 0;
      }
    }, headerCellClass: $scope.highlightFilteredHeader

find the test code here http://plnkr.co/edit/UVDWfucjclXl4Ij9Ylm7?p=preview 在此处找到测试代码http://plnkr.co/edit/UVDWfucjclXl4Ij9Ylm7?p=preview

Resolved, I made some adjustments to improve the code as well. 解决后,我也进行了一些调整以改进代码。

 condition: function(searchTerm, cellValue) {
    var separators = ['-', '/', ':', ';', ','];
    var strippedValue = searchTerm.split(new RegExp(separators.join('|'), 'g'));
    var bReturnValue = false;
    //console.log(strippedValue, "teste");
    for(iIndex in strippedValue){
        var sValueToTest = strippedValue[iIndex];
        sValueToTest = sValueToTest.replace(" ", "");
        if (cellValue.toLowerCase().indexOf(sValueToTest.toLowerCase()) >= 0)
            bReturnValue = true;
    }
    return bReturnValue;
}

Look code in link: http://plnkr.co/edit/UVDWfucjclXl4Ij9Ylm7?p=preview 在链接中查看代码: http : //plnkr.co/edit/UVDWfucjclXl4Ij9Ylm7?p=preview

If someone needs a AND and not OR 如果有人需要AND而不是OR

filter: {
         condition:function(searchTerm, cellValue){          
                    if(cellValue===null){
                        return false;
                    }
                    let separators = [' ', '/', ':', ';', ','];
                    let strippedValue = searchTerm.split(new RegExp(separators.join('|'), 'g'));
                    let bReturnValue = false; 
                    let intNumFound = 0;
                    for(let iIndex in strippedValue){
                            let sValueToTest = strippedValue[iIndex];
                            sValueToTest = sValueToTest.replace(" ", "");
                            if (cellValue.toLowerCase().indexOf(sValueToTest.toLowerCase()) >= 0){
                            intNumFound++;
                            }
                            //bReturnValue = true;
                    } 
                    if(intNumFound>=strippedValue.length){
                        bReturnValue = true;
                    }
                    return bReturnValue;   
                }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM