jqGrid与Struts2的结合应用(七) —— 浅谈排序
终于讲到排序了,这一部分应该说还是比较好理解的。
jqGrid通过colModel选项中的sortable来控制是否可以以某列的值排序。sortable的默认值是true,当设为false时,即此列不能用于排序。
- $(function(){
- $("#gridTable").jqGrid({
- ...
- colModel: [
- {name:"id",index:"id",label:"编码",width:40},
- {name:"lastName",index:"lastName",label:"姓",width:80},
- {name:"firstName",index:"firstName",label:"名",width:80},
- {name:"email",index:"email",label:"电子邮箱",width:160,sortable:false},
- {name:"telNo",index:"telNo",label:"电话",width:120,sortable:false}
- ],
- ...
- });
- });
当点击sortable为true的列首时,jqGrid会向Server发送排序请求,例如:
http://localhost:8085/Hare/jqGridTest/jqGrid05.action?search=false&nd=1279006749246&rows=15&page=3&sidx=firstName&sord=asc
注:其中sord和sidx参数名都是在jqGrid的prmNames选项中设定的(可参考本系列文章的第一篇)。而sidx参数的值即各列的colModel的index选项值。(在查询和排序时,发送的关于列的参数都是基于colModel的index属性的)
后面的事情就交给服务器端的Action来处理了,还拿我们的Contact联系人列表为例。
既然我们可能会分别使用不同的字段来排序,那么就必须为Contact提供不同的Comparator来简化比较操作。因此我写了一个针对Contact的Comparator的工厂类,用来根据不同的字段提供不同的Comparator。
ContactComparatorFactory的代码:
- package cn.gengv.struts2ex.jqGrid;
- import java.text.Collator;
- import java.util.Comparator;
- import com.byzl.hare.model.Contact;
- public class ContactComparatorFactory {
- private static Collator collator_Chinese = Collator.getInstance(java.util.Locale.CHINA);
- private final static Comparator<Contact> idComparator = new IdComparator();
- private final static Comparator<Contact> firstNameComparator = new FirstNameComparator();
- private final static Comparator<Contact> lastNameComparator = new LastNameComparator();
- private final static Comparator<Contact> fullNameComparator = new FullNameComparator();
- private final static Comparator<Contact> idCardNoNoComparator = new IdCardNoComparator();
- private final static Comparator<Contact> nationalityComparator = new NationalityComparator();
- public static Comparator<Contact> getComparator(String compareType) {
- if ("id".equalsIgnoreCase(compareType)) {
- return idComparator;
- } else if ("firstName".equalsIgnoreCase(compareType)) {
- return firstNameComparator;
- } else if ("lastName".equalsIgnoreCase(compareType)) {
- return lastNameComparator;
- } else if ("fullName".equalsIgnoreCase(compareType)) {
- return fullNameComparator;
- } else if ("idCardNoNo".equalsIgnoreCase(compareType)) {
- return idCardNoNoComparator;
- } else if ("nationality".equalsIgnoreCase(compareType)) {
- return nationalityComparator;
- } else {
- return null;
- }
- }
- public static class IdComparator implements Comparator<Contact> {
- public int compare(Contact c1, Contact c2) {
- if (c1 == null && c2 == null) {
- return 0;
- } else if (c1 == null && c2 != null) {
- return -1;
- } else if (c1 != null && c2 == null) {
- return 1;
- } else {
- int id1 = c1.getId();
- int id2 = c2.getId();
- return id1 == id2 ? 0 : (id1 < id2 ? -1 : 1);
- }
- }
- }
- public static class FirstNameComparator implements Comparator<Contact> {
- public int compare(Contact c1, Contact c2) {
- if (c1 == null && c2 == null) {
- return 0;
- } else if (c1 == null && c2 != null) {
- return -1;
- } else if (c1 != null && c2 == null) {
- return 1;
- } else {
- String s1 = c1.getFirstName();
- String s2 = c2.getFirstName();
- if (s1 == null && s2 == null) {
- return 0;
- } else if (s1 == null && s2 != null) {
- return -1;
- } else if (s1 != null && s2 == null) {
- return 1;
- } else {
- return collator_Chinese.compare(s1, s2);
- }
- }
- }
- }
- public static class LastNameComparator implements Comparator<Contact> {
- public int compare(Contact c1, Contact c2) {
- if (c1 == null && c2 == null) {
- return 0;
- } else if (c1 == null && c2 != null) {
- return -1;
- } else if (c1 != null && c2 == null) {
- return 1;
- } else {
- String s1 = c1.getLastName();
- String s2 = c2.getLastName();
- if (s1 == null && s2 == null) {
- return 0;
- } else if (s1 == null && s2 != null) {
- return -1;
- } else if (s1 != null && s2 == null) {
- return 1;
- } else {
- return collator_Chinese.compare(s1, s2);
- }
- }
- }
- }
- public static class FullNameComparator implements Comparator<Contact> {
- public int compare(Contact c1, Contact c2) {
- if (c1 == null && c2 == null) {
- return 0;
- } else if (c1 == null && c2 != null) {
- return -1;
- } else if (c1 != null && c2 == null) {
- return 1;
- } else {
- String s1 = c1.getFullName();
- String s2 = c2.getFullName();
- if (s1 == null && s2 == null) {
- return 0;
- } else if (s1 == null && s2 != null) {
- return -1;
- } else if (s1 != null && s2 == null) {
- return 1;
- } else {
- return collator_Chinese.compare(s1, s2);
- }
- }
- }
- }
- public static class IdCardNoComparator implements Comparator<Contact> {
- public int compare(Contact c1, Contact c2) {
- if (c1 == null && c2 == null) {
- return 0;
- } else if (c1 == null && c2 != null) {
- return -1;
- } else if (c1 != null && c2 == null) {
- return 1;
- } else {
- String s1 = c1.getIdCardNo();
- String s2 = c2.getIdCardNo();
- if (s1 == null && s2 == null) {
- return 0;
- } else if (s1 == null && s2 != null) {
- return -1;
- } else if (s1 != null && s2 == null) {
- return 1;
- } else {
- return s1.compareToIgnoreCase(s2);
- }
- }
- }
- }
- public static class NationalityComparator implements Comparator<Contact> {
- public int compare(Contact c1, Contact c2) {
- if (c1 == null && c2 == null) {
- return 0;
- } else if (c1 == null && c2 != null) {
- return -1;
- } else if (c1 != null && c2 == null) {
- return 1;
- } else {
- String s1 = c1.getNationality();
- String s2 = c2.getNationality();
- if (s1 == null && s2 == null) {
- return 0;
- } else if (s1 == null && s2 != null) {
- return -1;
- } else if (s1 != null && s2 == null) {
- return 1;
- } else {
- return collator_Chinese.compare(s1, s2);
- }
- }
- }
- }
- }
然后再来看JqGridBaseAction,其中添加了一个抽象方法,用来将数据结果进行排序。
- package cn.gengv.struts2ex.jqGrid;
- // import ...
- @SuppressWarnings("serial")
- public abstract class JqGridBaseAction<T> extends ActionSupport {
- ...
- // (1)添加排序方法
- public abstract void sortResults(List<T> results, String field, String order);
- public String refreshGridModel() {
- try {
- List<Criterion> criteria = Collections.emptyList();
- if(search == true) {
- criteria = new ArrayList<Criterion>();
- if(filters != null && filters.length()>0) {
- criteria.addAll(this.generateSearchCriteriaFromFilters(filters));
- }
- Criterion criterion = this.generateSearchCriterion(searchField, searchString, searchOper);
- if(criterion != null) {
- criteria.add(criterion);
- }
- }
- List<T> results = Collections.emptyList();
- int from = rows * (page - 1);
- int length = rows;
- if(loadonce) {
- from = 0;
- length = 100;
- }
- if(!criteria.isEmpty()) {
- record = this.getResultSize(criteria);
- results = this.listResults(criteria, from, length);
- } else {
- record = this.getResultSize();
- results = this.listResults(from, length);
- }
- // (2)将结果排序
- if(sidx != null && sord != null) {
- sortResults(results, sidx, sord);
- }
- this.setGridModel(results);
- total = (int) Math.ceil((double) record / (double) rows);
- return SUCCESS;
- } catch (Exception e) {
- e.printStackTrace();
- this.addActionError(e.getMessage());
- return ERROR;
- }
- }
- ...
- }
而在ListContactsAction中提供了方法实现:
- package cn.gengv.struts2ex.jqGrid;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import com.byzl.hare.dao.impl.Criterion;
- import com.byzl.hare.model.Contact;
- import com.byzl.hare.service.ContactService;
- @SuppressWarnings("serial")
- public class ListContactsAction extends JqGridBaseAction<Contact> {
- ...
- @Override
- public void sortResults(List<Contact> results, String field, String order) {
- // (1)根据field获得对应的Comparator
- Comparator<Contact> comparator = ContactComparatorFactory.getComparator(field);
- if(comparator != null) {
- // (2)使用Comparator排序
- Collections.sort(results, comparator);
- // (3)如果需要的排序顺序为desc,则颠倒顺序
- if("desc".equals(order)) {
- Collections.reverse(results);
- }
- }
- }
- ...
- }
不过这个例子存在一定的局限性,即只能将当前页中的数据根据某列进行排序;而不能跨页间进行数据排序。之所以存在这种局限,也是源于实际应用中的客观限制。还以这个例子来说,数据库里总共模拟了两万多条数据记录。如果每次要将这些记录进行排里的话,除非有数据库索引支持,否则所要消耗的时间也是相当客观的,对于用户体验来说,几乎就是灾难。如果数据量更多的话,结果可想而知。
因此,我们应该换一个角度来看这个问题,用户之所以使用排序,更多的目的还是在于查找数据方便,既然我们可以提供条件查询(尤其是复杂条件查询),那么用户对于排序的需求也就不会那么迫切了。同时也可以体会到,排序更多地应用在少量数据的场合下。
转载于:https://www.cnblogs.com/tware-dsy/archive/2013/02/26/2933659.html
jqGrid与Struts2的结合应用(七) —— 浅谈排序相关推荐
- Python进阶(七)浅谈python3和Python2的区别
文章目录 一.前言 二.print函数 2.1 举例 三.reduce()函数 四.try except 五.打开文件 六.从键盘录入一个字符串 七.整形除法自动转为float 八.新的字符串格式化方 ...
- 浅谈排序算法:冒泡排序法和选择排序法的区别
之前学习了冒泡排序法和选择排序法,最近被老师问某个道题用的是什么排序法.自己居然答不出来,才发现自己没有真正弄懂,这两个算法的原理和区别,所以····· 1冒泡排序法 1.1什么是冒泡排序法? 顾名思 ...
- 浅谈struts2之chain
前一段时间,有关chain的机制着实困绕了许久.尽管网上有许多关于chain的解说,但要不是只谈大理论,不结合实例:要不就是只有示例,没有挖出示例背后的意义. 先解释下chain吧: Chain ...
- catia三维轴承_浅谈基于CATIA二次开发的单排四点接触球轴承三维设计论文
浅谈基于CATIA二次开发的单排四点接触球轴承三维设计论文 一.概述 单排四点接触球转盘轴承是一种能够同时承受较大轴向负荷.径向负荷和倾覆力矩等综合载荷,集支承.旋转.传动.固定等多种功能于一身的特殊 ...
- 浅谈大数据中的 2PC、3PC、Paxos、Raft、ZAB
一致性 简述 一致性,是指对每个节点一个数据的更新,整个集群都知道更新,并且是一致的.假设一个具有N个节点的分布式系统,当其满足以下条件时,我们说这个系统满足一致性: 全认同: 所有N个节点都认同一个 ...
- 浅谈(线性)卷积公式为什么要翻转
浅谈(线性)卷积公式为什么要翻转 信号系统 信号处理 卷积 文章题目之所以写 (线性)卷积,是因为卷积有很多种,如循环卷积,周期卷积以及线性卷积.本文主要讨论线性卷积,为书写方便,下文都以卷积代替.至 ...
- 沈向洋:浅谈人工智能创造
来源:AI科技评论 本文约7000字,建议阅读10分钟 本文为你介绍沈向洋的<浅谈人工智能创造>,分享过去六年其在微软小冰身上得到的一些实践想法. 2020年9月21日上午9点,由北京大学 ...
- 浅谈ASP.NET的内部机制(一)
浅谈ASP.NET的内部机制(一) 前言:当一个Http请求发送给一个aspx页面时,服务器进行了哪些操作?又如何来解析这个请求?ASP.NET在接收请求后是怎么运行的,如怎么编译以及怎么样用托管的代 ...
- 浅谈ASP.NET内部机制(五)
浅谈ASP.NET内部机制(五) 前言:本章要谈页面生命周期了,过程挺多的,但是一点都不难.不信可以看看.我尽量的讲的平实一些,而且理解页面的生命周期对喜欢开发自定义控件和组件的朋友是很有帮助的. 系 ...
最新文章
- iOS中UITextField 使用全面解析
- Eclipse + Spring boot +mybatis + mysql
- am5728 是否支持aarch64_am5728开启uart0接口通讯
- 博弈——通过博弈思想解决的问题(hdu1847,2147)
- 心酸!苹果自研5G芯片最快2022年推出 首款5G iPhone还得靠高通
- iOS中如何旋转UIView
- [other] 代码量代码复杂度统计-lizard
- HDU - 3506 Monkey Party
- BXP千兆无盘网吧解决方案(转)
- 三十正青春!苏宁818要用“好服务”抢占年轻用户心智
- 数字电子技术基础知识点总结_数字电子技术课程线上授课一周之感想 ——信息与控制学院 电工基础教研室 李姿...
- 月销13485台的理想ONE,到底做对了哪些事儿?
- linux更换浏览器,ubuntu 默认浏览器换Chrome 是正确的选择
- 手游平台系统搭建sdk服务端接口文档
- 算法的时间复杂度排序
- 用Excel做一次数据分析(二)——一次简单的分析
- 四扫客户接口——接口测试完成文档
- 什么是正态分布?二八法则又是什么?
- fpga实操训练(从模块到系统开发)
- 关于深度学习理论和架构的最新综述(part3)
热门文章
- 东航期货行情接口和交易接口(20190509)
- 克罗谈投资策略05_涨势买入,跌势卖出
- java程序组成_java程序是由什么组成的
- mysql特定格式导出数据_MySQL 将表数据以特定格式的文本导出与导入
- 如何用研发流程搞垮一个团队?
- commit git 删除文件夹_Git-git删除文件夹/文件(删除/不删除本地文件/文件夹)
- python合并文件夹下的文件_Python实现合并同一个文件夹下所有txt文件的方法示例...
- oracle+gsm安装,针对Oracle的10G版本提升SCOTT为DBA脚本!
- LeetCode Week 4:第 31 ~ 40 题
- PyTorch报错“/.../Loss.cu: ... [59,0,0] Assertion input_val >= zero input_val <= one failed.”