angularJs中datatable实现代码


Posted in Javascript onJune 03, 2017

本文介绍了angularJs中datatable实现,有需要的小伙伴可以参考下

html引用derective:

<table datatable dtOptions="dtOptionsExample2" class="table table-striped m-b-none"></table>

controller设置:

$scope.dtOptions = { 
"bProcessing": true, 
"bServerSide": true, 
iDisplayLength: 5, 
sAjaxSource: 'http://10.188.192.200:8080/employee/page?deptId='+ data, 
sAjaxDataProp: 'aaData', 
"sDom": "<'row'<'col-sm-6'l><'col-sm-6'f>r>t<'row'<'col-sm-6'i><'col-sm-6'p>>", 
sPaginationType: "full_numbers", 
"aoColumns": 
[ 
{ "mData": "employeeId" }, 
{ "mData": "employeeName", 
"sClass": "center", 
"mRender": function(data,type,full) { 
return '<a class="emplyeeInfoLink" href="javascript:;" rel="external nofollow" >阿司法所</a>'; 
} 
}, 
{ "mData": "employeeEmail" }, 
{ "mData": "employeeMobilePhoneMaster" } 
], 
/*"aoColumnDefs":[ 
{ 
"aTargets":[4], 
"mData": null 
} 
],*/ 
"fnServerData": function( sUrl, aoData, fnCallback, oSettings ) { 
oSettings.jqXHR = $.ajax({ 
"url": sUrl, 
beforeSend: function(xhr) { 
xhr.withCredentials = true; 
}, 
"data": aoData, 
"type": 'get', 
"success": fnCallback, 
"cache": false 
}); 
} 
}

angular.datatable.js:

angular.module('datatablesDirectives', []).directive('datatable', function ($http) { 
 return { 
 // I restricted it to A only. I initially wanted to do something like 
 // <datatable> <thead> ... </thead> </datatable> 
 // But thead elements are only valid inside table, and <datatable> is not a table. 
 // So.. no choice to use <table datatable> 
 restrict: 'A', 
 
 link: function ($scope, $elem, attrs) { 
  var options = {}; 
 
  // Start with the defaults. Change this to your defaults. 
  options = {} 
 
  // If dtOptions is defined in the controller, extend our default option. 
  if (typeof $scope.dtOptions !== 'undefined') { 
 
   angular.extend(options, $scope.dtOptions); 
  } 
 
  // If dtoptions is not declared, check the other options 
  if (attrs['dtoptions'] === undefined) { 
 
   // Get the attributes, put it in an options 
   // We need to do a switch/case because attributes does not retain case 
   // and datatables options are case sensitive. Damn. It's okay! We need to detect 
   // the callbacks anyway and call it as functions, so it works out! 
   // I put what I needed, most of my settings are not dynamics except those 2. 
   for (property in attrs) { 
    switch (property) { 
     // This is the ajax source 
     case 'sajaxsource': 
      options['sAjaxSource'] = attrs[property]; 
     break; 
     // This is the ajax data prop. For example, your result might be 
     // {code: 200, data: [ .. ]} -> in the case, sAjaxDataProp is data 
     case 'sajaxdataprop': 
      options['sAjaxDataProp'] = attrs[property]; 
     break; 
    } 
   } 
  } else { 
   // If dtoptions is declare, extend the current options with it. 
 
   angular.extend(options, $scope.dtOptions); 
  }  
   
  // Just some basic validation. 
  if (typeof options['sAjaxSource'] === 'undefined') { 
 
   throw "Ajax Source not defined! Use sajaxsource='/api/v1/blabla'"; 
  } 
   
  // for Angular http inceptors 
  if (typeof options['fnServerData'] === 'undefined') { 
   options['fnServerData'] = function (sSource, aoData, resultCb) { 
    $http.get(sSource, aoData).then(function (result) { 
     resultCb(result.data); 
    }); 
   }; 
  } 
 
  // Get the column options, put it in a aocolumn object. 
  // Obviously, mdata is the only one required. 
  // I personally just needed those 3, if you need other more feel free to add it. 
  // mData also accepts a function; I'm sure there's a more elegant way but for now 
  // it detects if it's a function, and if it is, do it. 
  options.aoColumns = []; 
 
  // Get the thead rows. 
  $elem.find('thead th').each(function() { 
   var colattr = angular.element(this).data(); 
   //console.log(colattr); 
   //console.log('demodeo'); 
   // Detects if it's a function. Must exist in scope. 
   if (colattr.mdata.indexOf("()") > 1) { 
 
    // Simple one-liner that removes the ending () 
    var fn = $scope[colattr.mdata.substring(0, colattr.mdata.length - 2)]; 
 
    // Throw an error if it's not a function. 
    if (typeof fn === 'function') { 
     options.aoColumns.push({ 
     mData: fn, 
     sClass: colattr.sclass, 
     bVisible: colattr.bvisible, 
     mRender: colattr.mrender 
    });  
 
    } else { 
 
     throw "mData function does not exist in $scope."; 
 
    } 
   } else { 
    //console.log('<1?'); 
    options.aoColumns.push({ 
    mData: colattr.mdata, 
    sClass: colattr.sclass, 
    bVisible: colattr.bvisible, 
    mRender: colattr.mrender 
   }); 
 
   } 
  }); 
 
  // Load the datatable! 
  $elem.dataTable(options); 
  //console.log(options); 
 
 } 
 } 
});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
基于prototype的validation.js发布2.3.4新版本,让你彻底脱离表单验证的烦恼
Dec 06 Javascript
12个非常有创意的JavaScript小游戏
Mar 18 Javascript
Jquery 选中表格一列并对表格排序实现原理
Dec 15 Javascript
js获取当前日期代码适用于网页头部
Jun 27 Javascript
jquery $.fn $.fx是什么意思有什么用
Nov 04 Javascript
jQuery的end()方法使用详解
Jul 15 Javascript
jQuery EasyUI Dialog拖不下来如何解决
Sep 28 Javascript
pace.js页面加载进度条插件
Sep 29 Javascript
基于AngularJS实现页面滚动到底自动加载数据的功能
Oct 16 Javascript
JS+CSS3实现超炫的散列画廊特效
Jul 16 Javascript
JS失效 提示HTML1114: (UNICODE 字节顺序标记)的代码页 utf-8 覆盖(META 标记)的冲突的代码页 utf-8
Jun 23 Javascript
在Echarts图中给坐标轴加一个标识线markLine
Jul 20 Javascript
angularJS利用ng-repeat遍历二维数组的实例代码
Jun 03 #Javascript
详解JavaScript调用栈、尾递归和手动优化
Jun 03 #Javascript
详解有关easyUI的拖动操作中droppable,draggable用法例子
Jun 03 #Javascript
利用vueJs实现图片轮播实例代码
Jun 03 #Javascript
angular中使用Socket.io实例代码
Jun 03 #Javascript
jquery请求servlet实现ajax异步请求的示例
Jun 03 #jQuery
深入理解Node中的buffer模块
Jun 03 #Javascript
You might like
header中Content-Disposition的作用与使用方法
2012/06/13 PHP
is_uploaded_file函数引发的不能上传文件问题
2013/10/29 PHP
smarty中常用方法实例总结
2015/08/07 PHP
php设计模式之策略模式应用案例详解
2019/06/17 PHP
JavaScript 放大镜 放大倍率和视窗尺寸
2011/05/09 Javascript
非常漂亮的JS+CSS图片幻灯切换特效
2013/11/20 Javascript
js中window.open打开一个新的页面
2014/08/10 Javascript
javascript检测浏览器的缩放状态实现代码
2014/09/28 Javascript
用jQuery的AJax实现异步访问、异步加载
2016/11/02 Javascript
微信小程序swiper组件用法实例分析【附源码下载】
2017/12/07 Javascript
vue如何将v-for中的表格导出来
2018/05/07 Javascript
js限制输入框只能输入数字(onkeyup触发)
2018/09/28 Javascript
angularjs http与后台交互的实现示例
2018/12/21 Javascript
小试小程序云开发(小结)
2019/06/06 Javascript
js实现指定时间倒计时效果
2019/08/26 Javascript
Nodejs + Websocket 指定发送及群聊的实现
2020/01/09 NodeJs
Python设计足球联赛赛程表程序的思路与简单实现示例
2016/06/28 Python
django 按时间范围查询数据库实例代码
2018/02/11 Python
Python pymongo模块常用操作分析
2018/09/01 Python
python实现Zabbix-API监控
2018/09/17 Python
对python requests的content和text方法的区别详解
2018/10/11 Python
python用pandas数据加载、存储与文件格式的实例
2018/12/07 Python
Python实现word2Vec model过程解析
2019/12/16 Python
pytorch+lstm实现的pos示例
2020/01/14 Python
python opencv实现直线检测并测出倾斜角度(附源码+注释)
2020/12/31 Python
python自动打开浏览器下载zip并提取内容写入excel
2021/01/04 Python
详解使用CSS3的@media来编写响应式的页面
2017/11/01 HTML / CSS
CSS3改变浏览器滚动条样式
2019/01/04 HTML / CSS
日本最大级玩偶手办购物:あみあみ Amiami
2018/04/23 全球购物
美国在线面料商店:Online Fabric Store
2018/07/26 全球购物
医学院学生求职简历的自我评价
2013/10/24 职场文书
银行门卫岗位职责
2013/12/29 职场文书
大学自主招生推荐信
2014/05/10 职场文书
老人节标语大全
2014/10/08 职场文书
2015年光棍节活动总结
2015/03/24 职场文书
Python如何使用logging为Flask增加logid
2021/03/30 Python