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 得到文件后缀名的思路及实现
May 09 Javascript
javascript的变量、传值、传址、参数之间关系
Jul 26 Javascript
微信企业号开发之微信考勤Cookies的使用
Sep 11 Javascript
WEB前端开发都应知道的jquery小技巧及jquery三个简写
Nov 15 Javascript
JS基于clipBoard.js插件实现剪切、复制、粘贴
May 03 Javascript
connection reset by peer问题总结及解决方案
Oct 21 Javascript
jquery拖动改变div大小
Jul 04 jQuery
JavaScript数组去重的多种方法(四种)
Sep 19 Javascript
js数组去重的N种方法(小结)
Jun 07 Javascript
js实现同一个页面,多个enter事件绑定的示例
Oct 10 Javascript
JavaScript 实现自己的安卓手机自动化工具脚本(推荐)
May 13 Javascript
bootstrap-closable-tab可实现关闭的tab标签页插件
Aug 09 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格式化工具Beautify PHP小小BUG
2008/04/24 PHP
解析coreseek for sphinx的使用
2013/06/21 PHP
swoole和websocket简单聊天室开发
2017/11/18 PHP
Sample script that displays all of the users in a given SQL Server DB
2007/06/16 Javascript
用户注册常用javascript代码
2009/08/29 Javascript
javascript实现的距离现在多长时间后的一个格式化的日期
2009/10/29 Javascript
深入理解React中es6创建组件this的方法
2016/08/29 Javascript
JQuery页面随滚动条动态加载效果的简单实现(推荐)
2017/02/08 Javascript
Vue 按键修饰符处理事件的方法
2018/05/04 Javascript
详解Vue.js和layui日期控件冲突问题解决办法
2019/07/25 Javascript
vue使用原生swiper代码实例
2020/02/05 Javascript
[56:14]Fnatic vs OG 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
[52:26]完美世界DOTA2联赛决赛 FTD vs Phoenix 第一场 11.08
2020/11/11 DOTA
python使用rabbitmq实现网络爬虫示例
2014/02/20 Python
Python彩色化Linux的命令行终端界面的代码实例分享
2016/07/02 Python
详解使用python的logging模块在stdout输出的两种方法
2017/05/17 Python
Django数据库操作的实例(增删改查)
2017/09/04 Python
Python 函数基础知识汇总
2018/03/09 Python
python读取中文txt文本的方法
2018/04/12 Python
Python实现的微信好友数据分析功能示例
2018/06/21 Python
在Pycharm中执行scrapy命令的方法
2019/01/16 Python
python django下载大的csv文件实现方法分析
2019/07/19 Python
Pytorch实现GoogLeNet的方法
2019/08/18 Python
最新版 Windows10上安装Python 3.8.5的步骤详解
2020/11/28 Python
python selenium 获取接口数据的实现
2020/12/07 Python
For Art’s Sake官网:手工制作的奢华眼镜
2018/12/15 全球购物
手机被没收检讨书
2014/02/22 职场文书
梅花魂教学反思
2014/04/25 职场文书
中秋节国旗下演讲稿
2014/09/13 职场文书
合伙开公司协议书范本
2014/10/28 职场文书
2014大学生学生会工作总结
2014/12/19 职场文书
交警失职检讨书
2015/01/26 职场文书
幼儿园小班开学寄语
2015/05/27 职场文书
社区安全温馨提示语
2015/07/14 职场文书
幼儿园六一儿童节开幕词
2016/03/04 职场文书
python 如何将两个实数矩阵合并为一个复数矩阵
2021/05/19 Python