2

I have an application which shows data in tabular form which has columns

id, name, price, quantity

A am showing the data using ng-repeat Please see this

Plunker

<body ng-controller="myController">
<h1>Data</h1>

<table>
  <tr>
    <th>ID</th>
    <th>NAME</th>
    <th>PRICE</th>
    <th>QUANTITY</th>
  </tr>
  <tr ng-repeat="item in myData">
    <td>{{ item.id }}</td>
    <td>{{ item.name }}</td>
    <td>{{ item.price }}</td>
    <td>{{ item.quantity }}</td>
  </tr>
</table>

What I want to do is to add multiple filters in ng-repeat on this table

a) Filter by 'Name'

b) Filter by 'Price' OR 'Quantity'

It means that at any given point of time the result of the table should be filtered by combination of

i) EITHER 'Name' and 'Price'

ii) OR 'Name' and 'Quantity'

The Quantity filter should be inactive when Price filter is active and vice versa.

I will have 3 input fields for the filter parameters.

How can I apply filters to the ng-repeat in html to achieve this?

6
  • do you mean to filter or sort? also from where do you get input for filter value which is required for filtering? for example, how would you know what quantity to filter on ? Commented Jun 16, 2016 at 13:43
  • "How can I apply filters to the ng-repeat to achieve this?" - The same way as you would do with normal filtering in Angular. This has already been asked many times. As for disabling this is also trivial with some boolean flags and ngChange that would set them. Commented Jun 16, 2016 at 13:43
  • ng-table.com Commented Jun 16, 2016 at 13:48
  • @gaurav5430 I want to filter the resultset, I will have 3 text boxes for Name, Price and Quantity. name is a straight-forward filter, Now When user inputs content in Price input, the other one(quantity) should get empty and vice versa Commented Jun 16, 2016 at 14:17
  • @dfsq I have no issue in implementing the filter in ng-repeat, here, my main concern is to toggle between Price and Quantity filter, Only one of them should be applied at one instance. Commented Jun 16, 2016 at 14:19

2 Answers 2

2

I suggest you create a custom filter and pass a custom filterObject, containing all filteroptions to this filter.

<tr ng-repeat="item in myData | myCustomFilter:filterObject">
  <td>{{ item.id }}</td>
  <td>{{ item.name }}</td>
  <td>{{ item.price }}</td>
  <td>{{ item.quantity }}</td>
</tr>

your filterobject would look like :

$scope.filterObject = {
    id: false,
    name: true,
    price: false,
    quantity: true
}

In myCustomFilter :

app.filter('myCustomFilter', function(items, filterObject) {
    // filter your items depending on which filters are enabled
});
Sign up to request clarification or add additional context in comments.

Comments

0

this is test myDatafilter add in js

myDatafilter = function () {};

<h1>Data</h1>
<input type="text" ng-model="myDatafilter"/>
<tr ng-repeat="item in myData | filter:myDatafilter">

1 Comment

You should really add some explanation as to why this code should work - you can also add comments in the code itself - in its current form, it does not provide any explanation which can help the rest of the community to understand what you did to solve/answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.