Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I do have table which where i will have dropdown list which will have '<=, >=, =' option and another input field where user enter the value to filter. so it will be like if <= -1. >=0 , =1

How do i filter data in my table using angular js?


I tried to write a function and now it does not even populate table with initial data before filter.

What I have tried:

Item


Order

Line

Status

ToLocation

Qty







{{c}}





RegDate







{{c}}









{{p.Item}}
{{p.Order}}
{{p.Line}}
{{p.Status}}
{{p.ToLocation}}
{{p.Qty}}
{{p.RegDate}}


<div ng-controller="demoController">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <td>Item<br />
                            <input type="text" ng-model="Search.Item" />
                        </td>
                        <td>Order<br />
                            <input type="text" ng-model="Search.Order" /></td>
                        <td>Line<br />
                            <input type="text" ng-model="Search.Line" /></td>
                        <td>Status<br />
                            <input type="text" ng-model="Search.Status" /></td>
                        <td>ToLocation<br />
                            <input type="text" ng-model="Search.ToLocation" /></td>
                        <td>Qty<br />
                            <table>
                                <tr>
                                    <td>
                                        <input type="text" ng-model="compValue"/>
                                    </td>
                                    <td>
                                        <select ng-model="compFilter" ng-change="onChange(compFilter,compValue)">
                                            <option ng-repeat="c in Comparers" value="{{c}}">{{c}}</option>
                                        </select>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td>RegDate<br />
                            <table>
                                <tr>
                                    <td>
                                        <input type="text" ng-model="dateFilter" />
                                    </td>
                                    <td>
                                        <select ng-model="compFilter" ng-change="onChange(compFilter,compValue)">
                                            <option ng-repeat="c in Comparers" value="{{c}}">{{c}}</option>
                                        </select>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="p in Products |filter:Search">
                        <td>{{p.Item}}</td>
                        <td>{{p.Order}}</td>
                        <td>{{p.Line}}</td>
                        <td>{{p.Status}}</td>
                        <td>{{p.ToLocation}}</td>
                        <td>{{p.Qty}}</td>
                        <td>{{p.RegDate}}</td>
                    </tr>
                </tbody>
            </table>
        </div>



Angualr code :
var app = angular.module("demoModule", []);

app.controller("demoController", function ($scope, $http) {

    $scope.Comparers = ['=', '>=', '<='];
    $scope.compValue = "";

    $http(
        {
            method: "POST",
            url: "ProdForm.aspx/GetProduct",
            data: {},
            contentType: "application/json",
            dataType: "json"

        }).then(function (response) {

            $scope.Products = JSON.parse(response.data.d);
            debugger;
        });

    $scope.onChange = function (compFilter, compValue, Products) {
       
            var newArray = new Array();
            if (compFilter === '=') {
               ;
               for (var i = 0; i < Products.length; i++) {
                    var j = 0;
                    if (Products[i].Qty == compValue) {
                        newArray[j] = Products[i];
                        j = j + 1
                    }
                }
            }
            else if (compFilter === '>=') {
               
                for (var i = 0; i < Products.length; i++) {
                    var j = 0;
                    if (Products[i].Qty >= compValue) {
                        newArray[j] = Products[i];
                        j = j + 1
                    }
                }
            }
            else {

                for (var i = 0; i < Products.length; i++) {
                    var j = 0;
                    if (Products[i].Qty >= compValue) {
                        newArray[j] = Products[i];
                        j = j + 1
                    }
                }
            }

            $scope.Products = newArray;
    }
});
Posted
Updated 13-Jul-17 4:58am
v5
Comments
Karthik_Mahalingam 13-Jul-17 3:09am    
can you host it in jsfiddle?

1 solution

Well I knocked up a jsFiddle version myself: CodePerfect: Filter Issues[^]

So there are plenty of issues here, but let's highlight some basic ones

1: Loop counter reset
Your filter for loops:
JavaScript
for (var i = 0; i < $scope.Products.length; i++) {
        var j = 0;
        if ( $scope.Products[i].Qty >= compValue) {
          newArray[j] =  $scope.Products[i];
          j = j + 1
        }
      }


2: add to an array
so at the start of each loop it sets j = 0? do you want j to reset every loop?
newArray[j] why? Just do this:
JavaScript
let result = []
for(...){
  result.push(item)
}

No need to iterate through the result array. just add new items with a push.

3: welcome to Ecmascript6 (or javascript as you now it)
Check this out:
JavaScript
newArray = $scope.Products.filter(p=>p.Qty>=compValue)

did you just see that? wow :P

So here is your whole filter function so far (with switch)
JavaScript
$scope.onChange = function(compFilter, compValue) {
  $scope.Products = $scope.allProducts
  let newArray = [];
  switch(compFilter)
  {
      case '=':
    newArray = $scope.Products.filter(p=>p.Qty==compValue)
    break;
    case '>=':
    newArray = $scope.Products.filter(p=>p.Qty>=compValue)
    break;
    default:
    newArray = $scope.Products.filter(p=>p.Qty<=compValue)
    break;
  }
  $scope.Products = newArray;
}


I made a few small changes, but you get the idea.

We can go much further. You are using about 5% of angular's potential and about 0.1% of javascript. I will expand if you like. We can make this simple device very neat indeed
 
Share this answer
 
Comments
[no name] 13-Jul-17 10:55am    
Sir, thank you. but there is problem, I want filter such that it compares the value it gets in the input field and then filter the data as per comparer (>=, =, <=) and populate the table. its not doing it. So if you again check my html code in question which I have updated. I do my compare function work like search filter .
Andy Lanng 13-Jul-17 11:03am    
try the new fiddle: https://jsfiddle.net/bxrwnb7z/1/
Let me know if this works for you
[no name] 13-Jul-17 11:20am    
could not open the jsfiddle link
Andy Lanng 13-Jul-17 11:38am    
Andy Lanng 13-Jul-17 11:04am    
PS: to add code you can update the question. Just please add an "UPDATE" note to your changes ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900