angularjs表格ng-table使用备忘录
来源: 阅读:1958 次 日期:2016-07-21 15:09:46
温馨提示: 小编为您整理了“angularjs表格ng-table使用备忘录”,方便广大网友查阅!

这篇文章主要为大家分享了angularjs表格ng-table使用备忘录,感兴趣的小伙伴们可以参考一下

项目中用到angularjs的表格ng-table,功能相当强大,像搜索、排序、checkbox、分页、每页表格显示数目等都有。API,demo什么的也只能参考官网了。这里做个备忘,哪天肯定还会用到。

HTML:

<!DOCTYPE html>

<html>

<meta charset="utf-8"/>

<head>

  <script data-require="angular.js@*" data-semver="1.2.0-rc3-nonmin" src="angular.js"></script>

  <script data-require="ng-table@*" data-semver="0.3.0" src="ngTable.js"></script>

  <link data-require="ng-table@*" data-semver="0.3.0" rel="stylesheet" href="http://bazalt-cms.com/assets/ng-table/0.3.2/ng-table.css" />

  <link data-require="bootstrap-css@*" data-semver="3.0.0" rel="stylesheet" href="bootstrap.min.css" />

  <link rel="stylesheet" href="style.css" />

  <script src="script.js"></script>

</head>

<body ng-app="main" ng-controller="DemoCtrl">

  <p><strong>Page:</strong> {{tableParams.page()}}

  <p><strong>Count per page:</strong> {{tableParams.count()}}

<p>Filter: <input class="form-control" type="text" ng-model="filter.$" /></p>

  <table ng-table="tableParams" show-filter="true" class="table">

    <tr ng-repeat="user in $data" ng-class="{ 'emphasis': user.money > 500 }">

      <td width="30" style="text-align: left" header="'ng-table/headers/checkbox.html'">

        <input type="checkbox" ng-model="checkboxes.items[user.organizationId]" />

      </td>

      <td data-title="'编号'" sortable="'organizationId'">

        {{user.organizationId}}

      </td>

      <td data-title="'名称'" sortable="'name'">

        {{user.name}}

      </td>

    </tr>

  </table>

  <script type="text/ng-template" id="ng-table/headers/checkbox.html">

    <input type="checkbox" ng-model="checkboxes.checked" id="select_all" name="filter-checkbox" value="" />

  </script>

</body>

</html>

js:

var app = angular.module('main', ['ngTable']).

controller('DemoCtrl', function($scope, ngTableParams, NameService) {

  var data = NameService.data;

  $scope.tableParams = new ngTableParams(

   {

    page: 1,      // show first page

    count: 10,      // count per page

    sorting: {name:'asc'}

   },

   {

    total: 0, // length of data

    getData: function($defer, params) {

     NameService.getData($defer,params,$scope.filter);

    }

  });

  $scope.$watch("filter.$", function () {

    $scope.tableParams.reload();

  });

});

app.service("NameService", function($http, $filter){

 function filterData(data, filter){

  return $filter('filter')(data, filter);

 }

 function orderData(data, params){

  return params.sorting() ? $filter('orderBy')(data, params.orderBy()) : filteredData;

 }

 function sliceData(data, params){

  return data.slice((params.page() - 1) * params.count(), params.page() * params.count())

 }

 function transformData(data,filter,params){

  return sliceData( orderData( filterData(data,filter), params ), params);

 }

 var service = {

  cachedData:[],

  getData:function($defer, params, filter){

   if(service.cachedData.length>0){

    console.log("using cached data")

    var filteredData = filterData(service.cachedData,filter);

    var transformedData = sliceData(orderData(filteredData,params),params);

    params.total(filteredData.length)

    $defer.resolve(transformedData);

   }

   else{

    console.log("fetching data")

    $http.get("data.json").success(function(resp)

    {

     angular.copy(resp,service.cachedData)

     params.total(resp.length)

     var filteredData = $filter('filter')(resp, filter);

     var transformedData = transformData(resp,filter,params)

      

     $defer.resolve(transformedData);

    }); 

   }

  }

 };

 return service; 

});

json数据:

[{

      "hidden": 1,

      "launchImage": "2015_4_9_12_bb7e0a446ed74ae7ad0a3554a54183bc_1298477763",

      "name": "张三",

      "orgId": 1498031949070997504,

      "organizationId": "1498031949070997504"

    }, {

      "hidden": 1,

      "launchImage": "http://7s1rmc.com1.z0.glb.clouddn.com/2015_4_9_12_0ab5b02cf1df4ac49a7376b820816307_2127491219|39e202124ac90ec531009b0edbbbe0ba",

      "name": "李四",

      "orgId": 1498046360909250560,

      "organizationId": "1498046360909250560"

    }]

以上就是本文的全部内容,希望对大家的学习有所帮助。

更多信息请查看网络编程
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:
云南网警报警专用图标
Baidu
map