max_s
max_s

Reputation: 31

AngularJS smart-table strict search

Smart-table has a built in functionality to search through columns (st-search). Is it possible to perform a strict search for words, so with an exact match? For example: if I have table like this: id, name, status:

and do a search for 'John' only the first row should show. Is this possible?

Upvotes: 3

Views: 3633

Answers (3)

LatvjuAvs
LatvjuAvs

Reputation: 21

Old, but still relevant. If you have multiple input fields, but want strict only on some.
@elthrasher gave me the idea.

angular.module('app').filter('myStrictFilter', function($filter) {
    return function(input, predicate) {
        var response = input,
            strict = false,
            custom_predicate = {};
        // some input fields must be strict
        angular.forEach(predicate, function(value, key) {
            custom_predicate = {};
            custom_predicate[key] = value;
            strict = false;
            if (key === 'completion_status') { // strict it is.
                strict = true;
            }
            response = $filter('filter')(response, custom_predicate, strict);
        });
        return response;
    };
});

Upvotes: 1

elthrasher
elthrasher

Reputation: 1162

I had the same problem. My solution was to use the undocumented stSetFilter feature to pass in my own custom filter.

<table st-set-filter="myFilter" st-table="myData">

Then you just create an angular filter.

angular.module('myApp').filter('myFilter',
  function($filter) {
    return function(input, predicate) {
      return $filter('filter')(input, predicate, true);
    };
  }
);

In my case I needed to only do strict searches some of the time so I put some additional logic in the filter, but this will do you for doing strict searches all the time.

Mine looks more like this:

angular.module('myApp').filter('myFilter',
  function($filter) {
    return function(input, predicate) {
      var strict = false;
      if (predicate) { // some conditional if I want strict
        strict = true;
      }
      return $filter('filter')(input, predicate, strict);
    };
  }
);

Upvotes: 5

Jes&#250;s
Jes&#250;s

Reputation: 21

Yo should use strict comparator:

<input type="text" ng-model="query" />
<ul>
    <li data-ng-repeat="user in users | filter:{ name:query }:true">
      {{ user.id }}, {{ user.name }}, {{ user.address }}
    </li>
</ul>

Comparator which is used in determining if the expected value (from the filter expression) and actual value (from the object in the array) should be considered a match.

Can be one of:

function(actual, expected): The function will be given the object value and the predicate value to compare and should return true if both values should be considered equal.

true: A shorthand for function(actual, expected) { return angular.equals(actual, expected)}. This is essentially strict comparison of expected and actual.

false|undefined: A short hand for a function which will look for a substring match in case insensitive way.

Primitive values are converted to strings. Objects are not compared against primitives, unless they have a custom toString method (e.g. Date objects).

Upvotes: 0

Related Questions