AngularJS orderby with empty field

前端 未结 9 1768
北海茫月
北海茫月 2020-12-05 13:19

I am ordering a my data and its working all correcty except some fields are empty or have no value. When ordered these empty field come up first. For example when ordering n

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 13:53

    I don't know why other answer suggest to put the null value records at the bottom, If I want to sort normally, means in ASC order all the null on top and in DESC order all the nulls go to bottom, I tried other answers here but could not helped me so change the code to convert the null to '' in my array and it works now smooth like this:

    $scope.convertNullToBlank = function (array) {
      for (var i = 0; i < array.length; i++) {
         if (array[i].col1 === null)
           array[i].col1 = '';
    
         if (array[i].col2 === null)
            array[i].col2 = '';
      }
      return array;
    }
    

提交回复
热议问题