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

天涯浪子 提交于 2019-12-11 09:53:31

问题


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


回答1:


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




回答2:


If someone needs a AND and not 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;   
                }   


来源:https://stackoverflow.com/questions/30085872/how-to-filter-with-multiple-values-in-ui-grid-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!