0

I have a directive for the mover list called smDualList. The directive has the following template (partial code):

<div>
                <select class="select-list" multiple
                        ng-model="unassigned"
                        name="unAssignedList"
                        data-no-dirty-check
                        ng-options="unassignedItem.descrip for unassignedItem in unassignedItems | orderBy:'descrip' | filter: filterCriteria"></select>
            </div>

This is my page markup:

<data-sm:duallist-directive ng-required="false" keep-pristine="true"
                                        unassigned-items-title="'@String.Format(Labels.availableX, Labels.items)'"
                                        unassigned-items="currentBenefit.unassignedItems"
                                        assigned-items-title="'@String.Format(Labels.assignedX, Labels.items)'"
                                        assigned-items="currentBenefit.assignedItems"
                                        sortable="false"
                                        filter-criteria="{'categoryId':selectedCategoryId}"
                                        selected-item="currentBenefit.selectedItem">
            </data-sm:duallist-directive>

with the following code for the directive.js file:

 restrict: 'E',
            templateUrl: 'app/templates/smDuallist',
            require: ['^form'],
            scope: {
                assignedItems: '=assignedItems',
                unassignedItems: '=unassignedItems',
                assignedItemsTitle: '=',
                unassignedItemsTitle: '=',
                filterCriteria: '=',
                sortable: '=sortable',
                selectedItem: '=?',
                keepPristine: '=?'
            },
            link: function (scope, element, attrs, ctrls) {
                scope.form = ctrls[0];
                if (!scope.keepPristine)
                    scope.keepPristine = false;                
            }

So, the problem is that I need to add a true parameter at the end of the filter-criteria expression, so it would read as

filter-criteria="{'myColumn': myScopeProperty}:true"

Unfortunately, when I try to put it exactly like this on the form I get an error.

angular.js:13920 [Error: [$parse:syntax] Syntax Error: Token ':' is an unexpected token at column 34 of the expression [{'categoryId':selectedCategoryId}:true] starting at [:true].] http://errors.angularjs.org/1.5.8/$parse/syntax?p0=%3A&p1=is%20an%20unexpec…20token&p2=34&p3=%7B'categoryId'%3AselectedCategoryId%7D%3Atrue&p4=%3Atrue at http://localhost:9753/SiriuswareControl/Scripts/angular.js:68:12 at AST.throwError (http://localhost:9753/SiriuswareControl/Scripts/angular.js:14555:11) at AST.ast (http://localhost:9753/SiriuswareControl/Scripts/angular.js:14308:12) at ASTCompiler.compile (http://localhost:9753/SiriuswareControl/Scripts/angular.js:14771:31) at Parser.parse (http://localhost:9753/SiriuswareControl/Scripts/angular.js:15700:29) at $parse (http://localhost:9753/SiriuswareControl/Scripts/angular.js:15865:39) at initializeBinding (http://localhost:9753/SiriuswareControl/Scripts/angular.js:9988:25) at forEach (http://localhost:9753/SiriuswareControl/Scripts/angular.js:329:18) at initializeDirectiveBindings (http://localhost:9753/SiriuswareControl/Scripts/angular.js:9947:7) at nodeLinkFn (http://localhost:9753/SiriuswareControl/Scripts/angular.js:9242:30)

May be there is a way to somehow add that true parameter in the directive JavaScript code instead?

That last parameter is from the following discussion: Angular filter exactly on object key

I can probably create a filter function, but how can I create it to be generic enough to know which property to use? Because I have several movers in my form and I don't want to create a function in the controller for each of them.

Community
  • 1
  • 1
Naomi
  • 718
  • 1
  • 9
  • 28
  • what is the error message you are getting? – samnu pel May 18 '17 at 16:02
  • angular.js:13920 [Error: [$parse:syntax] Syntax Error: Token ':' is an unexpected token at column 34 of the expression [{'categoryId':selectedCategoryId}:true] starting at [:true].] – Naomi May 18 '17 at 16:09

1 Answers1

1

Just write :true after your filtercriteria. If you need it flexible, you can define truealso as a variable. Sample

Example:

var myApp=angular.module('myApp', []);

myApp.controller('Ctrl',function ($scope) {
  $scope.myfilter = {'id':  1};    
  $scope.excactfilter = true;
  $scope.users = [
{"id":1,"username":"simon",},
{"id":8,"username":"betty",},
{"id":14,"username":"archie",},
{"id":3,"username":"jumbo1"},
  ];     
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
  <div ng-controller="Ctrl">
<div ng-repeat="user in users | filter:myfilter:excactfilter">
  <span>{{ user.username }}, {{ user.id }}</span>
</div>
  </div>
</body>
Stefan
  • 14,826
  • 17
  • 80
  • 143
  • I am going to try that, but I am a bit worried. I have many cases where I don't define any filter-criteria at all. Would it still work? – Naomi May 18 '17 at 16:19
  • Seems to be working well. As always, I was trying to over-complicate things. Thanks, Stefan – Naomi May 18 '17 at 16:23
  • 1
    if you leave filter element empty you get all results, no metter if you define excact or not. Sample: filter: {}:true. Thanks – Stefan May 18 '17 at 16:24