angularJs + datatable使用示例

1.html

<div class="wrapper wrapper-content animated fadeInRight" ng-controller="inventoryLogCtrl as ctrl">
    <div class="table-responsive">
        <table id="so_inventory" datatable
               dt-options="ctrl.dataTable.dtOptions"
               dt-columns="ctrl.dataTable.dtColumns"
               dt-instance="ctrl.dataTable.dtInstance"
               class="table table-striped table-bordered table-hover dataTables-example darkbg-hover item-search-datatables"
               dt-disable-deep-watchers="true">
            <thead>
                <tr>
                    <th>订单号</th>
                    <th>商品号</th>
                    <th>订单类型</th>
                    <th>订单时间</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
</div>

2.angularJS controller

/**
 * Created by Scott on 2016/5/17.
 */
/**
 * @author Scott Huang
 * @since 2016-05-17
 */
(function (angular) {
    'use strict';

    function inventoryLogCtrl($compile, $scope, $location, $timeout, $filter, inService,getMsg, buildTable, DTColumnBuilder, localStorageService) {
        var vm = this;

        vm.data = {
            searchFilterData: {
                keyword: "",
                pageSize: "",
                startColumn: "",
                item_number:"",
                order_number:"",
                order_type:"",
                order: {
                    orderColumn: "",
                    orderRule: ""
                }
            }
        };
        vm.search = function () {
            $scope.searchLoading = true;
            $timeout(function () {
                vm.dataTable.dtInstance.DataTable.draw();
                $scope.searchLoading = false;
            }, 1000)
        };

        vm.renderWith={
          setOrderType:function (cellData) {
            if(cellData!=null){
              if(cellData=="out"){
                return "出库";
              }if(cellData=="in"){
                return "入库";
              }else{
                return "未知";
              }
            }
          },
          //*******************************************************************
          // cellData 单元格数据 type标识需不需要隐藏 full 代表全部数据
          //*******************************************************************
          setOrderNumberClick:function (cellData, type, full, meta) {
            if(cellData!=null){
              if(full.order_type=="in"){
                return 	'<a target="_blank" href="../po/index.html#/po/poManagement?actionType=orderEdit&order_number='+cellData+'">'+cellData+'</a>';
              }
              if(full.order_type=="out"){
                return 	'<a target="_blank" href="../po/index.html#/po/poManagement?actionType=orderDetail&order_number='+cellData+'">'+cellData+'</a>';
              }
            }else{
              return "";
            }
          },
          setItemNumberClick:function (cellData) {
            if(cellData!=null){
              return 	'<a target="_blank" href="../im/index.html#/im/itemEdit?item_number='+cellData+'">'+cellData+'</a>';
            }else{
              return "";
            }
          },
        }

        vm.events = {
            init: function () {
                vm.events.bindGrid();
                vm.events.queryOrderTypeList();
            },
            bindGrid: function () {
                vm.dataTable = buildTable.YMTable(vm.data.searchFilterData, inService, "queryOrderList");
                vm.dataTable.dtOptions
                .withButtons([
                     //{
                  //    extend: 'copy',
                  //    text: 'copy',
                  //    exportOptions: {
                  //        columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
                  //    }
                  //},
                  //{
                  //    extend: 'print',
                  //    text: 'print',
                  //    exportOptions: {
                  //        columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
                  //    }
                  //},
                  //{
                  //    extend: 'excel',
                  //    text: 'excel',
                  //    exportOptions: {
                  //        columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
                  //    }
                  //}
                ])
                .withOption('fnRowCallback', vm.events.rowCallback)
                .withOption('order', [8, 'desc']);
                vm.dataTable.dtColumns = [
                  DTColumnBuilder.newColumn('order_number').renderWith(vm.renderWith.setOrderNumberClick),
                  DTColumnBuilder.newColumn('item_number').renderWith(vm.renderWith.setItemNumberClick),
                  DTColumnBuilder.newColumn('order_type').renderWith(vm.renderWith.setOrderType),
                  DTColumnBuilder.newColumn('in_dtm').renderWith(setDate),
                ];
            },
            rowCallback: function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                $compile(angular.element(nRow).contents())($scope);
                return nRow;
            },
            queryOrderTypeList: function () {
                inService.queryOrderTypeList().success(function (data) {
                    if (data.messageId == 10000) {
                        vm.data.order_type = data.body;
                    }
                });
            },

        };
        function setDate(cellData, type, full) {
          if(undefined != cellData && cellData.toString().length > 10) {
            return $filter('date')(cellData ? cellData : '', 'yyyy-MM-dd  HH:mm:ss');
          }
          return $filter('date')(cellData ? cellData * 1000 : '', 'yyyy-MM-dd  HH:mm:ss');
        };

        vm.events.init();
    }

    angular.module('central.in')
      .controller('inventoryLogCtrl', [ '$compile', '$scope', '$location', '$timeout', '$filter', 'inService', 'getMsg', 'buildTable', 'DTColumnBuilder','localStorageService', inventoryLogCtrl]);

})(window.angular);

3.angularJs service

/**
 * INSPINIA - Responsive Admin Theme
 *
 */
angular.module('central.in')
  .service('inService', ['appContext', 'common', 'localStorageService', function (appContext, common, localStorageService) {
      this.queryOrderList = function (oData) {
          return common.baseAjax('http://localhost:8080/queryLogList', 'POST', oData);
      };
      this.queryOrderTypeList = function (oData) {
          return common.baseAjax('http://localhost:8080/queryLogList', 'POST', oData);
      };
  }])

4.注意

由于是从文件中截取出相关代码并修改相关变量及方法名,代码中可能存在错误,仅供参考

转载于:https://my.oschina.net/u/2937605/blog/1488736

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值