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 相关文章推荐
延时重复执行函数 lLoopRun.js
May 08 Javascript
javascript 手动给表增加数据的小例子
Jul 10 Javascript
返回页面顶部top按钮通过锚点实现(自写)
Aug 30 Javascript
js导入导出excel(实例代码)
Nov 25 Javascript
js实现透明度渐变效果的方法
Apr 10 Javascript
jQuery实现的简单折叠菜单(折叠面板)效果代码
Sep 16 Javascript
教你如何在Node.js中使用jQuery
Aug 28 Javascript
jQuery的三种bind/One/Live/On事件绑定使用方法
Feb 23 Javascript
js实现城市级联菜单的2种方法
Jun 23 Javascript
使用mock.js随机数据和使用express输出json接口的实现方法
Jan 07 Javascript
详解vue-cli+es6引入es5写的js(两种方法)
Apr 19 Javascript
js 图片懒加载的实现
Oct 21 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
别人整理的服务器变量:$_SERVER
2006/10/20 PHP
linux下使用crontab实现定时PHP计划任务失败的原因分析
2014/07/05 PHP
PHP中实现接收多个name相同但Value不相同表单数据实例
2015/02/03 PHP
PHP json_encode() 函数详解及中文乱码问题
2015/11/05 PHP
php+ajax注册实时验证功能
2016/07/20 PHP
PHP设计模式之工厂模式与单例模式
2016/09/28 PHP
解决PHP上传非标准格式的图片pjpeg失败的方法
2017/03/12 PHP
php实现基于PDO的预处理示例
2017/03/28 PHP
thinkPHP框架实现生成条形码的方法示例
2018/06/06 PHP
PHP使用redis位图bitMap 实现签到功能
2019/10/08 PHP
js创建元素(节点)示例
2014/01/02 Javascript
javascript文件中引用依赖的js文件的方法
2014/03/17 Javascript
js点击选择文本的方法
2015/02/09 Javascript
js漂浮广告实现代码
2015/08/15 Javascript
JS返回只包含数字类型的数组实例分析
2016/12/16 Javascript
JS实现焦点图轮播效果的方法详解
2016/12/19 Javascript
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#‘的解决方法
2017/06/17 Javascript
JQuery 选择器、DOM节点操作练习实例
2017/09/28 jQuery
webpack下实现动态引入文件方法
2018/02/22 Javascript
JavaScript数据结构与算法之二叉树添加/删除节点操作示例
2019/03/01 Javascript
移动端底部导航固定配合vue-router实现组件切换功能
2019/06/13 Javascript
Node.js+Vue脚手架环境搭建的方法步骤
2020/03/08 Javascript
Python with语句上下文管理器两种实现方法分析
2018/02/09 Python
python生成tensorflow输入输出的图像格式的方法
2018/02/12 Python
python爬虫之模拟登陆csdn的实例代码
2018/05/18 Python
Python常用的json标准库
2019/02/19 Python
python词云库wordCloud使用方法详解(解决中文乱码)
2020/02/17 Python
django实现模板中的字符串文字和自动转义
2020/03/31 Python
Selenium使用Chrome模拟手机浏览器方法解析
2020/04/10 Python
Python实现快速大文件比较代码解析
2020/09/04 Python
python判断变量是否为列表的方法
2020/09/17 Python
支部书记四风对照材料
2014/08/28 职场文书
安全先进个人材料
2014/12/29 职场文书
导师工作推荐信
2015/03/27 职场文书
2015年财务部工作总结
2015/04/10 职场文书
总结几个非常实用的Python库
2021/06/26 Python