{"id":567,"date":"2014-10-17T22:40:32","date_gmt":"2014-10-17T22:40:32","guid":{"rendered":"http:\/\/www.honobono-life.info\/wpeng\/?p=567"},"modified":"2017-10-08T08:18:23","modified_gmt":"2017-10-08T08:18:23","slug":"angularjs-ng-grid-filter-grid-data","status":"publish","type":"post","link":"http:\/\/www.honobono-life.info\/wpeng\/angularjs-ng-grid-filter-grid-data\/","title":{"rendered":"AngularJS ng-grid filter paging grid data"},"content":{"rendered":"<ul>\r\n<li>AngularJS : 1.2.23<\/li>\r\n<li>Bootstrap : 3.x<\/li>\r\n<li>ng-grid : 2.0.13<\/li>\r\n<li>include file<\/li>\r\nng-grid.min.css,jquery.js,angular.min.js,ng-grid.min.js,ui-bootstrap-tpls-0.11.0.min.js\r\n<\/ul>\r\n<!--more-->\r\n<link href=\"http:\/\/www.honobono-life.info\/wpeng\/wp-content\/themes\/ang\/css\/ng-grid.min.css\" rel=\"stylesheet\">\r\n<link href=\"http:\/\/www.honobono-life.info\/wpeng\/wp-content\/themes\/ang\/css\/grid1.css\" rel=\"stylesheet\">\r\n<script type='text\/javascript' src='http:\/\/www.honobono-life.info\/wpeng\/wp-content\/themes\/ang\/js\/angular.min.js'><\/script><script type='text\/javascript' src='http:\/\/www.honobono-life.info\/wpeng\/wp-content\/themes\/ang\/js\/ui-bootstrap-tpls-0.11.0.min.js'><\/script>\r\n<script type='text\/javascript' src='http:\/\/www.honobono-life.info\/wpeng\/wp-content\/themes\/ang\/js\/ng-grid-2.0.13.min.js'><\/script>\r\n<script type='text\/javascript' src='http:\/\/www.honobono-life.info\/wpeng\/wp-content\/themes\/ang\/js\/grid6.js'><\/script>\r\n<div ng-app=\"googleMapApp\" role=\"tabpanel\">\r\n  <ul class=\"nav nav-tabs\">\r\n    <li class=\"active\"><a href=\"#demo1\" data-toggle=\"tab\">demo<\/a><\/li>\r\n    <li><a href=\"#html1\" data-toggle=\"tab\">markup<\/a><\/li>\r\n    <li><a href=\"#script1\" data-toggle=\"tab\">script<\/a><\/li>\r\n    <li><a href=\"#template1\" data-toggle=\"tab\">Server PHP<\/a><\/li>\r\n  <\/ul>\r\n  <div class=\"tab-content\">\r\n    <div class=\"tab-pane active\" id=\"demo1\">\r\n\r\n<div ng-controller=\"MyCtrl\">\r\n  <div class=\"row\">\r\n    <div class=\"col-sm-12\">\r\n      <strong>Filter:<\/strong><input type=\"text\" ng-model=\"filterOptions.filterText\" \/>\r\n      <div class=\"gridStyle\" ng-grid=\"gridOptions\">\r\n      <\/div>\r\n    <\/div>\r\n  <\/div>\r\n<\/div>\r\n\r\n    <\/div>\r\n    <div class=\"tab-pane\" id=\"html1\">\r\n<pre>\r\n&lt;link href=\"http:\/\/www.example.com\/wp\/wp-content\/themes\/ang\/css\/ng-grid.min.css\" rel=\"stylesheet\">\r\n&lt;link href=\"http:\/\/www.example.com\/wp\/wp-content\/themes\/ang\/css\/grid1.css\" rel=\"stylesheet\">\r\n&lt;script type='text\/javascript' src='http:\/\/www.example.com\/wp\/wp-content\/themes\/ang\/js\/ng-grid.min.js'>&lt;\/script>\r\n&lt;script type='text\/javascript' src='http:\/\/www.example.com\/wp\/wp-content\/themes\/ang\/js\/grid1.js'>&lt;\/script>\r\n&lt;div ng-controller=\"MyCtrl2\">\r\n  &lt;div class=\"row\">\r\n    &lt;div class=\"col-sm-12\">\r\n      &lt;strong>Filter:&lt;\/strong>&lt;input type=\"text\" ng-model=\"filterOptions.filterText\" \/>\r\n      &lt;div class=\"gridStyle\" ng-grid=\"gridOptions\">\r\n      &lt;\/div>\r\n    &lt;\/div>\r\n  &lt;\/div>\r\n&lt;\/div>\r\n<\/pre>\r\n    <\/div>\r\n\r\n    <div class=\"tab-pane\" id=\"script1\">\r\n<pre>\r\nvar myApp = angular.module('googleMapApp', ['ui.bootstrap','ngGrid']);\r\n\r\nmyApp.controller('MyCtrl', function($scope,$http) {\r\n    $scope.pagingOptions = {\r\n      pageSizes: [3, 5, 8],\r\n      pageSize: 3,\r\n      currentPage: 1\r\n    };\r\n    $scope.totalServerItems = 0;\r\n    $scope.setPagingData = function(data,page,pageSize){\r\n      var pagedData = data.slice((page - 1) * pageSize, page * pageSize);\r\n      $scope.myData = pagedData;\r\n      $scope.totalServerItems = data.length;\r\n      if (!$scope.$$phase) {\r\n        $scope.$apply();\r\n      }\r\n    };\r\n    $scope.getPagedDataAsync = function (pageSize, page, searchText) {\r\n      setTimeout(function () {\r\n        var data;\r\n        if (searchText) {\r\n          var ft = searchText.toLowerCase();\r\n          $http.get('..\/wp-content\/themes\/ang\/json-grid.php').success(function (largeLoad) {\r\n            data = largeLoad.filter(function(item) {\r\n              return JSON.stringify(item).toLowerCase().indexOf(ft) != -1;\r\n            });\r\n            $scope.setPagingData(data,page,pageSize);\r\n          });\r\n        } else {\r\n          $http.get('..\/wp-content\/themes\/ang\/json-grid.php').success(function (largeLoad) {\r\n            $scope.setPagingData(largeLoad,page,pageSize);\r\n          });\r\n        }\r\n      }, 100);\r\n    };\r\n\r\n    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);\r\n\r\n    $scope.$watch('pagingOptions', function (newVal, oldVal) {\r\n      if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {\r\n        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);\r\n      }\r\n    }, true);\r\n\r\n    $scope.$watch('filterOptions', function (newVal, oldVal) {\r\n      if (newVal !== oldVal) {\r\n        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);\r\n      }\r\n    }, true);\r\n\r\n    $scope.filterOptions = {\r\n        filterText: \"\",\r\n        useExternalFilter: true\r\n    };\r\n    $scope.gridOptions = {\r\n      data: 'myData',\r\n      enablePaging: true,\r\n      showFooter: true,\r\n      totalServerItems: 'totalServerItems',\r\n      pagingOptions: $scope.pagingOptions,\r\n      filterOptions: $scope.filterOptions\r\n    };\r\n});\r\n<\/pre>\r\n    <\/div><!-- \/.tab-pane -->\r\n\r\n    <div class=\"tab-pane\" id=\"template1\">\r\n<pre>\r\n&lt;?php\r\nrequire(\"dbinfo.php\");\r\n$dsn = 'mysql:host=localhost;dbname='.$database.';charset=utf8';\r\ntry {\r\n  $dbh = new PDO($dsn, $username, $password,array(PDO::ATTR_EMULATE_PREPARES =>\r\nfalse,PDO::MYSQL_ATTR_INIT_COMMAND => \"SET CHARACTER SET `utf8`\"));\r\n} catch (PDOException $e) {\r\n  exit('DB connect error'.$e->getMessage());\r\n}\r\n$st = $dbh->query(\"SELECT * FROM grid_products WHERE 1\");\r\necho json_encode($st->fetchAll(PDO::FETCH_ASSOC));\r\n\r\n?>\r\n<\/pre>\r\n    <\/div><!-- \/.tab-pane -->\r\n\r\n  <\/div><!-- \/.tab-content -->\r\n<\/div><!-- \/.tabpanel -->","protected":false},"excerpt":{"rendered":"<p>AngularJS : 1.2.23 Bootstrap : 3.x ng-grid : 2.0.13 include file ng-grid.min.css,jquery.js,angular.min.js,ng-grid.min.js,ui-bootstrap-tpls-0.11.0.min.js<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[74,73,69],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/567"}],"collection":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/comments?post=567"}],"version-history":[{"count":8,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/567\/revisions"}],"predecessor-version":[{"id":1482,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/567\/revisions\/1482"}],"wp:attachment":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/media?parent=567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/categories?post=567"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/tags?post=567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}