table表运用在后台管理用得频繁,一般bootstrap的class="table"就能满足样式需求以及分页需求,但是每个产品经理需求不一样,比如说分页:bootstrap分页实现是这样子的

但原型要求这样子的

被推荐angular相关table组件smart-table算是功能相当全的,学习官网地址http://lorenzofox3.github.io/smart-table-website/#section-intro

看了一下,上手可以,但是要深入了解内容较难。调研smart-table主要是为了进一步在工作中完善之前的table表不足之处。比如涉及功能包括排序、搜索、分页、选中等等。

下面作为angular新手如何整合了smart-table和自定义的分页,当然,提升优化空间相当大!

实现的效果页:

首先下载smart-table,我用的bower: bower install angular-smart-table --save-dev

在index中引入相关文件,下面是我的index中引入的一些文件:

<!--angular文件-->
<script src="bower_components/angular/angular.js"></script>
<!--ui-route配置路由库文件-->
<script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
<!--支持angular的动画库文件-->
<script src="bower_components/angular-animate/angular-animate.js"></script>
<!--smart-table库文件-->
<script src="bower_components/angular-smart-table/dist/smart-table.js"></script>
<!--按需加载库文件-->
<script src="bower_components/oclazyload/dist/ocLazyLoad.js"></script>
<!--支持angular的bootstrap库文件-->
<script src="src/js/ui-bootstrap-tpls.min.js"></script>
<!--bootstrap注入主应用文件-->
<script src="bootstrap.js"></script>
<!--smart-table测试功能文件夹的路由配置-->
<script src="smartTableTest/smartTable.route..js"></script>
<!--控制输入框数值directive-->
<script src="common/directive/my-max-number.js"></script>
<!--smart-table官网提供的示例directive,增加复选框按钮-->
<script src="common/directive/custom-select.js"></script>

smart-table.view.html

<table class="table" st-pipe="callServer" st-table="displayed">
<thead>
<tr>
<th></th>
<th st-sort="id">id</th>
<th st-sort="name">name</th>
<th st-sort="age">age</th>
<th st-sort="saved">saved</th>
</tr>
<tr>
<th></th>
<th><input st-search="id" /></th>
<th><input st-search="name" /></th>
<th><input st-search="age" /></th>
<th><input st-select="saved" /></th>
</tr>
</thead>
<tbody ng-show="!isLoading">
<tr ng-repeat="row in displayed">
<td cs-select="row"></td>
<td>{{row.id}}</td>
<td>{{row.name}}</td>
<td>{{row.age}}</td>
<td>{{row.saved}}</td>
</tr>
</tbody>
<tbody ng-show="isLoading">
<tr>
<td colspan="4" class="text-center">Loading...</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="4" class="text-center">
<div st-pagination="" st-items-by-page="tableState.pagination.number" st-displayed-pages="7" st-template="common/template/pagination.html"></div>
</td>
</tr>
</tfoot>
</table>

template中的pagination.html代码:

<form class="pagination-define">
<select ng-model="$parent.tableState.pagination.number" ng-options="item as item for item in $parent.itemsOptions" ng-change="$parent.getData($parent.tableState.pagination.currentPage)">
<option value="">--请选择--</option>
</select>
<uib-pagination total-items="$parent.tableState.pagination.totalItemCount" items-per-page="$parent.tableState.pagination.number" ng-model="$parent.tableState.pagination.currentPage" ng-change="$parent.getData($parent.tableState.pagination.currentPage)" max-size="$parent.tableState.pagination.totalItemCount/$parent.tableState.pagination.number" class="pagination-sm" boundary-link-numbers="true" boundary-links="true" rotate="false" previous-text="&lsaquo;" next-text="&rsaquo;" first-text="&laquo;" last-text="&raquo;"></uib-pagination>
<input type="text" ng-model="$parent.tableState.pagination.inputCurPage" min=1 my-max-number ="{{$parent.tableState.pagination.totalItemCount/$parent.tableState.pagination.number}}" current-page="{{$parent.tableState.pagination.currentPage}}">
<button class="btn btn-info" ng-click="$parent.getData($parent.tableState.pagination.inputCurPage)" ng-disabled="!$parent.tableState.pagination.inputCurPage||$parent.tableState.pagination.inputCurPage==''">Go</button>
</form>

smartTable.controller.js代码:

angular.module("smartTableApp")
.controller("smartTableTestCtrl", ['$scope', '$filter', 'SmartTableTestServ', function($scope, $filter, SmartTableTestServ) {
//动态显示条数
$scope.itemsOptions = [5,10,20];
//smart-table配置变量
$scope.displayed = [];
$scope.callServer = function callServer(tableState) {
$scope.tableState = tableState; //tableState对象中包含sort排序信息以及search搜索信息
$scope.getData(1);
};
$scope.getData = function(pageNo) {
console.log("getData exe");
var pagination = $scope.tableState.pagination;
var start = pagination.start || 0; //从第几条数据开始查询,默认0条
var number = pagination.number || 10; //每页显示条数
pagination.currentPage = pagination.inputCurPage = pageNo;
$scope.isLoading = true; //控制数据加载状态
//数据获取函数
SmartTableTestServ.getPage(start, number, $scope.tableState).then(function(result) {
console.log(result);
$scope.displayed = result.data;
pagination.totalItemCount = result.totalItems; //设置数据总条数
pagination.maxSize = pagination.totalItemCount/pagination.number; //一共有多少页
$scope.isLoading = false;
console.log("tableState信息:",$scope.tableState);
});
}
}]);

对分页输入框中directive控制my-max-number.js代码

myApp
//控制页码跳转框
.directive('myMaxNumber', ['$timeout', function ($timeout) {
return {
restrict: 'EA',
require: 'ngModel',
scope: {
maxNumber: '@myMaxNumber',
currentPage: '@currentPage'
},
link: function (scope, element, attr, ctrl) {
ctrl.$parsers.push(function (viewValue) {
var maxNumber = parseInt(scope.maxNumber, 10);
var curNumber = scope.currentPage; //当前页数
var transformedInput = viewValue.replace(/[^0-9]/g, '');
if (transformedInput !== viewValue||parseInt(transformedInput,10)<1||parseInt(transformedInput,10)>maxNumber) {
ctrl.$setViewValue(curNumber);
ctrl.$render();
return curNumber;
}
return viewValue;
});
}
};
}]);

官网提供的复选框directive

myApp
.directive('csSelect', function () {
return {
require: '^stTable',
template: '<input type="checkbox"/>',
scope: {
row: '=csSelect'
},
link: function (scope, element, attr, ctrl) { element.bind('change', function (evt) {
scope.$apply(function () {
ctrl.select(scope.row, 'multiple');
});
}); scope.$watch('row.isSelected', function (newValue, oldValue) {
if (newValue === true) {
element.parent().addClass('st-selected');
} else {
element.parent().removeClass('st-selected');
}
});
}
};
});

沿用了官网提供的service

angular.module("smartTableApp")
.service ('SmartTableTestServ', ['$q', '$filter', '$timeout', function ($q, $filter, $timeout) {
//this would be the service to call your server, a standard bridge between your model an $http // the database (normally on your server)
var randomsItems = []; function createRandomItem(id) {
var heroes = ['Batman', 'Superman', 'Robin', 'Thor', 'Hulk', 'Niki Larson', 'Stark', 'Bob Leponge'];
return {
id: id,
name: heroes[Math.floor(Math.random() * 7)],
age: Math.floor(Math.random() * 1000),
saved: Math.floor(Math.random() * 10000)
}; } for (var i = 0; i < 100; i++) {
randomsItems.push(createRandomItem(i));
} //fake call to the server, normally this service would serialize table state to send it to the server (with query parameters for example) and parse the response
//in our case, it actually performs the logic which would happened in the server
function getPage(start, number, params) { var deferred = $q.defer(); var filtered = params.search.predicateObject ? $filter('filter')(randomsItems, params.search.predicateObject) : randomsItems; if (params.sort.predicate) {
filtered = $filter('orderBy')(filtered, params.sort.predicate, params.sort.reverse);
} var result = filtered.slice(start, start + number); $timeout(function () {
//note, the server passes the information about the data set size
deferred.resolve({
data: result,
totalItems: filtered.length,
numberOfPages: Math.ceil(filtered.length / number)
});
}, 1500); return deferred.promise;
} return {
getPage: getPage
}
}]);

先记录以上所需代码,写的不清不楚,等心情好点再细细补充说明~见谅,另外还需要改善优化

  

angular smart-table组件如何定制化之初步研究的更多相关文章

  1. 依赖于angular的table组件

    组件实现了以下功能 1. 列宽可动态拖动 2. 列数据排序 3. 列过滤 4. 列位置自由调整 除了需要引入angular.js(我用的是1.4.6版本),还需要引用一个angular衍生出来的插件n ...

  2. Angular企业级开发(10)-Smart Table插件开发

    1.Smart Table内置的分页功能 Smart Table是基于AngularJS模块特性开发出来的一款优秀的表格组件,默认就支持过滤.排序等核心功能.开发者基于它也可以开发插件,满足个性化需求 ...

  3. Yoshino: 一个基于React的可定制化的PC组件库

    Github: https://github.com/Yoshino-UI... Docs: https://yoshino-ui.github.io/#/ Cli-Tool: https://git ...

  4. ant-design的Table组件自定义空状态

    Table,是antd中一个我们经常使用的组件,在官方文档中给出了非常详细的实例以及API, 但在我们在使用过程中可能需要根据项目的要求来定制空状态时的展示. 什么是空状态呢? 在antd的官方文档中 ...

  5. jquery-ui-datepicker定制化,汉化,因手机布局美观化源码修改

    感谢浏览,欢迎交流=.= 公司微信网页需要使用日历控件,想到jquery-mobile,但是css影响页面布局,放弃后使用jquery-ui-datepicker. 话不多说,进入正题: 1.jque ...

  6. python+robot framework实现测报告定制化和邮件发送

    前面已经介绍了python+robot framework自动化框架和基本原理的实现,详情请看 python+robot framework接口自动化测试 本章主要讲解报告已经产生那如何以自动化的方式 ...

  7. AI应用开发实战 - 定制化视觉服务的使用

    AI应用开发实战 - 定制化视觉服务的使用 本篇教程的目标是学会使用定制化视觉服务,并能在UWP应用中集成定制化视觉服务模型. 前一篇:AI应用开发实战 - 手写识别应用入门 建议和反馈,请发送到 h ...

  8. AntD02 Table组件的使用

    1 前提准备 1.1 创建一个angular项目 1.2 将 Ant Design 整合到 Angular 项目中 1.3 官方文档 点击前往 2 简单使用 <nz-table #rowSele ...

  9. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:11.定制化Log输出

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 前言 在<迷你微信>服务器中,我们用了Log4J来进行输出,这可以在我们程序出现异常的时候找到错误发生时 ...

随机推荐

  1. eclipse自动提示设置以及问题:去除变量自动提示(图文详解)

    第一件事 eclipse设置为自动提示 配置步骤: 1 Window > Preferences > Java > Editor > Content Assist 2 &quo ...

  2. oracle之备份详解

    1.冷备份(执行冷备份前必须关闭数据库) 物理备份(备份物理数据库文件) 2.热备份(热备份是当数据库正在运行时进行数据备份的过程.执行热备份的前提是:数据库运行在可归档日志模式.适用于24X7不间断 ...

  3. Jmeter常用功能详解

    嘻嘻,忙碌的一周,马上就到周四了~明天就是周五了,可以去嗨了! 这几天正式成立了一个微信订阅号,旨在免费帮助需要入门软件测试的小白! 各位走过路过的亲,欢迎订阅哦:扫描二维码即可订阅

  4. python+selenium自动化软件测试(第13章):selenium面试题

    前言最近看到群里有小伙伴贴出一组面试题,最近又是跳槽黄金季节,小编忍不住抽出一点时间总结了下 一.selenium中如何判断元素是否存在?expected_conditions模块提供了16种判断方法 ...

  5. 又一流氓推广Microsoft Edge,我勒个去

    最新的Windows10 的升级也是醉了,不得不吐槽一个非常流氓的浏览器推广:Microsoft Edge(这小婊砸). 为了将之前的历史包袱IE干掉,这次微软也是蛮拼的,直接把IE从电脑里干掉了,你 ...

  6. 性能测试中vmstat命令的常见使用。

    vmstat是Linux中常用的一个命令,尤其是在进行性能测试时,使用该命令,往往能辅助我们进行性能问题定位. 我们先看一下这个命令执行得到的数据. [root@xxx ~]# vmstat 1 10 ...

  7. MiniProfiler工具

    MiniProfiler工具介绍   MiniProfiler是一款针对.NET, Ruby, Go and Node.js的性能分析的轻量级程序.可以对一个页面本身,及该页面通过直接引用.Ajax. ...

  8. 非对话框程序创建组合框Groupbox

    对话框程序中的控件,例如button.groupbox之类,是直接放上去的.当然,除groupbox以外,在MFC中其他控件都有相对应的类,以支持程序员在非对话框程序中动态创建控件.而唯独好像没有gr ...

  9. 201521123035《Java程序设计》第八周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 //泛型方法,打印MyStack的所有元素的薪水,不管MyStack中 ...

  10. 201521123034《Java程序设计》第七周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 答:从ArrayList ...