【发布时间】:2016-09-07 16:20:16
【问题描述】:
我正在尝试使用 Angular 实现 DataTables,我在 google 上搜索过一些解决方案正在创建指令,它还可以,但非常古老,只有“正常”的方式来绘制 DataTable,问题是排序或在搜索框中输入数据丢失!!例如:
还有我的代码:
查看
var myApp = angular.module('myApp', ['ngRoute','ui.utils']);
myApp.controller("CompanyController", function ($scope, $window, CompanyService) {
$scope.Companies = [];
$scope.Company = {};
$scope.dataTableOpt = {
//custom datatable options
"aLengthMenu": [[10, 50, 100, -1], [10, 50, 100, 'All']],
};
$scope.$watch("data", function (value) {
console.log("Data changed, refresh table:");
var val = value || null;
if (val) {
}
});
$scope.InitializeIndexView = function () {
var getAllProcess = CompanyService.GetAllCompanies();
getAllProcess.then(function (response) {
//console.log(response.data)
$scope.Companies = response.data;
},
function (response) {
console.log(response);
})
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<table id="company-table" class="table table-striped table-bordered" ui-jq="DataTable" ui-options="dataTableOpt">
<thead>
<tr>
<th>Id</th>
<th>Register time</th>
<th>Short Name</th>
<th>Long Name</th>
<th>Status</th>
<th>Owner Client</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in Companies">
<td>{{item._id}}</td>
<td>{{item.RegisterTime}}</td>
<td>{{item.LongName}}</td>
<td>{{item.ShortName}}</td>
<td>{{item.CompanyStatus}}</td>
<td>{{item.OwnerClient}}</td>
<td><a href="@Url.Action("Edit","Company")&CompanyId={{item._id}}">Edit</a> | <a href="@Url.Action("Delete","Company")&CompanyId={{item._id}}">Delete</a></td>
</tr>
</tbody>
</table>
编辑 1: 我遵循这些 sn-p 并且工作正常,因为数据是静态的:http://codepen.io/kalaiselvan/pen/RRBzda
【问题讨论】:
-
嗯,我觉得ng-table比较好用,它有你正在尝试实现的排序和搜索功能
标签: angularjs angularjs-directive datatables