angularJs + datatable使用示例

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

1.html

订单号 商品号 订单类型 订单时间

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 	''+cellData+'';
              }
              if(full.order_type=="out"){
                return 	''+cellData+'';
              }
            }else{
              return "";
            }
          },
          setItemNumberClick:function (cellData) {
            if(cellData!=null){
              return 	''+cellData+'';
            }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

你可能感兴趣的:(angularJs + datatable使用示例)