使用Jquery Datatable和AngularJs

我想在我的angularjs项目中使用jQuery数据表插件 。 但我的问题是它是否支持angularjs的延迟加载的价值? 我想因为我有很多行。 如何使用angularjs使用数据表pipe道 。

这里有一个分页的解决scheme。 如何使用angularjs解决scheme?

看看这个:AngularJS + JQuery(数据表)

完整代码: http : //jsfiddle.net/zdam/7kLFU/

JQuery数据表的文档: http ://www.datatables.net/

var dialogApp = angular.module('tableExample', []); dialogApp.directive('myTable', function() { return function(scope, element, attrs) { // apply DataTable options, use defaults if none specified by user var options = {}; if (attrs.myTable.length > 0) { options = scope.$eval(attrs.myTable); } else { options = { "bStateSave": true, "iCookieDuration": 2419200, /* 1 month */ "bJQueryUI": true, "bPaginate": false, "bLengthChange": false, "bFilter": false, "bInfo": false, "bDestroy": true }; } // Tell the dataTables plugin what columns to use // We can either derive them from the dom, or use setup from the controller var explicitColumns = []; element.find('th').each(function(index, elem) { explicitColumns.push($(elem).text()); }); if (explicitColumns.length > 0) { options["aoColumns"] = explicitColumns; } else if (attrs.aoColumns) { options["aoColumns"] = scope.$eval(attrs.aoColumns); } // aoColumnDefs is dataTables way of providing fine control over column config if (attrs.aoColumnDefs) { options["aoColumnDefs"] = scope.$eval(attrs.aoColumnDefs); } if (attrs.fnRowCallback) { options["fnRowCallback"] = scope.$eval(attrs.fnRowCallback); } // apply the plugin var dataTable = element.dataTable(options); // watch for any changes to our data, rebuild the DataTable scope.$watch(attrs.aaData, function(value) { var val = value || null; if (val) { dataTable.fnClearTable(); dataTable.fnAddData(scope.$eval(attrs.aaData)); } }); }; }); function Ctrl($scope) { $scope.message = ''; $scope.myCallback = function(nRow, aData, iDisplayIndex, iDisplayIndexFull) { $('td:eq(2)', nRow).bind('click', function() { $scope.$apply(function() { $scope.someClickHandler(aData); }); }); return nRow; }; $scope.someClickHandler = function(info) { $scope.message = 'clicked: '+ info.price; }; $scope.columnDefs = [ { "mDataProp": "category", "aTargets":[0]}, { "mDataProp": "name", "aTargets":[1] }, { "mDataProp": "price", "aTargets":[2] } ]; $scope.overrideOptions = { "bStateSave": true, "iCookieDuration": 2419200, /* 1 month */ "bJQueryUI": true, "bPaginate": true, "bLengthChange": false, "bFilter": true, "bInfo": true, "bDestroy": true }; $scope.sampleProductCategories = [ { "name": "1948 Porsche 356-A Roadster", "price": 53.9, "category": "Classic Cars", "action":"x" }, { "name": "1948 Porsche Type 356 Roadster", "price": 62.16, "category": "Classic Cars", "action":"x" }, { "name": "1949 Jaguar XK 120", "price": 47.25, "category": "Classic Cars", "action":"x" } , { "name": "1936 Harley Davidson El Knucklehead", "price": 24.23, "category": "Motorcycles", "action":"x" }, { "name": "1957 Vespa GS150", "price": 32.95, "category": "Motorcycles", "action":"x" }, { "name": "1960 BSA Gold Star DBD34", "price": 37.32, "category": "Motorcycles", "action":"x" } , { "name": "1900s Vintage Bi-Plane", "price": 34.25, "category": "Planes", "action":"x" }, { "name": "1900s Vintage Tri-Plane", "price": 36.23, "category": "Planes", "action":"x" }, { "name": "1928 British Royal Navy Airplane", "price": 66.74, "category": "Planes", "action":"x" }, { "name": "1980s Black Hawk Helicopter", "price": 77.27, "category": "Planes", "action":"x" }, { "name": "ATA: B757-300", "price": 59.33, "category": "Planes", "action":"x" } ]; } 

经过几个小时对Angular使用jQueryDataTables的实验后,我发现我需要的是一个名为ng-table的本地Angular指令。 它提供了sorting,分页和阿贾克斯重新加载(一些懒惰加载能力和一些调整)。

http://ng-table.com/

看看: http : //l-lin.github.io/angular-datatables/它已经准备好使用。

添加一个新的答案只是作为未来研究人员的参考,没有人提到,但我认为这是有效的。

另一个不错的select是ng-grid http://angular-ui.github.io/ng-grid/

还有一个testing版本( http://ui-grid.info/ )已经有了一些改进:

  • 原生的AngularJS实现,没有jQuery
  • 与大型数据集一起运行良好; 甚至超过10,000行
  • 插件架构允许您只使用您需要的function

更新:

看来UI GRID不再是testing版了。

在3.0版本中,版本库已经从“ng-grid”重命名为“ui-grid”。

对于AngularJs,你必须使用“angular-datatables.min.js”文件进行数据表设置。 你会从http://l-lin.github.io/angular-datatables/#/welcome得到这个。;

之后,你可以写下面的代码,

 <script> var app = angular.module('AngularWayApp', ['datatables']); </script> <div ng-app="AngularWayApp" ng-controller="AngularWayCtrl"> <table id="example" datatable="ng" class="table"> <thead> <tr> <th><b>UserID</b></th> <th><b>Firstname</b></th> <th><b>Lastname</b></th> <th><b>Email</b></th> <th><b>Actions</b></th> </tr> </thead> <tbody> <tr ng-repeat="user in users" ng-click="testingClick(user)"> <td> {{user.UserId}} </td> <td> {{user.FirstName}} </td> <td> {{user.Lastname}} </td> <td> {{user.Email}} </td> <td> <span ng-click="editUser(user)" style="color:blue;cursor: pointer; font-weight:500; font-size:15px" class="btnAdd" data-toggle="modal" data-target="#myModal">Edit</span> &nbsp;&nbsp; | &nbsp;&nbsp; <span ng-click="deleteUser(user)" style="color:red; cursor: pointer; font-weight:500; font-size:15px" class="btnRed">Delete</span> </td> </tr> </tbody> </table> </div> 

我知道使用由其他开发人员创build的拖放angular模块是很诱人的 – 但实际上,除非你正在做一些非标准的事情,比如通过调用$http services来dynamic地添加/删除重复的数据集中的行,不需要一个基于指令的解决scheme,所以如果你确实朝这个方向发展,你可能只是创build了一些你并不需要的额外的观察者。

这个实现提供了什么:

  • 分页总是正确的
  • 过滤总是正确的(即使您添加自定义filter,但当然,他们只需要在相同的封闭)

实施很简单。 只需使用你的视图的控制器的angular度的版本的jQuery DOM就绪:

在你的控制器里面

 'use strict'; var yourApp = angular.module('yourApp.yourController.controller', []); yourApp.controller('yourController', ['$scope', '$http', '$q', '$timeout', function ($scope, $http, $q, $timeout) { $scope.users = [ { email: 'email@address.com', name: { first: 'User', last: 'Last Name' }, phone: '(416) 555-5555', permissions: 'Admin' }, { email: 'example@email.com', name: { first: 'First', last: 'Last' }, phone: '(514) 222-1111', permissions: 'User' } ]; angular.element(document).ready( function () { dTable = $('#user_table') dTable.DataTable(); }); }]); 

现在在你的html视图中可以这样做:

 <div class="table table-data clear-both" data-ng-show="viewState === possibleStates[0]"> <table id="user_table" class="users list dtable"> <thead> <tr> <th>E-mail</th> <th>First Name</th> <th>Last Name</th> <th>Phone</th> <th>Permissions</th> <th class="blank-cell"></th> </tr> </thead> <tbody> <tr data-ng-repeat="user in users track by $index"> <td>{{ user.email }}</td> <td>{{ user.name.first }}</td> <td>{{ user.name.last }}</td> <td>{{ user.phone }}</td> <td>{{ user.permissions }}</td> <td class="users controls blank-cell"> <a class="btn pointer" data-ng-click="showEditUser( $index )">Edit</a> </td> </tr> </tbody> </table> </div> 

请访问此链接以供参考: http : //codepen.io/kalaiselvan/pen/RRBzda

 <script> var app=angular.module('formvalid', ['ui.bootstrap','ui.utils']); app.controller('validationCtrl',function($scope){ $scope.data=[ [ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011\/04\/25", "$320,800" ], [ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011\/07\/25", "$170,750" ], [ "Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009\/01\/12", "$86,000" ], [ "Cedric Kelly", "Senior Javascript Developer", "Edinburgh", "6224", "2012\/03\/29", "$433,060" ], [ "Airi Satou", "Accountant", "Tokyo", "5407", "2008\/11\/28", "$162,700" ], [ "Brielle Williamson", "Integration Specialist", "New York", "4804", "2012\/12\/02", "$372,000" ], [ "Herrod Chandler", "Sales Assistant", "San Francisco", "9608", "2012\/08\/06", "$137,500" ], [ "Rhona Davidson", "Integration Specialist", "Tokyo", "6200", "2010\/10\/14", "$327,900" ], [ "Colleen Hurst", "Javascript Developer", "San Francisco", "2360", "2009\/09\/15", "$205,500" ], [ "Sonya Frost", "Software Engineer", "Edinburgh", "1667", "2008\/12\/13", "$103,600" ], [ "Jena Gaines", "Office Manager", "London", "3814", "2008\/12\/19", "$90,560" ], [ "Quinn Flynn", "Support Lead", "Edinburgh", "9497", "2013\/03\/03", "$342,000" ], [ "Charde Marshall", "Regional Director", "San Francisco", "6741", "2008\/10\/16", "$470,600" ], [ "Haley Kennedy", "Senior Marketing Designer", "London", "3597", "2012\/12\/18", "$313,500" ], [ "Tatyana Fitzpatrick", "Regional Director", "London", "1965", "2010\/03\/17", "$385,750" ], [ "Michael Silva", "Marketing Designer", "London", "1581", "2012\/11\/27", "$198,500" ], [ "Paul Byrd", "Chief Financial Officer (CFO)", "New York", "3059", "2010\/06\/09", "$725,000" ], [ "Gloria Little", "Systems Administrator", "New York", "1721", "2009\/04\/10", "$237,500" ], [ "Bradley Greer", "Software Engineer", "London", "2558", "2012\/10\/13", "$132,000" ], [ "Dai Rios", "Personnel Lead", "Edinburgh", "2290", "2012\/09\/26", "$217,500" ], [ "Jenette Caldwell", "Development Lead", "New York", "1937", "2011\/09\/03", "$345,000" ], [ "Yuri Berry", "Chief Marketing Officer (CMO)", "New York", "6154", "2009\/06\/25", "$675,000" ], [ "Caesar Vance", "Pre-Sales Support", "New York", "8330", "2011\/12\/12", "$106,450" ], [ "Doris Wilder", "Sales Assistant", "Sidney", "3023", "2010\/09\/20", "$85,600" ], [ "Angelica Ramos", "Chief Executive Officer (CEO)", "London", "5797", "2009\/10\/09", "$1,200,000" ], [ "Gavin Joyce", "Developer", "Edinburgh", "8822", "2010\/12\/22", "$92,575" ], [ "Jennifer Chang", "Regional Director", "Singapore", "9239", "2010\/11\/14", "$357,650" ], [ "Brenden Wagner", "Software Engineer", "San Francisco", "1314", "2011\/06\/07", "$206,850" ], [ "Fiona Green", "Chief Operating Officer (COO)", "San Francisco", "2947", "2010\/03\/11", "$850,000" ], [ "Shou Itou", "Regional Marketing", "Tokyo", "8899", "2011\/08\/14", "$163,000" ], [ "Michelle House", "Integration Specialist", "Sidney", "2769", "2011\/06\/02", "$95,400" ], [ "Suki Burks", "Developer", "London", "6832", "2009\/10\/22", "$114,500" ], [ "Prescott Bartlett", "Technical Author", "London", "3606", "2011\/05\/07", "$145,000" ], [ "Gavin Cortez", "Team Leader", "San Francisco", "2860", "2008\/10\/26", "$235,500" ], [ "Martena Mccray", "Post-Sales support", "Edinburgh", "8240", "2011\/03\/09", "$324,050" ], [ "Unity Butler", "Marketing Designer", "San Francisco", "5384", "2009\/12\/09", "$85,675" ], [ "Howard Hatfield", "Office Manager", "San Francisco", "7031", "2008\/12\/16", "$164,500" ], [ "Hope Fuentes", "Secretary", "San Francisco", "6318", "2010\/02\/12", "$109,850" ], [ "Vivian Harrell", "Financial Controller", "San Francisco", "9422", "2009\/02\/14", "$452,500" ], [ "Timothy Mooney", "Office Manager", "London", "7580", "2008\/12\/11", "$136,200" ], [ "Jackson Bradshaw", "Director", "New York", "1042", "2008\/09\/26", "$645,750" ], [ "Olivia Liang", "Support Engineer", "Singapore", "2120", "2011\/02\/03", "$234,500" ], [ "Bruno Nash", "Software Engineer", "London", "6222", "2011\/05\/03", "$163,500" ], [ "Sakura Yamamoto", "Support Engineer", "Tokyo", "9383", "2009\/08\/19", "$139,575" ], [ "Thor Walton", "Developer", "New York", "8327", "2013\/08\/11", "$98,540" ], [ "Finn Camacho", "Support Engineer", "San Francisco", "2927", "2009\/07\/07", "$87,500" ], [ "Serge Baldwin", "Data Coordinator", "Singapore", "8352", "2012\/04\/09", "$138,575" ], [ "Zenaida Frank", "Software Engineer", "New York", "7439", "2010\/01\/04", "$125,250" ], [ "Zorita Serrano", "Software Engineer", "San Francisco", "4389", "2012\/06\/01", "$115,000" ], [ "Jennifer Acosta", "Junior Javascript Developer", "Edinburgh", "3431", "2013\/02\/01", "$75,650" ], [ "Cara Stevens", "Sales Assistant", "New York", "3990", "2011\/12\/06", "$145,600" ], [ "Hermione Butler", "Regional Director", "London", "1016", "2011\/03\/21", "$356,250" ], [ "Lael Greer", "Systems Administrator", "London", "6733", "2009\/02\/27", "$103,500" ], [ "Jonas Alexander", "Developer", "San Francisco", "8196", "2010\/07\/14", "$86,500" ], [ "Shad Decker", "Regional Director", "Edinburgh", "6373", "2008\/11\/13", "$183,000" ], [ "Michael Bruce", "Javascript Developer", "Singapore", "5384", "2011\/06\/27", "$183,000" ], [ "Donna Snider", "Customer Support", "New York", "4226", "2011\/01\/25", "$112,000" ] ] $scope.dataTableOpt = { //if any ajax call }; }); </script> <div class="container" ng-app="formvalid"> <div class="panel" data-ng-controller="validationCtrl"> <div class="panel-heading border"> <h2>Data table using jquery datatable in Angularjs </h2> </div> <div class="panel-body"> <table class="table table-bordered bordered table-striped table-condensed datatable" ui-jq="dataTable" ui-options="dataTableOpt"> <thead> <tr> <th>#</th> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start Date</th> </tr> </thead> <tbody> <tr ng-repeat="n in data"> <td>{{$index+1}}</td> <td>{{n[0]}}</td> <td>{{n[1]}}</td> <td>{{n[2]}}</td> <td>{{n[3]}}</td> <td>{{n[4] | date:'dd/MM/yyyy'}}</td> </tr> </tbody> </table> </div> </div> </div>