0

I want to pass two argument to the custom filter 'from' and 'to' into the custom filter I have created in my controller.

Here you can see my custom filter I have created:

    vm.filterMinutes = function (prop, from, to) {
        return function (item) {
            return item[prop] >= from && item[prop] <= to;
        };
    };

And the view looks like this:

<label>Search from: <input ng-model="fromMinutes"></label>
<label>Search from: <input ng-model="toMinutes"></label>

    <tr style="cursor: pointer;" ng-repeat="student in AdminReportsWorksnaps.data | filter: AdminReportsWorksnaps.filterMinutes('totalMinutes', fromMinutes,toMinutes)">
                <td>{{ student.studentId }}</td>
                <td>{{ student.firstName }}</td>
                <td>{{ student.lastName }}</td>
                <td>{{ student.municipality }}</td>
                <td class="total success">{{ student.totalMinutes | number}}</td>
   </tr>

For some reason this is not working. Well, If I Call filter like this: filter: AdminReportsWorksnaps.filterMinutes('totalMinutes', 5000,6000)" it works perfectly fine.. I just cant see how can I pass the input values from the textboxs.

Thanks

2 Answers 2

1

Pass it as : separated (Assuming you are using controllerAs pattern with vm alias)

ng-repeat="student in AdminReportsWorksnaps.data | filter: AdminReportsWorksnaps.filterMinutes: 'totalMinutes': fromMinutes: toMinutes"

So in above case you need to expect 4 parameter in a filter, 1st parameter would be AdminReportsWorksnaps.data, 2nd would be totalMinutes, 3rd fromMinutes & last would be toMinutes value

vm.filterMinutes = function (collection, prop, from, to) {
    //collection would have 
    console.log("AdminReportsWorksnaps.data", collection)
    console.log(prop, from, to);
    return (collection || []).filter(function (item) {
        return item[prop] >= from && item[prop] <= to;
    });
};
Sign up to request clarification or add additional context in comments.

5 Comments

@HisniFazlija is your controller alias is AdminReportsWorksnaps?
yes and I tried with AdminReportsWorksnaps too but still does not seem like working
so I tried this: ng-repeat="student in AdminReportsWorksnaps.data | filter: AdminReportsWorksnaps.filterMinutes: 'totalMinutes': fromMinutes: toMinutes"
it prints the collection, and I can see the consoles updating. but the table is not filtering yet...
Now I get this error: VM19414:27 TypeError: collection.filter is not a function
0

It look like you're using a controllerAs, so why don't use the controller alias for the models too :

<label>Search from: <input ng-model="AdminReportsWorksnaps.fromMinutes"></label>
<label>Search from: <input ng-model="AdminReportsWorksnaps.toMinutes"></label>

<tr style="cursor: pointer;" ng-repeat="student in AdminReportsWorksnaps.data | filter: AdminReportsWorksnaps.filterMinutes('totalMinutes', AdminReportsWorksnaps.fromMinutes, AdminReportsWorksnaps.toMinutes)">
    <td>{{ student.studentId }}</td>
    <td>{{ student.firstName }}</td>
    <td>{{ student.lastName }}</td>
    <td>{{ student.municipality }}</td>
    <td class="total success">{{ student.totalMinutes | number}}</td>

It should works like that.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.