0

I have a angular ui-grid in which I have column 'note' which can be either blank or non-blank. I am trying to create a custom filter to filter out blanks and non-blanks. I am not able to filter out blanks in the column. The column might contain null,undefined or ''. All these are shown as blank in ui-grid. Note column is below:

{
      displayName: 'Note',
      enableSorting:true,
      enableFiltering: true,
      enableCellEdit: false,
      width: '10%',
      field: 'note',
      visible: false,
      filterHeaderTemplate: '<div class="ui-grid-filter-container" ng-repeat=\
      "colFilter in col.filters"><div my-custom-dropdown2></div></div>',
      filter: {
        options: ['Blanks','Non-blanks']     // custom attribute that goes with custom directive above
      } 

.directive('myCustomDropdown2', function() {
  return {
    template: '<select class="form-control" ng-model="colFilter.term" ng-change="filterNotes(colFilter.term)" ng-options="option for option in colFilter.options"></select>',
    controller:'NoteController'
  };
})

.controller('NoteController',function($scope, $compile, $timeout){
  $scope.filterNotes = function(input){
    var field = $scope.col.field;
    var notes = _.pluck($scope.col.grid.options.data,field);
    $scope.colFilter.listTerm = [];
    var temp = notes.filter(function(val){if(val && val!=''){return val}});
    $scope.colFilter.listTerm = temp;
    $scope.colFilter.term = input;
    if(input=='Blanks'){
      $scope.colFilter.condition = new RegExp("[\\b]");
    }
    else{
      //for Non-blanks
      $scope.colFilter.condition = new RegExp($scope.colFilter.listTerm.join('|'));
    }
    console.log("$scope.colFilter.condition",$scope.colFilter.condition);
    // console.log("temp",temp);
  }
})

I have tried solutin given in this question and many more. Nothing worked. How should I create regex to match only blank cells?

AMahajan
  • 189
  • 1
  • 3
  • 17
  • is there a specific reason why you want to use regex for this instead of DOM functions? (they are usually better for this kind of work) – Kaddath Sep 07 '17 at 08:45
  • `/^(\s)*$/g` what blank means exactly? maybe `cell.innerHTML.length===0` is what you need? – Paweł Sep 07 '17 at 08:46
  • @Pawel blank means either only white spaces or null or undefined. – AMahajan Sep 07 '17 at 08:50
  • regular expressions are for `String` type, neither for `null` nor `undefined` If you mean that cell [String] content may be `"null"` or `"undefined"`: `/^((\s)*|null|undefined)$/g` try this out – Paweł Sep 07 '17 at 08:53
  • @Pawel thanks .. /^(\s)*$/g worked for me. – AMahajan Sep 07 '17 at 08:53
  • @Pawel could you please explain how the expression is working. I also added null and undefined as suggested by you. – AMahajan Sep 07 '17 at 08:58

1 Answers1

2

Use /^(\s)*$/g.

The \s metacharacter matches a white space character in string. \s is equivalent to [ \f\n\r\t\v\u00a0\u1680\u180e\u2000\u200a\u2028\u2029\u202f\u205f\u3000\ufeff], so it covers white spaces, tabs, line break, etc.

The ^n quantifier matches any character combination n at the beginning of string.

The n$ quantifier matches any character combination n at the end of string.

So if you use /^\s$/ you expect that the string should contain only one white-space.

The n* quantifier matches any character combination that contains 0 or more occurances of preceding n expression.

So if you use /^(\s)*$/ you expect that the string should be empty, or contain any number of white spaces (but nothing else).

I made regexp tutorial for my portfolio so you can experiment there with plenty of default or your own regexp samples and text samples.

Edit: The g flag seems to be unnecessary here, I was writing in a hurry. So you can use /^(\s)*$/.

Paweł
  • 4,238
  • 4
  • 21
  • 40
  • I used regular expression including null and undefined. Thanks for help. It saved lots of time and your regexp tutorial is great. – AMahajan Sep 07 '17 at 09:14