ng-table 简单实例

时间:2023-12-11 11:14:08

今天用的AngularJs需要做个分页,于是用ng-table去实现,不过这个官网感觉有点坑,说的不够清楚。

下面实现了一个Demo实力,代码如下:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="../../resources/ng-table/ng-table.min.css">
<link rel="stylesheet" href="../../resources/ng-table/bootstrap.min.css">
<script src="../../resources/ng-table/lodash.min.js"></script>
<script src="../../resources/ng-table/jquery-2.2.4.js"></script>
<script src="../../resources/ng-table/angular.js"></script>
<script src="../../resources/ng-table/ng-table.js"></script>
<style>
.demoTable td:last-child {
min-width: 90px;
}
/** Editable table
------------------------- */
.editable-table > tbody > tr > td {
padding: 4px
}
.editable-text {
padding-left: 4px;
padding-top: 4px;
padding-bottom: 4px;
display: inline-block;
}
.editable-table tbody > tr > td > .controls {
//width: 100%
}
.editable-input {
padding-left: 3px;
}
.editable-input.input-sm {
height: 30px;
font-size: 14px;
padding-top: 4px;
padding-bottom: 4px;
}
</style>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="demoController as demo">
<h2 class="page-header">Loading data - managed array</h2>
<div class="bs-callout bs-callout-info">
<h4>Overview</h4>
<p>When you have the <em>entire</em> dataset available in-memory you can hand this to <code>NgTableParams</code> to manage the filtering, sorting and paging of that array</p>
</div>
<table ng-table="demo.tableParams" class="table table-condensed table-bordered table-striped">
<tr ng-repeat="row in $data" ng-form="rowForm" demo-tracked-table-row="row">
<td title="'Name'" filter="{name: 'text'}" sortable="'name'" ng-switch="row.isEditing" ng-class="name.$dirty ? 'bg-warning' : ''" ng-form="name" demo-tracked-table-cell>
<span ng-switch-default class="editable-text">{{row.name}}</span>
<div class="controls" ng-class="name.$invalid && name.$dirty ? 'has-error' : ''" ng-switch-when="true">
<input type="text" name="name" ng-model="row.name" class="editable-input form-control input-sm" required />
</div>
</td>
<td title="'Age'" filter="{age: 'number'}" sortable="'age'" ng-switch="row.isEditing" ng-class="age.$dirty ? 'bg-warning' : ''" ng-form="age" demo-tracked-table-cell>
<span ng-switch-default class="editable-text">{{row.age}}</span>
<div class="controls" ng-class="age.$invalid && age.$dirty ? 'has-error' : ''" ng-switch-when="true">
<input type="number" name="age" ng-model="row.age" class="editable-input form-control input-sm" required/>
</div>
</td>
<td title="'Money'" filter="{money: 'number'}" sortable="'money'" ng-switch="row.isEditing" ng-class="money.$dirty ? 'bg-warning' : ''" ng-form="money" demo-tracked-table-cell>
<span ng-switch-default class="editable-text">{{row.money}}</span>
<div class="controls" ng-class="money.$invalid && money.$dirty ? 'has-error' : ''" ng-switch-when="true">
<input type="number" name="money" ng-model="row.money" class="editable-input form-control input-sm" required/>
</div>
</td>
<!--<td data-title="'Name'" filter="{name: 'text'}" sortable="'name'">{{row.name}}</td>-->
<!--<td data-title="'Age'" filter="{age: 'number'}" sortable="'age'">{{row.age}}</td>-->
<!--<td data-title="'Money'" filter="{money: 'number'}" sortable="'money'">{{row.money}}</td>-->
<td>
<button class="btn btn-primary btn-sm" ng-click="demo.save(row, rowForm)" ng-if="row.isEditing" ng-disabled="rowForm.$pristine || rowForm.$invalid"><span class="glyphicon glyphicon-ok"></span></button>
<button class="btn btn-default btn-sm" ng-click="demo.cancel(row, rowForm)" ng-if="row.isEditing"><span class="glyphicon glyphicon-remove"></span></button>
<button class="btn btn-default btn-sm" ng-click="row.isEditing = true" ng-if="!row.isEditing"><span class="glyphicon glyphicon-pencil"></span></button>
<button class="btn btn-danger btn-sm" ng-click="demo.del(row)" ng-if="!row.isEditing"><span class="glyphicon glyphicon-trash"></span></button>
</td>
</tr>
</table>
</div>
</div>
<script>
(function() {
"use strict"; var app = angular.module("myApp", ["ngTable"]); app.controller("demoController", demoController);
// demoController.$inject = ["NgTableParams", "ngTableSimpleList"]; var data = [
{name: "Moroni", age: 16, money: 88.1},
{name: "Enos", age: 99, money: 22.3},
{name: "Tracy", age: 2, money: 22.4},
{name: "Oracle", age: 31, money: 33.3},
{name: "Java", age: 13, money: 53.3},
{name: "Php", age: 46, money: 67.3}
]; function demoController(NgTableParams,$http,$scope) {
var self = this;
self.tableParams = new NgTableParams({}, {
filterDelay: 0,
dataset: data
});
self.cancel = cancel;
self.del = del;
self.save = save;
function cancel(row, rowForm) {
var originalRow = resetRow(row, rowForm);
angular.extend(row, originalRow);
} function del(row) {
_.remove(self.tableParams.settings().dataset, function(item) {
return row === item;
});
self.tableParams.reload().then(function(data) {
if (data.length === 0 && self.tableParams.total() > 0) {
self.tableParams.page(self.tableParams.page() - 1);
self.tableParams.reload();
}
});
} function resetRow(row, rowForm){
row.isEditing = false;
rowForm.$setPristine();
self.tableTracker.untrack(row);
return _.findWhere(originalData, function(r){
return r.id === row.id;
});
} function save(row, rowForm) {
var originalRow = resetRow(row, rowForm);
angular.extend(originalRow, row);
}
}
})();
</script>
</body>
</html>

页面的效果:

ng-table 简单实例

ng-table 简单实例

ng-table="demo.tableParams" 的demo不能去掉,改用官网的第一个例子vm.tableParams也不行的。

这里只是一个简单的测试,等有进一步的了解再记录。