一. 真分页表格基础

  1. 需求:分页,排序,搜索都是需要发API到服务端。

  2. JS实现代码:

    getStorage是localStorage一个工具方法,可以自己写这个方法。

    API参数如下:

    {

      limit: initItemCountPerPage,

      index: options1.page,

      sortKey: options1.sortKey ? encodeURIComponent(options1.sortKey) : '',

      sortType: options1.sortType ? options1.sortType: '',

      searchWord: ''

    }

angular.module('newApp')
.factory('ListTableSV', ['$timeout', 'ngTableParams', 'getStorage', function ($timeout, ngTableParams, getStorage) {
var store = getStorage('localStorage'); /**
* 创建表格
* @param options1
* @param options2
* @returns {ngTableParams|*}
*/
function createListTable(options1, options2) {
var listTable = new ngTableParams(options1, {
counts: options2.counts,
getData: function ($defer, params) {
if (!listTable.isNotFisrtLoadData) {
listTable.apiParams = initApiParams(options1);
}
listTable.isNotFisrtLoadData = true;
options2.getData(listTable.apiParams).then(function (data) {
listTable.resetCheckboxes();
var resData = data.data.data;
var onDataResult = options2.onDataResult;
if (onDataResult) {
onDataResult(resData);
}
var items = resData.items;
var dataPreprocessFunc = options2.dataPreprocessFunc;
if (dataPreprocessFunc) {
dataPreprocessFunc(items);
}
var totalCount = resData.totalCount ? resData.totalCount : 0;
params.items = (items && totalCount) ? items : [];
listTable.calculatePages(totalCount);
params.total(totalCount);
$defer.resolve(params.items);
return;
}, function () {
params.items = [];
var totalCount = 0;
listTable.calculatePages(totalCount);
params.total(totalCount);
$defer.resolve(params.items);
return;
});
}
});
listTable.tableName = options1.tableName;
listTable.titles = options1.titles;
listTable.getItemId = options2.getItemId;
listTable.toFirstPageAndReload = function () {
if (listTable.page() == 1) {
listTable.reload();
} else {
listTable.toPage(1);
}
};
return listTable;
}; function initApiParams(options1) {
var STORAGE_PAGESIZE_KEY = getKeyForStoragePageSize(options1);
if (store.get(STORAGE_PAGESIZE_KEY)) {
var initItemCountPerPage = store.get(STORAGE_PAGESIZE_KEY)['value'];
} else {
var initItemCountPerPage = options1.count;
}
return {
limit: initItemCountPerPage,
index: options1.page,
sortKey: options1.sortKey ? encodeURIComponent(options1.sortKey) : '',
sortType: options1.sortType ? options1.sortType: '',
searchWord: ''
};
} /**
* 初始化排序功能
* @param listTable
*/
function initSort(listTable, options1) {
listTable.sortClickIndex = 0;
listTable.sortClickDelayIndex = 0;
listTable.sortDelay = 750;
listTable.nextSortType = function () {
return listTable.sortParams.type == 'asc' ? 'desc' : 'asc';
};
listTable.isColumnSorting = function (key) {
return listTable.sortParams.key == key;
};
listTable.isColumnSortingByASC = function (key) {
return listTable.isColumnSorting(key) && listTable.sortParams.type == 'asc';
};
listTable.isColumnSortingByDESC = function (key) {
return listTable.isColumnSorting(key) && listTable.sortParams.type == 'desc';
};
listTable.resetSortParams = function () {
listTable.sortParams = {
key: options1.sortKey ? options1.sortKey : '',
type: options1.sortType ? options1.sortType : ''
};
};
listTable.toSorting = function (key, type) {
listTable.sortClickIndex ++;
listTable.sortParams.key = key;
listTable.sortParams.type = type;
$timeout(function() {
listTable.sortClickDelayIndex ++;
if (listTable.sortClickIndex === listTable.sortClickDelayIndex) {
listTable.sortClickIndex = 0;
listTable.sortClickDelayIndex = 0;
listTable.apiParams.sortKey = encodeURIComponent(key);
listTable.apiParams.sortType = type;
listTable.toFirstPageAndReload();
}
}, listTable.sortDelay);
}
listTable.resetSortParams();
}; /**
* 初始化条目选择框
* @param listTable
* @param withCheckboxes
* @returns {*}
*/
function initCheckboxes(listTable, withCheckboxes) {
if (withCheckboxes) {
listTable.toggleCheckAll = function () {
var checkedCount = listTable.checkedIds().length;
if (checkedCount == listTable.items.length) {
listTable.deselectAll();
} else {
listTable.selectAll();
}
};
listTable.selectAll = function () {
listTable.resetCheckboxes();
listTable.checkboxes.selectAll = true;
for (var index in listTable.items) {
listTable.checkboxes.items[listTable.getItemId(listTable.items[index])] = true;
}
};
listTable.deselectAll = function () {
listTable.resetCheckboxes();
listTable.checkboxes.selectAll = false;
for (var index in listTable.items) {
listTable.checkboxes.items[listTable.getItemId(listTable.items[index])] = false;
}
};
listTable.checkedIds = function () {
var ids = [];
for (var index in listTable.items) {
var id = listTable.getItemId(listTable.items[index]);
if (listTable.checkboxes.items[id]) {
ids.push(id);
}
}
listTable.checkboxes.selectAll = listTable.items ? (listTable.items.length == ids.length) : false;
return ids;
}
listTable.resetCheckboxes = function () {
listTable.checkboxes = {
selectAll: false,
items: {}
};
};
} else {
listTable.resetCheckboxes = function () {
}
}
listTable.resetCheckboxes();
return listTable;
}; /**
* 初始话分页功能
* @param listTable 表格
* @private
*/
function initPagination(listTable, options1) {
var STORAGE_PAGESIZE_KEY = getKeyForStoragePageSize(options1);
if (store.get(STORAGE_PAGESIZE_KEY)) {
listTable.count(store.get(STORAGE_PAGESIZE_KEY)['value']);
}
if (options1.page) {
listTable.page(options1.page);
}
listTable.editCount = listTable.count(); listTable.toCount = function (count) {
var currentCount = listTable.count();
if (currentCount != count) {
listTable.count(count);
listTable.apiParams.index = 1;
listTable.apiParams.limit = count;
}
};
listTable.prePage = function () {
var page = listTable.page();
if (page > 1) {
listTable.page(page - 1);
listTable.apiParams.index = page - 1;
}
};
listTable.nextPage = function () {
var page = listTable.page();
if (page < Math.ceil(1.0 * listTable.total() / listTable.count())) {
listTable.page(page + 1);
listTable.apiParams.index = page + 1;
}
};
listTable.toPage = function (page) {
var currentPage = listTable.page();
if (currentPage != page) {
listTable.page(page);
listTable.apiParams.index = page;
}
};
listTable.calculatePages = function (totalCount) {
var itemCountPerPage = listTable.count();
store.set(STORAGE_PAGESIZE_KEY, {
'value': itemCountPerPage
});
var totalPage = Math.ceil(1.0 * totalCount / itemCountPerPage);
var currentPage = Math.max(1, Math.min(listTable.page(), totalPage));
listTable.pages = calculateTablePages(currentPage, totalPage);
}
}; function getKeyForStoragePageSize(options1) {
return 'list_table_page_size@' + options1.tableName;
}; function calculateTablePages(currentPage, totalPage) {
if (totalPage == 0) {
return [];
}
var end = Math.min(9, totalPage);
var pages = [currentPage];
while (pages.length < end) {
var pre = pages[0] - 1;
var next = pages[pages.length - 1] + 1;
if (pre >= 1) {
pages.unshift(pre);
}
if (next <= totalPage) {
pages.push(next);
}
}
return pages;
}; function init(options1, options2) {
var listTable = createListTable(options1, options2);
initSort(listTable, options1);
initCheckboxes(listTable, options1.withCheckboxes);
initPagination(listTable, options1);
return listTable;
}; return {
init: init,
};
}]);

  2. html分页代码

<div class="ng-cloak dahuo-pagination" ng-if="params.pages.length > 0">
<div class="col-lg-6">
<div class="dataTables_info" role="status" aria-live="polite" ng-if="params.pages.length > 0">
{{ params.count() * (params.page() - 1) + 1 }}~
{{ params.count() * (params.page() - 1) + params.data.length }}条,共{{
params.total() }}条记录
</div>
</div>
<div class="col-lg-6 pagination2" style="margin-bottom: 40px">
<div class="dataTables_paginate paging_simple_numbers">
<ul class="pagination ng-table-pagination">
<li><a href="" ng-click="params.prePage()">上一页</a></li>
<li ng-repeat="page in params.pages" ng-class="{'active' : page == params.page()}">
<a href="" ng-click="params.toPage(page)">{{ page }}</a>
</li>
<li><a href="" ng-click="params.nextPage()">下一页</a></li>
</ul>
</div>
<div class="col-lg-3 pull-right">
<select class="select form-control" ng-model="params.editCount" ng-change="params.toCount(params.editCount)" style="padding: 6px; margin-top: 2px;min-width: 80px;margin-right: 10px" ng-options="size for size in params.settings().counts">
</select>
</div>
</div>
</div>

  3. 说明:

    基本实现了分页,搜索,排序(仅仅支持单个排序)等功能。搜索和排序会返回第一页。

二. 使用方法

  服务端应该返回结果{data:{data: {items: [{name1: ...}, {...}]}}};

  1. html中Table部分的代码

    没有什么其他说的,这么写就行了。

<div class="table-responsive">
<table ng-table="productsTable" class="table trans-table table-hover text-left dahuo-table" template-pagination="layouts/pagination_v3.html" width="100%">
<thead>
<tr>
<th width="5%" class="dahuo-table-header-th text-center" header="'ng-table/headers/checkbox.html'">
<span class="list-checkbox">
<input type="checkbox" ng-model="productsTable.checkboxes.selectAll" ng-disabled="productsTable.items.length < 1" ng-click="productsTable.toggleCheckAll()"/>
<span></span>
</span>
</th>
<th width="15%" ng-repeat="title in productsTable.titles" ng-class="{'sort': title.sortable}"
ng-click="productsTable.toSorting(title.key, productsTable.nextSortType())">
{{ title.name }}
<li class="fa pull-right" ng-if="title.sortable" ng-class="{'fa-sort-asc': productsTable.isColumnSortingByASC(title.key), 'fa-sort-desc': productsTable.isColumnSortingByDESC(title.key), 'fa-unsorted': !productsTable.isColumnSorting(title.key)}" aria-hidden="true" ng-style="{'color' : (!productsTable.isColumnSorting(title.key) ? '#c9c9c9' : '')}"></li>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in $data">
<td width="5%" header="'ng-table/headers/checkbox.html'"
class="str-checkbox, dahuo-table-body-th text-center">
<span class="list-checkbox">
<input type="checkbox" ng-model="productsTable.checkboxes.items[item.id]" disabled/>
<span></span>
</span>
</td>
<td>{{item.name1}}</td>
<td>{{item.name2}}</a></td>
<td>{{item.name3}}</td>
<td>{{item.name4}}</td>
<td>{{item.name5}}</td>
</tr>
<tr style="height: 100px" ng-if="$data.length < 1">
<td colspan="6" class="text-center">暂时没有数据</td>
</tr>
</tbody>
</table>
</div>

  2. Controller调用

    DS是发送API的一个服务。这里可以改成自己要发的API,改的时候注意使用$q服务返回值。

$scope.productsTable = new ListTableSV.init(
{
tableName:'fund.update.list.products',
titles:[
{key: 'name1', name: "名字1", sortable: true},
{key: 'name2', name: "名字2", sortable: true},
{key: 'name3', name: "名字3", sortable: true},
{key: 'name4', name: "名字4", sortable: true},
{key: 'name5', name: "名字5", sortable: true},
],
withCheckboxes: true,
page: 1,
count: 25,
sortKey: 'name1',
sortType: 'desc'
},
{
counts: [10, 25, 50, 100],
getData: function(apiParams) {
return DS.simplelist(apiParams);
},
getItemId: function(item) {
return item.id;
}
}
);

  

三. 搜索的调用方法

  很好调用,就两行代码,先更改API,在reload。

$scope.productsTable.apiParams.searchWord = encodeURIComponent($scope.keyword);
$scope.productsTable.toFirstPageAndReload();

ngTbale真分页实现排序、搜索等功能的更多相关文章

  1. ngTbale假分页实现排序、搜索、导出CSV等功能

    一. ngTable功能简化 使用ngTable经常有分页,排序,过滤等功能,实现诸多功能较为麻烦.为了方便开发过程,可以抽取一些table共同点写一个公有方法. 注意: 1. 由于很多特别的需求,可 ...

  2. Datatable+Springmvc+mybatis(分页+排序+搜索)_Jquery

    一.简介 通过Jqury的Datatable插件,构造数据列表,并且增加或者隐藏相应的列,已达到数据显示要求.同时, jQuery Datatable 强大的功能支持:排序,分页,搜索等. 二.前台分 ...

  3. Django中使用JS通过DataTable实现表格前端分页,每页显示页数,搜索等功能

    Django架构中自带了后端分页的技术,通过Paginator进行分页,前端点击按钮提交后台进行页面切换. 优缺点:后端分页对于数据量大的场景有其优势,但页面切换比较慢. 后端分页python3代码如 ...

  4. SpringBoot JPA实现增删改查、分页、排序、事务操作等功能

    今天给大家介绍一下SpringBoot中JPA的一些常用操作,例如:增删改查.分页.排序.事务操作等功能.下面先来介绍一下JPA中一些常用的查询操作: //And --- 等价于 SQL 中的 and ...

  5. 02 - Unit08:搜索笔记功能、搜索分页、处理插入数据库乱码问题

    搜索笔记功能 按键监听事件 $("#search_note").keydown(function(event){ var code=event.keyCode; if(code== ...

  6. Ajax+存储过程真分页实例解析(10W数据毫秒级+项目解析)

    周末闲来无事,突然想写个分页的东西玩玩,说走就走 在文章最后我会把整个项目+数据库附上,下载下来直接运行就可以看效果了.整个项目采用的是简单三层模式,开发平开是VS2010+SQL2012 一.我要做 ...

  7. 动态多条件查询分页以及排序(一)--MVC与Entity Framework版url分页版

    一.前言 多条件查询分页以及排序  每个系统里都会有这个的代码 做好这块 可以大大提高开发效率  所以博主分享下自己的6个版本的 多条件查询分页以及排序 二.目前状况 不论是ado.net 还是EF ...

  8. 使用插件bootstrap-table实现表格记录的查询、分页、排序等处理

    在业务系统开发中,对表格记录的查询.分页.排序等处理是非常常见的,在Web开发中,可以采用很多功能强大的插件来满足要求,且能极大的提高开发效率,本随笔介绍这个bootstrap-table是一款非常有 ...

  9. 基于Metronic的Bootstrap开发框架经验总结(16)-- 使用插件bootstrap-table实现表格记录的查询、分页、排序等处理

    在业务系统开发中,对表格记录的查询.分页.排序等处理是非常常见的,在Web开发中,可以采用很多功能强大的插件来满足要求,且能极大的提高开发效率,本随笔介绍这个bootstrap-table是一款非常有 ...

随机推荐

  1. 循环语言(for)

    循环语句: 给出初始条件,先判断是否满足循环条件,如果不满足条件则跳过for语句,如果满足则进入for语句循环,for语句内的代码执行完毕之后,将按照状态改变改变变量,然后判断是否符合循环条件,符合继 ...

  2. putty中文乱码问题解决

    ###putty中文乱码问题解决 用putty从windows xp连接ubuntu server或者FreeBSD系统,其中中文部分乱码,经常遇到这个问题的时候,会觉得很郁闷.现共享一些解决这个问题 ...

  3. android视频播放器系列(二)——VideoView

    最近在学习视频相关的知识,现在也是在按部就班的一步步的来,如果有同样需求的同学可以跟着大家一起促进学习. 上一节说到了可以使用系统播放器以及浏览器播放本地以及网络视频,但是这在很大程度上并不能满足我们 ...

  4. Java运行报错问题——Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook

    http://blog.csdn.net/xifeijian/article/details/8830933 上述这个朋友博文提醒,可能是因为其他软件添加了JAVA_HOME的路径造成冲突.但他支持删 ...

  5. PostgreSQL 备忘

    truncate table page_frame_mst; select setval('page_frame_mst_id_seq', 1, false): select setval('imag ...

  6. mac下iterm2 设置笔记

    1.利用brew install zsh 来安装oh my zsh 2.chsh -s /bin/zsh,修改~/.zshrc文件 alias cls='clear' alias ll='ls -l' ...

  7. 更新html技术比较

    document.write() document对象的write方法可以很简单的向页面的源代码中添加内容,不过不推荐使用. 优点:可以快速简单的让初学者理解如何向页面添加内容: 缺点: 只有页面初始 ...

  8. 【sqli-labs】 对于less34 less36的宽字节注入的一点深入

    1.AddSlashes() 首先来观察一下是如何通过构造吃掉转义字符的 先将less 34的网页编码换成gbk 加上一些输出 echo "Before addslashes(): &quo ...

  9. blender_(uv应用)................http://digitalman.blog.163.com/blog/static/23874605620174172058299/

    轻松学习Blender基础入门之九:UV-1 2017-06-21 14:24:49|  分类: Blender |举报 |字号 订阅     下载LOFTER 我的照片书  |   [前言]     ...

  10. 网站卡测试用 PageSpeed Insights

    这个是google测试网页的;https://developers.google.com/speed/pagespeed/insights/ PageSpeed Insights 简介 PageSpe ...