简体   繁体   English

如何在带有NULL值的angularjs中实现Excel Like Filter?

[英]How to Implement Excel Like Filter in angularjs with NULL values?

I am new to AngualarJS and I Need to Implement Excel Like Filter for table using angulajs(v.1) . 我是AngualarJS的新手,我需要使用angulajs(v.1)为表实现Excel Like Filter。 I searched this site and found out a solution here: 我搜索了此站点,并在此处找到了解决方案:
https://stackoverflow.com/a/49655718/9967249 https://stackoverflow.com/a/49655718/9967249

The above solution works fine, But if I have a null or empty value in my list then it doesn't work. 上面的解决方案工作正常,但是如果我的列表中有一个null或空值,则它不起作用。

这是我项目中的示例表。 In this case the Search Text Functionality works for the column Status as it has all values populated but since all the other Columns are having null values, the Search Box functionality doesn't work. 在这种情况下,“ Search Text功能”适用于“ Status ”列,因为它填充了所有值,但是由于所有其他列都具有空值,因此“ Search Box功能不起作用。

Please see the above solution uses data from a variable in the Controller but I am using data from Backend(SpringBoot) and using it as a List. 请参阅上述解决方案使用Controller中变量的数据,但是我正在使用Backend(SpringBoot)数据并将其用作列表。

Please help me out in this situation. 请在这种情况下帮助我。

This is the snippet my Controller Class 这是我的控制器类的代码片段

var usecaseData = UseCaseOverview.get(function (){

        $scope.XLfilters = { list: [], dict: {}, results: [] };

        $scope.markAll = function(field, b) {
              $scope.XLfilters.dict[field].list.forEach(function(x){x.checked=b;});
              /*$scope.XLfilters.dict[field].list.forEach((x)=>{x.checked=b;});*/
            }

        $scope.clearAll = function(field) {
              $scope.XLfilters.dict[field].searchText='';
              $scope.XLfilters.dict[field].list.forEach(function(x){x.checked=true;});
              /*$scope.XLfilters.dict[field].list.forEach((x)=>{x.checked=true;});*/
            }

        $scope.XLfiltrate = function() {
            var i,j,k,selected,blocks,filter,option, data=$scope.XLfilters.all,filters=$scope.XLfilters.list;
            $scope.XLfilters.results=[];
            for (j=0; j<filters.length; j++) {
                filter=filters[j];
                filter.regex = filter.searchText.length?new RegExp(filter.searchText, 'i'):false;
                for(k=0,selected=0;k<filter.list.length;k++){
                    if(!filter.list[k].checked)selected++;
                  filter.list[k].visible=false;
                  filter.list[k].match=filter.regex?filter.list[k].title.match(filter.regex):true;
                }
                filter.isActive=filter.searchText.length>0||selected>0;
            }
            for (i=0; i<data.length; i++) {
                blocks={allows:[],rejects:[],mismatch:false};
                for (j=0; j<filters.length; j++) {
                  filter=filters[j]; option=filter.dict[data[i][filter.field]];
                  (option.checked?blocks.allows:blocks.rejects).push(option);
                  if(filter.regex && !option.match) blocks.mismatch=true;
                }
                if(blocks.rejects.length==1) blocks.rejects[0].visible=true;
                else if(blocks.rejects.length==0&&!blocks.mismatch){
                  $scope.XLfilters.results.push(data[i]);
                    blocks.allows.forEach(function(x){x.visible=true});
                    /*blocks.allows.forEach((x)=>{x.visible=true});*/
                }
            }
            for (j=0; j<filters.length; j++) {
                filter=filters[j];filter.options=[];
                for(k=0;k<filter.list.length;k++){
                  if(filter.list[k].visible && filter.list[k].match) filter.options.push(filter.list[k]);
                }
            }
        }


                    function createXLfilters(arr, fields) {
              $scope.XLfilters.all = arr;             
              for (var j=0; j<fields.length; j++) $scope.XLfilters.list.push($scope.XLfilters.dict[fields[j]]={list:[],dict:{},field:fields[j],searchText:"",active:false,options:[]});
              for (var i=0,z; i<arr.length; i++) for (j=0; j<fields.length; j++) {
                  z=$scope.XLfilters.dict[fields[j]];
                  z.dict[arr[i][fields[j]]] || z.list.push(z.dict[arr[i][fields[j]]]={title:arr[i][fields[j]],checked:true, visible:false,match:false});
              }
        }

        createXLfilters(usecaseData.list, ['searchId', 'ucname', 'company', 'division0', 'valueChain0', 'country', 'aiuc', 'resPerson', 'resPersonEmail', 'assocPerson1', 'assocPersonEmail1', 
            'pocSwarm', 'respBusinessUnit', 'emailRespBusinessUnit', 'businessDept', 'respItx', 'emailRespItx', 'itxDept', 'externalPartner', 'ucFullName', 'businessNeed', 
            'rolesResponsibilities', 'appearanceDate', 'dependencies', 'expetedBenefit', 'monetaryBenefit', 'problemType', 'method', 'dataSources', 'monetizeData', 'knowCustomer', 
            'incUtilization', 'monetization', 'stratAddedValue','feasibility', 'mvp', 'tagsKeywords', 'phase', 'activity', 'ucStatus', 'activityStart', 'curStatusComments', 'nextSteps', 
            'pocStartDate', 'availabilityDate', 'pocPresentationDate', 'decisionDate', 'appLiveDate', 'pocResult', 'pocTargetDate', 'datePocClosing']);
        //createXLfilters(vm.usecase_overview, ['ucname','resPerson']);             
    });

Finally after a study on the code, I came up with a solution. 最后,在研究了代码之后,我想出了一个解决方案。 The list was returning undefined when an element is not found. 找不到元素时,列表返回未定义。 So it was not working with Null values. 因此,它不适用于Null值。

Just had to add a check for undefined in the controller's XLfiltrate function and it worked. 只需在控制器的XLfiltrate函数中添加针对undefined的检查即可,并且可以正常工作。

if(filter.list[k].title){
                      filter.list[k].match=filter.regex?filter.list[k].title.match(filter.regex):true;
                  }

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

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