Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 5 records in table format, and i want to filter records by different parameters such as name, age and company.i have populated name, age, company in the drop-down and when user selects name, and if he enters anything in the textbox provided will be filtered by name, and if the user wants to search by age, he selects age in the dropdown and types in text box, records should be filtered by age, how to do it in angularjs
HTML
<pre><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="Scripts/angular.min.js"></script>
    <script src="Scripts/JavaScript1.js"></script>
    <title></title>
</head>
<body ng-app="MyModule" ng-controller="Mycontroller">
    <select id="Select1" ng-model="selected">
        <option>name</option>
        <option>age</option>
        <option>gender</option>
        <option>company</option>
    </select>
    <input type="text" placeholder="search" ng-model="parameter" />
    <table border="1" style="border:thin;border-collapse:collapse">
        <thead>
            <tr>
                <th>Name</th>
                <th>Age</th>
                <th>Gender</th>
                <th>Company</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="employee in employees|search">
                <td>{{employee.name}}</td>
                <td>{{employee.age}}</td>
                <td>{{employee.gender}}</td>
                <td>{{employee.company}}</td>
            </tr>
        </tbody>
    </table>
</body>
</html>


here is my controller code
JavaScript
<pre>/// <reference path="angular.min.js" />
var app = angular.module("MyModule", []).filter("search", function () {
    return function (employee) {
        if($scope.selected=='name')
        {
            if ($scope.parameter == employee.name)
                return employee;
        }
        if ($scope.selected == 'age') {
            if ($scope.parameter == employee.age)
                return employee;
        }
        if ($scope.selected == 'company') {
            if ($scope.parameter == employee.company)
                return employee;
        }
    }
}).controller("Mycontroller", function ($scope) {
    var employees=[{name:'raj',age:'23',gender:'M',company:'Mobileaspects'},
        {name:'santosh',age:'26',gender:'M',company:'Mobileaspects'},
        {name:'siddu',age:'21',gender:'M',company:'Mobileaspects'},
        {name:'sharana',age:'24',gender:'M',company:'TCS'},
        {name:'Rakshita',age:'23',gender:'F',company:'Mobileaspects'},
    ];
    $scope.employees = employees;
    $scope.selected = 'name';
   
})


What I have tried:

i tried various of binding data and using directives but no use, am newbiw to angularjs, help out plz, thanks in advance
Posted
Updated 26-Sep-19 17:15pm

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

    
    <title></title>
    <script>
        var app = angular.module("MyModule", []).filter("search", function ($filter) {
            return function (employees, $scope) {
                var selected = $scope.selected;
                var parameter = $scope.parameter;                
                if (parameter == undefined || parameter == '') return employees;
                var resultSet = [];
                for (var i = 0; i < employees.length; i++) {
                    var employee = employees[i];
                    if (selected == 'name') {
                        if (parameter == employee.name)
                            resultSet.push(employee);
                    }
                    if (selected == 'age') {
                        if (parameter == employee.age)
                            resultSet.push(employee);
                    }
                    if (selected == 'company') {
                        if (parameter == employee.company)
                            resultSet.push(employee);
                    }
                }
                return resultSet;

               
            }
        }).controller("Mycontroller", function ($scope) {
            var employees = [{ name: 'raj', age: '23', gender: 'M', company: 'Mobileaspects' },
                { name: 'santosh', age: '26', gender: 'M', company: 'Mobileaspects' },
                { name: 'siddu', age: '21', gender: 'M', company: 'Mobileaspects' },
                { name: 'sharana', age: '24', gender: 'M', company: 'TCS' },
                { name: 'Rakshita', age: '23', gender: 'F', company: 'Mobileaspects' },
            ];
            $scope.employees = employees;
            $scope.selected = 'name';

        })
    </script>
</head>
<body ng-app="MyModule" ng-controller="Mycontroller">
    <select id="Select1" ng-model="selected">
        <option>name</option>
        <option>age</option>
        <option>gender</option>
        <option>company</option>
    </select>
    <input type="text" placeholder="search" ng-model="parameter" />
    <table border="1" style="border:thin;border-collapse:collapse">
        <thead>
            <tr>
                <th>Name</th>
                <th>Age</th>
                <th>Gender</th>
                <th>Company</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="employee in employees|search:this">
                <td>{{employee.name}}</td>
                <td>{{employee.age}}</td>
                <td>{{employee.gender}}</td>
                <td>{{employee.company}}</td>
            </tr>
        </tbody>
    </table>
</body>
</html>
 
Share this answer
 
Comments
Member 12677894 5-Jun-17 3:35am    
thanks buddy, that works, but didn't understand why we must pass $filter object, and in ng-repeat, you passed 'this', instead if we write 'employees', it doesn't work, why is it so?
Karthik_Mahalingam 5-Jun-17 5:39am    
[^]
Member 12677894 5-Jun-17 7:53am    
thanks :-)
Karthik_Mahalingam 6-Jun-17 1:53am    
:)
with the same issue as with angular 8, is there anyone who can help me ?
 
Share this answer
 
Comments
Richard Deeming 27-Sep-19 8:34am    
If you want to ask a question, then ASK A QUESTION[^].

DO NOT post your question as a "solution" to someone else's question.

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