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 相关文章推荐
海量经典的jQuery插件集合
Jan 12 Javascript
Javascript生成json的函数代码(可以用php的json_decode解码)
Jun 11 Javascript
js实现弹出窗口、页面变成灰色并不可操作的例子分享
May 10 Javascript
node.js中的buffer.Buffer.isEncoding方法使用说明
Dec 14 Javascript
jQuery的context属性用法实例
Dec 27 Javascript
js获取页面及个元素高度、宽度的代码
Apr 26 Javascript
基于gulp合并压缩Seajs模块的方式说明
Jun 14 Javascript
Vue非父子组件通信详解
Jun 12 Javascript
input type=file 选择图片并且实现预览效果的实例
Oct 26 Javascript
vue项目首屏打开速度慢的解决方法
Mar 31 Javascript
JS实现查找数组中对象的属性值是否存在示例
May 24 Javascript
JS如何实现手机端输入验证码效果
May 13 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 查找字符串常用函数介绍
2012/06/07 PHP
探讨:如何使用PhpDocumentor生成文档
2013/06/25 PHP
PHP strip_tags()去除HTML、XML以及PHP的标签介绍
2014/02/18 PHP
php获取文件大小的方法
2014/02/26 PHP
redirect_uri参数错误的解决方法(必看)
2017/02/16 PHP
Laravel 批量更新多条数据的示例
2017/11/27 PHP
javascript编程起步(第七课)
2007/02/27 Javascript
js中arguments的用法(实例讲解)
2013/11/30 Javascript
JS生成随机字符串的多种方法
2014/06/10 Javascript
百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
2016/02/19 Javascript
jQuery.Uploadify插件实现带进度条的批量上传功能
2016/06/08 Javascript
微信小程序 教程之注册页面
2016/10/17 Javascript
Bootstrap表单控件使用方法详解
2017/01/11 Javascript
微信小程序 Nginx环境配置详细介绍
2017/02/14 Javascript
canvas雪花效果核心代码分享
2017/02/19 Javascript
用原生JS实现简单的多选框功能
2017/06/12 Javascript
vue 文件目录结构详解
2017/11/24 Javascript
基于Vue渲染与插件的加载顺序的问题详解
2018/03/05 Javascript
Vue无限滑动周选择日期的组件的示例代码
2018/07/18 Javascript
BootStrap模态框闪退问题实例代码详解
2018/12/10 Javascript
基于vue实现一个禅道主页拖拽效果
2019/05/27 Javascript
Python时间戳与时间字符串互相转换实例代码
2013/11/28 Python
美国购车网站:TrueCar
2016/10/19 全球购物
戛纳奢侈品商店:Jacques Loup法国
2019/11/04 全球购物
生日邀请函范文
2014/01/13 职场文书
初婚初育证明
2014/01/14 职场文书
群众路线教育党课主持词
2014/04/01 职场文书
《充气雨衣》教学反思
2014/04/07 职场文书
社区优秀志愿者先进事迹
2014/05/09 职场文书
竞聘上岗演讲
2014/05/19 职场文书
晚自修旷课检讨书怎么写
2014/11/17 职场文书
先进教师事迹材料
2014/12/16 职场文书
2014年节能工作总结
2014/12/18 职场文书
2015年度党员个人总结
2015/02/14 职场文书
培训通知
2015/04/17 职场文书
2015年副班长工作总结
2015/05/15 职场文书