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 相关文章推荐
JS文本框追加多个下拉框的值的简单实例
Jul 12 Javascript
jquery实现按Enter键触发事件示例
Sep 10 Javascript
js跨浏览器实现将字符串转化为xml对象的方法
Sep 25 Javascript
JS之Date对象和获取系统当前时间详解
Jan 13 Javascript
js关于命名空间的函数实例
Feb 05 Javascript
Flash图片上传组件 swfupload使用指南
Mar 14 Javascript
JS验证逗号隔开可以是中文字母数字
Apr 22 Javascript
在JavaScript中调用Java类和接口的方法
Sep 07 Javascript
vue iview实现动态路由和权限验证功能
Apr 17 Javascript
JS面向对象的程序设计相关知识小结
May 26 Javascript
vue动态设置img的src路径实例
Sep 18 Javascript
Vue-CLI项目中路由传参的方式详解
Sep 01 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
天使彦史上最神还原,性别曝光的那一刻,百万网友恋爱了
2020/03/02 国漫
追求程序速度,而不是编程的速度
2008/04/23 PHP
php学习笔记 [预定义数组(超全局数组)]
2011/06/09 PHP
header跳转和include包含问题详解
2012/09/08 PHP
模拟电子签章盖章效果的jQuery插件源码
2013/06/24 Javascript
js改变文章字体大小的实例代码
2013/11/27 Javascript
checkbox全选所涉及到的知识点介绍
2013/12/31 Javascript
详解JavaScript ES6中的Generator
2015/07/28 Javascript
AngularJS ng-mousedown 指令
2016/08/02 Javascript
AngularJS 执行流程详细介绍
2016/08/18 Javascript
使用JQuery中的trim()方法去掉前后空格
2016/09/16 Javascript
JS新包管理工具yarn和npm的对比与使用入门
2016/12/09 Javascript
最常见和最有用的字符串相关的方法详解
2017/02/06 Javascript
移动端利用H5实现压缩图片上传功能
2017/03/29 Javascript
微信小程序出现wx.navigateTo页面不跳转问题的解决方法
2017/12/26 Javascript
在vue中使用公共过滤器filter的方法
2018/06/26 Javascript
WebGL three.js学习笔记之阴影与实现物体的动画效果
2019/04/25 Javascript
Vue项目中使用jquery的简单方法
2019/05/16 jQuery
jQuery 图片查看器插件 Viewer.js用法简单示例
2020/04/04 jQuery
js实现简易ATM功能
2020/10/27 Javascript
[47:36]Optic vs Newbee 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
python基础教程之udp端口扫描
2014/02/10 Python
Python中使用动态变量名的方法
2014/05/06 Python
java中的控制结构(if,循环)详解
2019/06/26 Python
numpy.linalg.eig() 计算矩阵特征向量方式
2019/11/29 Python
解决pycharm不能自动保存在远程linux中的问题
2021/02/06 Python
纪伊国屋泰国网上书店:Kinokuniya泰国
2017/12/24 全球购物
澳大利亚现代波西米亚风格女装网站:Bohemian Traders
2018/04/16 全球购物
高校教师思想汇报
2014/01/11 职场文书
小学生竞选班长演讲稿
2014/04/24 职场文书
2014年小学教师工作自我评价
2014/09/22 职场文书
乡镇群众路线教育实践活动整改措施
2014/10/04 职场文书
评先进个人材料
2014/12/29 职场文书
教师个人发展总结
2015/02/11 职场文书
《我的长生果》教学反思
2016/02/20 职场文书
如何理解及使用Python闭包
2021/06/01 Python