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 相关文章推荐
中国地区三级联动下拉菜单效果分析
Nov 15 Javascript
jQuery实现的向下图文信息滚动效果
May 03 Javascript
json实现添加、遍历与删除属性的方法
Jun 17 Javascript
js 基础篇必看(点击事件轮播图的简单实现)
Aug 20 Javascript
Javascript中判断一个值是否为undefined的方法详解
Sep 28 Javascript
AngularJS实现与Java Web服务器交互操作示例【附demo源码下载】
Nov 02 Javascript
浅析上传头像示例及其注意事项
Dec 14 Javascript
JavaScript三种绑定事件方式及相互之间的区别分析
Jan 10 Javascript
详解原生js实现offset方法
Jun 15 Javascript
Js利用console计算代码运行时间的方法示例
Sep 24 Javascript
vue-router2.0 组件之间传参及获取动态参数的方法
Nov 10 Javascript
layui表格 返回的数据状态异常的解决方法
Sep 10 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代码
2010/08/08 PHP
thinkPHP商城公告功能开发问题分析
2016/12/01 PHP
PHP 7.4 新语法之箭头函数实例详解
2019/05/09 PHP
JavaScript DOM 学习第七章 表单的扩展
2010/02/19 Javascript
jquery.boxy插件的iframe扩展代码
2010/07/02 Javascript
js变换显示图片的实例
2013/04/16 Javascript
JQuery中使用Ajax赋值给全局变量异常的解决方法
2014/01/10 Javascript
jquery实现的导航固定效果
2014/04/28 Javascript
简单谈谈javascript代码复用模式
2015/01/28 Javascript
详解javascript传统方法实现异步校验
2016/01/22 Javascript
基于JavaScript实现回到页面顶部动画代码
2016/05/24 Javascript
vue.js将unix时间戳转换为自定义时间格式
2017/01/03 Javascript
浅谈jquery拼接字符串效率比较高的方法
2017/02/22 Javascript
Vue2.x中的Render函数详解
2017/05/30 Javascript
详解windows下vue-cli及webpack 构建网站(二)导入bootstrap样式
2017/06/17 Javascript
微信小程序实现选项卡功能
2020/06/19 Javascript
Angular4.x通过路由守卫进行路由重定向实现根据条件跳转到相应的页面(推荐)
2018/05/10 Javascript
微信小程序自定义tabBar组件开发详解
2020/09/24 Javascript
vue中使用element ui的弹窗与echarts之间的问题详解
2019/10/25 Javascript
[01:22]DOTA2神秘商店携大量周边降临完美大师赛
2017/11/07 DOTA
[01:03:33]Alliance vs TNC 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/18 DOTA
wxpython 学习笔记 第一天
2009/02/09 Python
Python(Tornado)模拟登录小米抢手机
2013/11/12 Python
用Python进行TCP网络编程的教程
2015/04/29 Python
在Python中操作文件之truncate()方法的使用教程
2015/05/25 Python
Python多进程池 multiprocessing Pool用法示例
2018/09/07 Python
python利用跳板机ssh远程连接redis的方法
2019/02/19 Python
更新升级python和pip版本后不生效的问题解决
2020/04/17 Python
解决Keras TensorFlow 混编中 trainable=False设置无效问题
2020/06/28 Python
Python 使用Opencv实现目标检测与识别的示例代码
2020/09/08 Python
戴尔美国官网:Dell
2016/08/31 全球购物
俄罗斯达美乐比萨外送服务:Domino’s Pizza
2020/12/18 全球购物
先进工作者获奖感言
2014/02/08 职场文书
给老婆的道歉信
2015/01/20 职场文书
盲山观后感
2015/06/11 职场文书
给学校的建议书400字
2015/09/14 职场文书