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 Event学习第五章 高级事件注册模型
Feb 07 Javascript
判断一个对象是否为jquery对象的方法
Mar 12 Javascript
jQuery修改li下的样式以及li下的img的src的值的方法
Nov 02 Javascript
jQuery基于扩展简单实现倒计时功能的方法
May 14 Javascript
AngularJS入门教程之XHR和依赖注入详解
Aug 18 Javascript
用vue2.0实现点击选中active其他选项互斥的效果
Apr 12 Javascript
浅谈Node.js 中间件模式
Jun 12 Javascript
Vue组件之单向数据流的解决方法
Nov 10 Javascript
详解无限滚动插件vue-infinite-scroll源码解析
May 12 Javascript
node实现简单的增删改查接口实例代码
Aug 22 Javascript
layui-tree实现Ajax异步请求后动态添加节点的方法
Sep 23 Javascript
vue+swiper实现左右滑动的测试题功能
Oct 30 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
《OVERLORD》手游英文版即将上线 手机上也能扮演骨王
2020/04/09 日漫
php简单浏览目录内容的实现代码
2013/06/07 PHP
php获取目录所有文件并将结果保存到数组(实例)
2013/10/25 PHP
php无限遍历目录示例
2014/02/21 PHP
修改destoon会员公司的伪静态中的com目录的方法
2014/08/21 PHP
基于GD2图形库的PHP生成图片缩略图类代码分享
2015/02/08 PHP
laravel5.5安装jwt-auth 生成token令牌的示例
2019/10/24 PHP
七种PHP开发环境搭建工具
2020/06/28 PHP
解决jquery .ajax 在IE下卡死问题的解决方法
2009/10/26 Javascript
JQuery 文本框使用小结
2010/05/22 Javascript
js window.onload 加载多个函数和追加函数详解
2014/01/08 Javascript
引用外部脚本时script标签关闭的写法
2014/01/20 Javascript
javascript + jquery实现定时修改文章标题
2014/03/19 Javascript
JavaScript打印网页指定区域的例子
2014/05/03 Javascript
javascript实现简单的进度条
2015/07/02 Javascript
JS+HTML5 FileReader对象用法示例
2017/04/07 Javascript
jquery实现一个全局计时器(商城可用)
2017/06/30 jQuery
vue 中使用 watch 出现了如下的报错的原因分析
2019/05/21 Javascript
jQuery层叠选择器用法实例分析
2019/06/28 jQuery
JavaScript onclick事件使用方法详解
2020/05/15 Javascript
[44:26]DOTA2上海特级锦标赛主赛事日 - 2 胜者组第一轮#4EG VS Fnatic第二局
2016/03/03 DOTA
python读取json文件并将数据插入到mongodb的方法
2015/03/23 Python
python django 增删改查操作 数据库Mysql
2017/07/27 Python
详谈python3中用for循环删除列表中元素的坑
2018/04/19 Python
在django中实现页面倒数几秒后自动跳转的例子
2019/08/16 Python
python 调试冷知识(小结)
2019/11/11 Python
python将unicode和str互相转化的实现
2020/05/11 Python
python批量处理多DNS多域名的nslookup解析实现
2020/06/28 Python
Python3读写ini配置文件的示例
2020/11/06 Python
工作交流会欢迎词
2014/01/12 职场文书
二手房购房意向书范本
2014/04/01 职场文书
安全先进班组材料
2014/12/26 职场文书
2015年办公室人员工作总结
2015/05/15 职场文书
诚信考试主题班会
2015/08/17 职场文书
教师学习心得体会范文
2016/01/21 职场文书
用python自动生成日历
2021/04/24 Python