表格模版
<!-- table.template.html -->
<!-- table.template.html -->
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th ng-repeat="col in columns" ng-style="col.width && {'width': col.width + 'px'}">
{{col.text}}
</th>
<th ng-if="buttons.length > 0">操作</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in data track by $index">
<td ng-repeat="col in columns" ng-click="handleRowClick(item, col, $event)"
ng-style="col.width && {'width': col.width + 'px'}">
<span ng-if="!col.isLink">
{{col.formatter ? col.formatter(item[col.key]) : item[col.key]}}
</span>
<a ng-if="col.isLink" ng-href="{{col.getLink ? col.getLink(item) : (col.linkTemplate)}}"
target="_blank">
{{col.formatter ? col.formatter(item[col.key]) : item[col.key]}}
</a>
</td>
<td ng-if="buttons.length > 0">
<div class="table-actions">
<button ng-repeat="btn in buttons" ng-if="!btn.show || btn.show(item)"
class="btn btn-xs {{btn.btnClass}}" ng-click="executeAction(btn.action, item)"
title="{{btn.text}}">
<i class="fa {{btn.icon}}" ng-if="btn.icon"></i>
<span ng-if="!btn.iconOnly">{{btn.text}}</span>
</button>
</div>
</td>
</tr>
<tr ng-if="!data || data.length === 0">
<td colspan="{{columns.length + (buttons.length > 0 ? 1 : 0)}}" class="text-center">暂无数据</td>
</tr>
</tbody>
</table>
{{totalItems}} -- {{pageSize}} -- {{pageIndex}}
<div class="padding20" ng-if="totalItems > pageSize">
<uib-pagination total-items="totalItems" ng-model="pageIndex" ng-change="pageChanged(pageIndex)" max-size="10"
boundary-link-numbers="true" rotate="false"></uib-pagination>
</div>
</div>
<style>
.table-responsive {
overflow-x: auto;
min-height: 0.01%;
}
.table {
width: 100%;
margin-bottom: 20px;
}
.table th {
background-color: #f9f9f9;
white-space: nowrap;
vertical-align: middle;
}
.btn-group {
white-space: nowrap;
}
.dropdown-toggle .caret {
margin-left: 5px;
}
.text-center {
text-align: center;
color: #999;
padding: 20px !important;
}
</style>
表格封装逻辑
app.directive('smartTable', function () {
return {
restrict: 'E',
scope: {
columns: '=',
data: '=',
onRowClick: '&',
actionButtons: '=',
totalItems: '=',
pageIndex: '=',
pageSize: '=',
onPageChange: '&'
},
templateUrl: '../common/template/table.template.html',
link: function (scope) {
scope.defaultButtons = [
{
text: '查看',
icon: 'fa-eye',
btnClass: 'btn-info',
action: 'view',
show: function(item) { return true; }
},
{
text: '编辑',
icon: 'fa-edit',
btnClass: 'btn-primary',
action: 'edit',
show: function(item) { return true; }
},
{
text: '删除',
icon: 'fa-trash',
btnClass: 'btn-danger',
action: 'delete',
show: function(item) { return true; }
}
];
scope.buttons = angular.copy(scope.defaultButtons);
if (scope.actionButtons && scope.actionButtons.length) {
scope.buttons = scope.actionButtons;
}
scope.executeAction = function(action, item) {
scope.$emit('tableAction', { action: action, item: item });
};
scope.handleRowClick = function(item, col, event) {
if (angular.element(event.target).closest('.table-actions').length) {
return;
}
if (scope.onRowClick) {
scope.onRowClick({item: item, column: col, $event: event});
}
};
scope.pageChanged = function(pageIndex = 1, pageSize = 10) {
if (scope.onPageChange) {
scope.onPageChange({
pageIndex: pageIndex,
pageSize: pageSize
});
}
};
scope.$watch('pageSize', function(newVal) {
if (newVal) {
scope.pageChanged();
}
});
}
};
});
页面使用
<smart-table columns="columns" data="data" on-row-click="onRowClick(item,column,$event)" action-buttons="actionButtons"
total-items="totalItems" page-index="pageIndex" page-size="pageSize"
on-page-change="onPageChange(pageIndex, pageSize)">
</smart-table>
</smart-table>
基本数据定义
$scope.data = [
{ id: 1, name: '张三', age: 25, status: 'active' },
{ id: 2, name: '李四', age: 30, status: 0 },
{ id: 3, name: '张二三', age: 25, status: 1 },
{ id: 4, name: '李二四', age: 30, status: 0 },
];
$scope.onRowClick = function (item, column, $event) {
console.log('行被点击:', item, column, $event);
};
$scope.actionButtons = [
{
text: '查看',
icon: 'fa-eye',
btnClass: 'btn-info',
action: 'view',
show: function (item) { return true; }
},
{
text: '编辑',
icon: 'fa-edit',
btnClass: 'btn-primary',
action: 'edit',
show: function (item) { return item.status === 'active'; }
},
{
text: '删除',
icon: 'fa-trash',
btnClass: 'btn-danger',
action: 'delete',
show: function (item) { return true; }
},
{
text: '激活',
icon: 'fa-check',
btnClass: 'btn-success',
action: 'activate',
show: function (item) { return item.status === 'inactive'; },
iconOnly: true
}
];
$scope.$on('tableAction', function (event, args) {
var item = args.item;
switch (args.action) {
case 'view':
console.log('查看:', item);
break;
case 'edit':
console.log('编辑:', item);
break;
case 'delete':
if (confirm('确定要删除此项吗?')) {
console.log('删除:', item);
}
break;
case 'activate':
console.log('激活:', item);
break;
}
});
$scope.onPageChange = function (page, size) {
$scope.pageIndex = page;
$scope.pageSize = size;
};