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 相关文章推荐
javascript Array.remove() 数组删除
Aug 06 Javascript
不使用XMLHttpRequest实现异步加载 Iframe和script
Oct 29 Javascript
调用HttpHanlder的几种返回方式小结
Dec 20 Javascript
javascript动态添加、修改、删除对象的属性与方法详解
Jan 27 Javascript
Jquery $when done then的用法详解
May 20 Javascript
使用bootstrap typeahead插件实现输入框自动补全之问题及解决办法
Jul 07 Javascript
关于javascript原型的修改与重写(覆盖)差别详解
Aug 31 Javascript
Vue.js实现价格计算器功能
Mar 30 Javascript
jQuery实现checkbox的简单操作
Nov 18 jQuery
jQuery实现的点击图片居中放大缩小功能示例
Jan 16 jQuery
Vue 中 filter 与 computed 的区别与用法解析
Nov 21 Javascript
详解JavaScript中的Object.is()与&quot;===&quot;运算符总结
Jun 17 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
PHP性能优化工具篇Benchmark类调试执行时间
2011/12/06 PHP
ThinkPHP的L方法使用简介
2014/06/18 PHP
详解php几行代码实现CSV格式文件输出
2017/07/01 PHP
PHP实现微信红包金额拆分试玩的算法示例
2018/04/07 PHP
JavaScript XML和string相互转化实现代码
2011/07/04 Javascript
JS实现QQ图片一闪一闪的效果小例子
2013/07/31 Javascript
Javascript控制页面链接在新窗口打开具体方法
2013/08/16 Javascript
js获取checkbox值的方法
2015/01/28 Javascript
moment.js轻松实现获取当前日期是当年的第几周
2015/02/05 Javascript
jQuery实现页面顶部显示的进度条效果完整实例
2015/12/09 Javascript
AngularJS实现DOM元素的显示与隐藏功能
2016/11/22 Javascript
Mac下使用charles遇到的问题以及解决办法
2017/01/10 Javascript
老生常谈javascript中逻辑运算符&amp;&amp;和||的返回值问题
2017/04/13 Javascript
Angular.js项目中使用gulp实现自动化构建以及压缩打包详解
2017/07/19 Javascript
JavaScript使用Math.random()生成简单的验证码
2019/01/21 Javascript
Node.JS发送http请求批量检查文件中的网页地址、服务是否有效可用
2019/11/20 Javascript
vue实现从外部修改组件内部的变量的值
2020/07/30 Javascript
[01:08:17]2018DOTA2亚洲邀请赛3月29日 小组赛B组 EG VS VGJ.T
2018/03/30 DOTA
[01:14:30]TNC vs VG 2019国际邀请赛淘汰赛 胜者组赛BO3 第二场 8.20.mp4
2019/08/22 DOTA
使用Python编写vim插件的简单示例
2015/04/17 Python
python通过函数属性实现全局变量的方法
2015/05/16 Python
设计模式中的原型模式在Python程序中的应用示例
2016/03/02 Python
Python读取一个目录下所有目录和文件的方法
2016/07/15 Python
django项目中使用手机号登录的实例代码
2019/08/15 Python
numpy 返回函数的上三角矩阵实例
2019/11/25 Python
查看已安装tensorflow版本的方法示例
2020/04/19 Python
解决python脚本中error: unrecognized arguments: True错误
2020/04/20 Python
Django ForeignKey与数据库的FOREIGN KEY约束详解
2020/05/20 Python
keras 多gpu并行运行案例
2020/06/10 Python
详解python with 上下文管理器
2020/09/02 Python
纯CSS3实现地球自转实现代码(图文教程附送源码)
2012/12/26 HTML / CSS
一道SQL存储过程面试题
2016/10/07 面试题
大四自我鉴定
2014/02/08 职场文书
毕业实习自我鉴定范文2014
2014/09/26 职场文书
实习单位鉴定意见
2015/06/04 职场文书
2016关于预防职务犯罪的心得体会
2016/01/21 职场文书