Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Data Binding, Data Filter and Sorting in Angular JS using WEB API

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 326
    Comment on it

    In today's world, there are lots of client side scripts to make a better user interface. In this article, we are going to bind the data and apply sorting and filtering using AngularJS.

    Open your favourite text editor and create directory structure for new angular application.

    Add service, controller and angular JS library reference into your HTML page.

    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
     
        <script src="http://angular-ui.github.io/ui-router/release/angular-ui-router.min.js"></script>
        <script src="../Controllers/empController.js"></script>
        <script src="../Services/empService.js"></script>
     <div ng-app="MyApp">
                <div ng-controller="crudController">
                    <h4>Employee Data</h4>
                    <table class="table table-striped table-hover">
                        <tr>
                            <td width="500px"><b>Search :</b></td>
                            <td><input type="text" class="form-control" ng-model="search.Name" /></td>
                            <td><input type="text" class="form-control" ng-model="search.Address" /></td>
                        </tr>
                            <tr>
                                <th ng-click="orderByMe('Age')">Age</th>
                                <th ng-click="orderByMe('Name')">Name</th>
                                <th ng-click="orderByMe('Address')">Address</th>
                            </tr>
                        <tbody ng-repeat="Emp in employees  | orderBy:myOrderBy | filter:search">
                            <tr>
                                <td> <span>{{Emp.Age}}</span></td>
                                <td> <span>{{Emp.Name}}</span></td>
                                <td> <span>{{Emp.Address}}</span></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

     

    Now we call our web api from controller and bind the data on index.html page using angular directive ng-repeat


     

    angular.module('MyApp')
    .controller('crudController', function ($scope, crudService) {
        $scope.IsNewRecord = 1
        loadRecords();
        function loadRecords() {
            var promiseGet = crudService.getEmployees();
            promiseGet.then(function (pl) { $scope.employees = pl.data },
                  function (errorPl) {
                      $log.error('failure loading Employee', errorPl);
                  });
        }
    }
    .service('crudService', function ($http) {
        this.getEmployees = function () {
            return $http.get("/api/crud/GetEmployeesAPI");
        }
    });

    Here is the output :

     

     

    Sorting and Filtering data :

    For sorting and filter data column wise, we bind the our ng-click on column header and add two text box and bind these with search model as :

    <tr>
                  <td><b>Search :</b></td>
                  <td><input type="text" class="form-control" ng-model="search.Name" /></td>
                  <td><input type="text" class="form-control" ng-model="search.Address"/</td>
    </tr>
    <tr>
                 <th ng-click="orderByMe('Age')">Age</th>
                 <th ng-click="orderByMe('Name')">Name</th>
                 <th ng-click="orderByMe('Address')">Address</th>
    </tr>

    and add filter in ng-repeat directive as :

    <tbody ng-repeat="Emp in employees  | orderBy:myOrderBy | filter:search">

    Now implement orderByMe function in our controller and bind myOrderBy with column name.

      

      $scope.orderByMe = function (x) {
            $scope.myOrderBy = x;
        }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: