angularJs中datatable實(shí)現(xiàn)代碼
本文介紹了angularJs中datatable實(shí)現(xiàn),有需要的小伙伴可以參考下
html引用derective:
<table datatable dtOptions="dtOptionsExample2" class="table table-striped m-b-none"></table>
controller設(shè)置:
$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);
}
}
});
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- angularjs表格ng-table使用備忘錄
- AngularJS ng-table插件設(shè)置排序
- Angularjs+bootstrap+table多選(全選)支持單擊行選中實(shí)現(xiàn)編輯、刪除功能
- AngularJS中table表格基本操作示例
- angularjs實(shí)現(xiàn)table增加tr的方法
- 詳解angularjs popup-table 彈出框表格指令
- 對(duì)比分析Django的Q查詢及AngularJS的Datatables分頁(yè)插件
- angularjs實(shí)現(xiàn)table表格td單元格單擊變輸入框/可編輯狀態(tài)示例
相關(guān)文章
微信小程序?qū)崿F(xiàn)左右聯(lián)動(dòng)的實(shí)戰(zhàn)記錄
聯(lián)動(dòng)菜單是大家在開發(fā)小程序經(jīng)常會(huì)遇到的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于微信小程序?qū)崿F(xiàn)左右聯(lián)動(dòng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
初學(xué)者AngularJS的環(huán)境搭建過(guò)程
這篇文章主要介紹了初學(xué)者AngularJS的環(huán)境搭建過(guò)程,在文章給大家提到了Angular-cli的特性,大家一起看看吧2017-10-10
解決angular2 獲取到的數(shù)據(jù)無(wú)法實(shí)時(shí)更新的問(wèn)題
今天小編就為大家分享一篇解決angular2 獲取到的數(shù)據(jù)無(wú)法實(shí)時(shí)更新的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
使用AngularJS編寫多選按鈕選中時(shí)觸發(fā)指定方法的指令代碼詳解
最近做項(xiàng)目時(shí)遇到了需要用到多選按鈕選中觸發(fā)事件的功能,小編試著手寫一個(gè)指令,具體實(shí)現(xiàn)代碼大家參考下本文吧2017-07-07
Angular實(shí)現(xiàn)類似博客評(píng)論的遞歸顯示及獲取回復(fù)評(píng)論的數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于Angular如何實(shí)現(xiàn)類似博客評(píng)論的遞歸顯示及獲取回復(fù)評(píng)論的數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
SpringMVC簡(jiǎn)單整合Angular2的示例
這篇文章主要介紹了SpringMVC簡(jiǎn)單整合Angular2的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
AngularJs 終極購(gòu)物車(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇AngularJs 終極購(gòu)物車的實(shí)例講解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
詳解AngularJS ui-sref的簡(jiǎn)單使用
本篇文章主要介紹了詳解AngularJS ui-sref的簡(jiǎn)單使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04

