[转]ExtJS Grid 分页时保持选中的简单实现方法
原文地址 :http://www.qeefee.com/article/ext-grid-keep-paging-selection
ExtJS中经常要用到分页和选择,但是当选择遇到分页的时候,杯具就发生了,每一次翻页,其它页面的选中行就消失了。Ext 没有为我们提供内置的保持选中的支持,只有我们自己动手来实现了。
先说一下具体的思路吧:首先在页面中创建一个数组,用来存储Grid的所有选中行,然后分别处理selModel的select和unselect事件和Store的load事件。
- 在select事件中,将选中的行存储在全局数组中
- 在unselect事件中,将取消选中的行从数组中移除
- 在load事件中,遍历加载到的数据,判断哪些应该选中
那么,首先我们来快速的创建一个Grid,并绑定一些分页数据:
Ext.onReady(function () {
var supplierStore = Ext.create("Ext.data.Store", {
fields: [
{ name: "Name", type: "string" },
{ name: "Phone", type: "string" },
{ name: "Address", type: "string" }
],
autoLoad: true,
pageSize: 3,
proxy: {
type: "ajax",
url: rootUrl + "Grid/FetchPageData",
actionMethods: { read: "POST" },
reader: {
type: "json",
root: "data.records",
totalProperty: "data.total"
}
}
});
var grid = Ext.create("Ext.grid.GridPanel", {
border: true,
width: 600,
height: 200,
store: supplierStore,
columnLines: true,
enableColumnHide: false,
enableColumnMove: false,
enableLocking: true,
selModel: Ext.create("Ext.selection.CheckboxModel", {
mode: "MULTI",
checkOnly: true
}),
columns: [
{ text: "名称", dataIndex: "Name", width: 150, sortable: false },
{ text: "电话", dataIndex: "Phone", width: 150, sortable: false },
{ text: "地址", dataIndex: "Address", width: 260, sortable: false }
],
bbar: { xtype: "pagingtoolbar", store: supplierStore, displayInfo: true },
renderTo: Ext.getBody()
});
});
服务器段的代码:
public JsonResult FetchPageData()
{
int pageIndex = Convert.ToInt32(Request["page"]);
int pageSize = Convert.ToInt32(Request["limit"]); OperateResult result = new OperateResult();
var pageData = SupplierModel.SupplierRecords.Skip((pageIndex - 1) * pageSize).Take(pageSize); result.Set(true, new { records = pageData, total = SupplierModel.SupplierRecords.Count }); return Json(result);
}
这里面用到的SupplierModel代码如下:
public class SupplierModel
{
public string Name { get; set; }
public string Phone { get; set; }
public string Address { get; set; } public static List<SupplierModel> SupplierRecords = null;
static SupplierModel()
{
SupplierRecords = new List<SupplierModel>();
SupplierRecords.Add(new SupplierModel() { Name = "北京电信", Phone = "10000", Address = "北京市XX区XX路" });
SupplierRecords.Add(new SupplierModel() { Name = "北京移动", Phone = "10086", Address = "北京市XX区XX路" });
SupplierRecords.Add(new SupplierModel() { Name = "北京联通", Phone = "10010", Address = "北京市XX区XX路" });
SupplierRecords.Add(new SupplierModel() { Name = "北京铁通", Phone = "", Address = "北京市XX区XX路" });
SupplierRecords.Add(new SupplierModel() { Name = "北京邮政", Phone = "95599", Address = "北京市XX区XX路" });
}
}
硬编码了一些数据,如果我们每页显示3行,还是能够分页的。
然后运行程序,看看我们的界面吧:

接下来看看我们要完成的分页保持选中。
第一步,添加一个全局的数据,用来保存选中的数据
var AllSelectedRecords = [];
第二步,为selModel添加select事件
listeners: {
select: function (me, record, index, opts) {
AllSelectedRecords.push(record);
}
}
第三步,为selModel添加unselect事件
deselect: function (me, record, index, opts) {
AllSelectedRecords = Ext.Array.filter(AllSelectedRecords, function (item) {
return item.get("Name") != record.get("Name");
});
},
第四步,store添加load事件
listeners: {
load: function (me, records, success, opts) {
if (!success || !records || records.length == 0)
return;
//根据全局的选择,初始化选中的列
var selModel = grid.getSelectionModel();
Ext.Array.each(AllSelectedRecords, function () {
for (var i = 0; i < records.length; i++) {
var record = records[i];
if (record.get("Name") == this.get("Name")) {
selModel.select(record, true, true); //选中record,并且保持现有的选择,不触发选中事件
}
}
});
}
},
完成这四个步骤以后,我们来看一下完整的代码:
Ext.onReady(function () {
var supplierStore = Ext.create("Ext.data.Store", {
fields: [
{ name: "Name", type: "string" },
{ name: "Phone", type: "string" },
{ name: "Address", type: "string" }
],
autoLoad: true,
pageSize: 3,
listeners: {
load: function (me, records, success, opts) {
if (!success || !records || records.length == 0)
return;
//根据全局的选择,初始化选中的列
var selModel = grid.getSelectionModel();
Ext.Array.each(AllSelectedRecords, function () {
for (var i = 0; i < records.length; i++) {
var record = records[i];
if (record.get("Name") == this.get("Name"//选中record,并且保持现有的选择,不触发选中事件
}
}
});
}
},
proxy: {
type: "ajax",
url: rootUrl + "Grid/FetchPageData",
actionMethods: { read: "POST" },
reader: {
type: "json",
root: "data.records",
totalProperty: "data.total"
}
}
});
var AllSelectedRecords = [];
var grid = Ext.create("Ext.grid.GridPanel", {
border: true,
width: 600,
height: 200,
store: supplierStore,
columnLines: true,
enableColumnHide: false,
enableColumnMove: false,
enableLocking: true,
selModel: Ext.create("Ext.selection.CheckboxModel", {
mode: "MULTI",
listeners: {
deselect: function (me, record, index, opts) {
AllSelectedRecords = Ext.Array.filter(AllSelectedRecords, function (item) {
return item.get("Name") != record.get("Name");
});
},
select: function (me, record, index, opts) {
AllSelectedRecords.push(record);
}
}
}),
columns: [
{ text: "名称", dataIndex: "Name", width: 150, sortable: false },
{ text: "电话", dataIndex: "Phone", width: 150, sortable: false },
{ text: "地址", dataIndex: "Address", width: 260, sortable: false }
],
bbar: { xtype: "pagingtoolbar", store: supplierStore, displayInfo: true },
renderTo: Ext.getBody()
});
});
然后再次运行程序,试试翻页的选中的效果吧,少年,这样就轻松的实现了分页的选中。这样做的优点的非常的灵活,可以在页面中自由的使用,缺点也很明 显,他并不能够复用,如果你在别的Grid中使用,那就要继续再定义一个全局变量,以后的章节中我们会完成一个封装好的grid。
[转]ExtJS Grid 分页时保持选中的简单实现方法的更多相关文章
- Extjs grid分页多选记忆功能
很多同事在用extjs grid做分页的时候,往往会想用grid的多选功能来实现导出Excel之类的功能(也就是所谓的多选记忆功能),但在选选择下一页的时候 上一页选中的已经清除 这是因为做分页的时候 ...
- extjs grid 分页
在使用extjs创建带分页功能的 grid 如下: 1.创建一个模型 // 创建算定义模型 模型名称 User Ext.define('User', { extend: 'Ext.data.Model ...
- C#连接sqlserver分页查询的两个简单的方法
/// <summary> /// 分页查询函数 /// </summary> /// <param name="co ...
- ExtJS入门教程06,grid分页的实现
前面两篇内容分别介绍了extjs grid的基本用法和extjs grid异步加载数据,这篇文章将介绍extjs grid的分页. 数据量大的时候我们必须用到分页,结合上一篇的异步加载数据,今天我们就 ...
- ExtJS实现分页grid paging
背景 分页查询在Web页面中比例很大,我自己也写过分页框架,也用过很多第三方分页. 基于jquery的dataTables,那么多例子.清晰API.应用广泛.开源,即使是新手也可以很快上手. ExtJ ...
- Javascript - ExtJs - 组件 - 分页
服务端只需要返回如下格式的字符串,ExtJs就可以解析并自动生成分页数据. , name: "sam" } ] } 准备: CREATE PROCEDURE [dbo]. ...
- extjs grid renderer用法
extjs grid renderer用法 摘自:http://www.cnblogs.com/ljian/archive/2011/10/27/2226959.html var cm = new E ...
- ExtJs4 SpringMvc3 实现Grid 分页
新建一个Maven webapp项目,webxml以及spring配置没什么需要注意的,不再赘述. Maven依赖:(个人习惯,有用没用的都加上...) <project xmlns=" ...
- 72. js EXTJS grid renderer用法
转自:https://blog.csdn.net/shancunxiaoyazhi/article/details/22156083 renderer : Function (可选的)该函数用于加工单 ...
随机推荐
- c++ map 使用
. 包含头文件: #include <map> 2. 构造函数: std::map<char,int> first; first[; first[; first[; first ...
- Google官方关于Android架构中MVP模式的示例续-DataBinding
基于前面的TODO示例,使用Data Binding库来显示数据并绑定UI元素的响应动作. 这个示例并未严格遵循 Model-View-ViewModel 或 Model-View-Presenter ...
- 设置JDK环境变量(linux版)
设置环境变量一.修改/etc/profile文件当本机仅仅作为开发使用时推荐使用这种方法,因为此种配置时所有用户的shell都有权使用这些环境变量,可能会给系统带来安全性问题.用文本编辑器打开/et ...
- 关于Java泛型的使用
在目前我遇到的java项目中,泛型应用的最多的就属集合了.当要从数据库取出多个对象或者说是多条记录时,往往都要使用集合,那么为什么这么使用,或者使用时有什么要注意的地方,请关注以下内容. 感谢Wind ...
- ++a和a++的区别
另: short s = 4; s = s + 1; // 编译不通过.因为编译器无法判断等号右边的运算结果是否依然在等号左边的short类型范围内,容易丢失精度. s += 1; // 编译通过.+ ...
- React Native之 Navigator与NavigatorIOS使用
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- Android Studio使用时源码到处报红色警告,运行时又没错
转载地址:http://www.07net01.com/program/2016/04/1452749.html [摘要:正在AS上开辟时,碰到那个题目,翻开全部的Java源文件,右边一起标赤色,找没 ...
- Git本地服务器搭建及使用详解
Git本地服务器搭建及使用 Git是一款免费.开源的分布式版本控制系统.众所周知的Github便是基于Git的开源代码库以及版本控制系统,由于其远程托管服务仅对开源免费,所以搭建本地Git服务器也是个 ...
- Spring 4 创建REST API
什么是REST 全称:表述性状态转移 (Representational State Transfer), 将资源的状态以最适合客户端或服务端的形式从服务器端转移到客户端(或者反过来). 面向资源,而 ...
- 机器学习实战笔记(Python实现)-05-支持向量机(SVM)
--------------------------------------------------------------------------------------- 本系列文章为<机器 ...