终于讲到排序了,这一部分应该说还是比较好理解的。

jqGrid通过colModel选项中的sortable来控制是否可以以某列的值排序。sortable的默认值是true,当设为false时,即此列不能用于排序。

[javascript] view plaincopy
  1. $(function(){
  2. $("#gridTable").jqGrid({
  3. ...
  4. colModel: [
  5. {name:"id",index:"id",label:"编码",width:40},
  6. {name:"lastName",index:"lastName",label:"姓",width:80},
  7. {name:"firstName",index:"firstName",label:"名",width:80},
  8. {name:"email",index:"email",label:"电子邮箱",width:160,sortable:false},
  9. {name:"telNo",index:"telNo",label:"电话",width:120,sortable:false}
  10. ],
  11. ...
  12. });
  13. });

当点击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的代码:

[java] view plaincopy
  1. package cn.gengv.struts2ex.jqGrid;
  2. import java.text.Collator;
  3. import java.util.Comparator;
  4. import com.byzl.hare.model.Contact;
  5. public class ContactComparatorFactory {
  6. private static Collator collator_Chinese = Collator.getInstance(java.util.Locale.CHINA);
  7. private final static Comparator<Contact> idComparator = new IdComparator();
  8. private final static Comparator<Contact> firstNameComparator = new FirstNameComparator();
  9. private final static Comparator<Contact> lastNameComparator = new LastNameComparator();
  10. private final static Comparator<Contact> fullNameComparator = new FullNameComparator();
  11. private final static Comparator<Contact> idCardNoNoComparator = new IdCardNoComparator();
  12. private final static Comparator<Contact> nationalityComparator = new NationalityComparator();
  13. public static Comparator<Contact> getComparator(String compareType) {
  14. if ("id".equalsIgnoreCase(compareType)) {
  15. return idComparator;
  16. } else if ("firstName".equalsIgnoreCase(compareType)) {
  17. return firstNameComparator;
  18. } else if ("lastName".equalsIgnoreCase(compareType)) {
  19. return lastNameComparator;
  20. } else if ("fullName".equalsIgnoreCase(compareType)) {
  21. return fullNameComparator;
  22. } else if ("idCardNoNo".equalsIgnoreCase(compareType)) {
  23. return idCardNoNoComparator;
  24. } else if ("nationality".equalsIgnoreCase(compareType)) {
  25. return nationalityComparator;
  26. } else {
  27. return null;
  28. }
  29. }
  30. public static class IdComparator implements Comparator<Contact> {
  31. public int compare(Contact c1, Contact c2) {
  32. if (c1 == null && c2 == null) {
  33. return 0;
  34. } else if (c1 == null && c2 != null) {
  35. return -1;
  36. } else if (c1 != null && c2 == null) {
  37. return 1;
  38. } else {
  39. int id1 = c1.getId();
  40. int id2 = c2.getId();
  41. return id1 == id2 ? 0 : (id1 < id2 ? -1 : 1);
  42. }
  43. }
  44. }
  45. public static class FirstNameComparator implements Comparator<Contact> {
  46. public int compare(Contact c1, Contact c2) {
  47. if (c1 == null && c2 == null) {
  48. return 0;
  49. } else if (c1 == null && c2 != null) {
  50. return -1;
  51. } else if (c1 != null && c2 == null) {
  52. return 1;
  53. } else {
  54. String s1 = c1.getFirstName();
  55. String s2 = c2.getFirstName();
  56. if (s1 == null && s2 == null) {
  57. return 0;
  58. } else if (s1 == null && s2 != null) {
  59. return -1;
  60. } else if (s1 != null && s2 == null) {
  61. return 1;
  62. } else {
  63. return collator_Chinese.compare(s1, s2);
  64. }
  65. }
  66. }
  67. }
  68. public static class LastNameComparator implements Comparator<Contact> {
  69. public int compare(Contact c1, Contact c2) {
  70. if (c1 == null && c2 == null) {
  71. return 0;
  72. } else if (c1 == null && c2 != null) {
  73. return -1;
  74. } else if (c1 != null && c2 == null) {
  75. return 1;
  76. } else {
  77. String s1 = c1.getLastName();
  78. String s2 = c2.getLastName();
  79. if (s1 == null && s2 == null) {
  80. return 0;
  81. } else if (s1 == null && s2 != null) {
  82. return -1;
  83. } else if (s1 != null && s2 == null) {
  84. return 1;
  85. } else {
  86. return collator_Chinese.compare(s1, s2);
  87. }
  88. }
  89. }
  90. }
  91. public static class FullNameComparator implements Comparator<Contact> {
  92. public int compare(Contact c1, Contact c2) {
  93. if (c1 == null && c2 == null) {
  94. return 0;
  95. } else if (c1 == null && c2 != null) {
  96. return -1;
  97. } else if (c1 != null && c2 == null) {
  98. return 1;
  99. } else {
  100. String s1 = c1.getFullName();
  101. String s2 = c2.getFullName();
  102. if (s1 == null && s2 == null) {
  103. return 0;
  104. } else if (s1 == null && s2 != null) {
  105. return -1;
  106. } else if (s1 != null && s2 == null) {
  107. return 1;
  108. } else {
  109. return collator_Chinese.compare(s1, s2);
  110. }
  111. }
  112. }
  113. }
  114. public static class IdCardNoComparator implements Comparator<Contact> {
  115. public int compare(Contact c1, Contact c2) {
  116. if (c1 == null && c2 == null) {
  117. return 0;
  118. } else if (c1 == null && c2 != null) {
  119. return -1;
  120. } else if (c1 != null && c2 == null) {
  121. return 1;
  122. } else {
  123. String s1 = c1.getIdCardNo();
  124. String s2 = c2.getIdCardNo();
  125. if (s1 == null && s2 == null) {
  126. return 0;
  127. } else if (s1 == null && s2 != null) {
  128. return -1;
  129. } else if (s1 != null && s2 == null) {
  130. return 1;
  131. } else {
  132. return s1.compareToIgnoreCase(s2);
  133. }
  134. }
  135. }
  136. }
  137. public static class NationalityComparator implements Comparator<Contact> {
  138. public int compare(Contact c1, Contact c2) {
  139. if (c1 == null && c2 == null) {
  140. return 0;
  141. } else if (c1 == null && c2 != null) {
  142. return -1;
  143. } else if (c1 != null && c2 == null) {
  144. return 1;
  145. } else {
  146. String s1 = c1.getNationality();
  147. String s2 = c2.getNationality();
  148. if (s1 == null && s2 == null) {
  149. return 0;
  150. } else if (s1 == null && s2 != null) {
  151. return -1;
  152. } else if (s1 != null && s2 == null) {
  153. return 1;
  154. } else {
  155. return collator_Chinese.compare(s1, s2);
  156. }
  157. }
  158. }
  159. }
  160. }

然后再来看JqGridBaseAction,其中添加了一个抽象方法,用来将数据结果进行排序。

[java] view plaincopy
  1. package cn.gengv.struts2ex.jqGrid;
  2. // import ...
  3. @SuppressWarnings("serial")
  4. public abstract class JqGridBaseAction<T> extends ActionSupport {
  5. ...
  6. // (1)添加排序方法
  7. public abstract void sortResults(List<T> results, String field, String order);
  8. public String refreshGridModel() {
  9. try {
  10. List<Criterion> criteria = Collections.emptyList();
  11. if(search == true) {
  12. criteria = new ArrayList<Criterion>();
  13. if(filters != null && filters.length()>0) {
  14. criteria.addAll(this.generateSearchCriteriaFromFilters(filters));
  15. }
  16. Criterion criterion = this.generateSearchCriterion(searchField, searchString, searchOper);
  17. if(criterion != null) {
  18. criteria.add(criterion);
  19. }
  20. }
  21. List<T> results = Collections.emptyList();
  22. int from = rows * (page - 1);
  23. int length = rows;
  24. if(loadonce) {
  25. from = 0;
  26. length = 100;
  27. }
  28. if(!criteria.isEmpty()) {
  29. record = this.getResultSize(criteria);
  30. results = this.listResults(criteria, from, length);
  31. } else {
  32. record = this.getResultSize();
  33. results = this.listResults(from, length);
  34. }
  35. // (2)将结果排序
  36. if(sidx != null && sord != null) {
  37. sortResults(results, sidx, sord);
  38. }
  39. this.setGridModel(results);
  40. total = (int) Math.ceil((double) record / (double) rows);
  41. return SUCCESS;
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. this.addActionError(e.getMessage());
  45. return ERROR;
  46. }
  47. }
  48. ...
  49. }

而在ListContactsAction中提供了方法实现:

[java] view plaincopy
  1. package cn.gengv.struts2ex.jqGrid;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. import com.byzl.hare.dao.impl.Criterion;
  6. import com.byzl.hare.model.Contact;
  7. import com.byzl.hare.service.ContactService;
  8. @SuppressWarnings("serial")
  9. public class ListContactsAction extends JqGridBaseAction<Contact> {
  10. ...
  11. @Override
  12. public void sortResults(List<Contact> results, String field, String order) {
  13. // (1)根据field获得对应的Comparator
  14. Comparator<Contact> comparator = ContactComparatorFactory.getComparator(field);
  15. if(comparator != null) {
  16. // (2)使用Comparator排序
  17. Collections.sort(results, comparator);
  18. // (3)如果需要的排序顺序为desc,则颠倒顺序
  19. if("desc".equals(order)) {
  20. Collections.reverse(results);
  21. }
  22. }
  23. }
  24. ...
  25. }

不过这个例子存在一定的局限性,即只能将当前页中的数据根据某列进行排序;而不能跨页间进行数据排序。之所以存在这种局限,也是源于实际应用中的客观限制。还以这个例子来说,数据库里总共模拟了两万多条数据记录。如果每次要将这些记录进行排里的话,除非有数据库索引支持,否则所要消耗的时间也是相当客观的,对于用户体验来说,几乎就是灾难。如果数据量更多的话,结果可想而知。

因此,我们应该换一个角度来看这个问题,用户之所以使用排序,更多的目的还是在于查找数据方便,既然我们可以提供条件查询(尤其是复杂条件查询),那么用户对于排序的需求也就不会那么迫切了。同时也可以体会到,排序更多地应用在少量数据的场合下。

转载于:https://www.cnblogs.com/tware-dsy/archive/2013/02/26/2933659.html

jqGrid与Struts2的结合应用(七) —— 浅谈排序相关推荐

  1. Python进阶(七)浅谈python3和Python2的区别

    文章目录 一.前言 二.print函数 2.1 举例 三.reduce()函数 四.try except 五.打开文件 六.从键盘录入一个字符串 七.整形除法自动转为float 八.新的字符串格式化方 ...

  2. 浅谈排序算法:冒泡排序法和选择排序法的区别

    之前学习了冒泡排序法和选择排序法,最近被老师问某个道题用的是什么排序法.自己居然答不出来,才发现自己没有真正弄懂,这两个算法的原理和区别,所以····· 1冒泡排序法 1.1什么是冒泡排序法? 顾名思 ...

  3. 浅谈struts2之chain

    前一段时间,有关chain的机制着实困绕了许久.尽管网上有许多关于chain的解说,但要不是只谈大理论,不结合实例:要不就是只有示例,没有挖出示例背后的意义.    先解释下chain吧: Chain ...

  4. catia三维轴承_浅谈基于CATIA二次开发的单排四点接触球轴承三维设计论文

    浅谈基于CATIA二次开发的单排四点接触球轴承三维设计论文 一.概述 单排四点接触球转盘轴承是一种能够同时承受较大轴向负荷.径向负荷和倾覆力矩等综合载荷,集支承.旋转.传动.固定等多种功能于一身的特殊 ...

  5. 浅谈大数据中的 2PC、3PC、Paxos、Raft、ZAB

    一致性 简述 一致性,是指对每个节点一个数据的更新,整个集群都知道更新,并且是一致的.假设一个具有N个节点的分布式系统,当其满足以下条件时,我们说这个系统满足一致性: 全认同: 所有N个节点都认同一个 ...

  6. 浅谈(线性)卷积公式为什么要翻转

    浅谈(线性)卷积公式为什么要翻转 信号系统 信号处理 卷积 文章题目之所以写 (线性)卷积,是因为卷积有很多种,如循环卷积,周期卷积以及线性卷积.本文主要讨论线性卷积,为书写方便,下文都以卷积代替.至 ...

  7. 沈向洋:浅谈人工智能创造

    来源:AI科技评论 本文约7000字,建议阅读10分钟 本文为你介绍沈向洋的<浅谈人工智能创造>,分享过去六年其在微软小冰身上得到的一些实践想法. 2020年9月21日上午9点,由北京大学 ...

  8. 浅谈ASP.NET的内部机制(一)

    浅谈ASP.NET的内部机制(一) 前言:当一个Http请求发送给一个aspx页面时,服务器进行了哪些操作?又如何来解析这个请求?ASP.NET在接收请求后是怎么运行的,如怎么编译以及怎么样用托管的代 ...

  9. 浅谈ASP.NET内部机制(五)

    浅谈ASP.NET内部机制(五) 前言:本章要谈页面生命周期了,过程挺多的,但是一点都不难.不信可以看看.我尽量的讲的平实一些,而且理解页面的生命周期对喜欢开发自定义控件和组件的朋友是很有帮助的. 系 ...

最新文章

  1. iOS中UITextField 使用全面解析
  2. Eclipse + Spring boot +mybatis + mysql
  3. am5728 是否支持aarch64_am5728开启uart0接口通讯
  4. 博弈——通过博弈思想解决的问题(hdu1847,2147)
  5. 心酸!苹果自研5G芯片最快2022年推出 首款5G iPhone还得靠高通
  6. iOS中如何旋转UIView
  7. [other] 代码量代码复杂度统计-lizard
  8. HDU - 3506 Monkey Party
  9. BXP千兆无盘网吧解决方案(转)
  10. 三十正青春!苏宁818要用“好服务”抢占年轻用户心智
  11. 数字电子技术基础知识点总结_数字电子技术课程线上授课一周之感想 ——信息与控制学院 电工基础教研室 李姿...
  12. 月销13485台的理想ONE,到底做对了哪些事儿?
  13. linux更换浏览器,ubuntu 默认浏览器换Chrome 是正确的选择
  14. 手游平台系统搭建sdk服务端接口文档
  15. 算法的时间复杂度排序
  16. 用Excel做一次数据分析(二)——一次简单的分析
  17. 四扫客户接口——接口测试完成文档
  18. 什么是正态分布?二八法则又是什么?
  19. fpga实操训练(从模块到系统开发)
  20. 关于深度学习理论和架构的最新综述(part3)

热门文章

  1. 东航期货行情接口和交易接口(20190509)
  2. 克罗谈投资策略05_涨势买入,跌势卖出
  3. java程序组成_java程序是由什么组成的
  4. mysql特定格式导出数据_MySQL 将表数据以特定格式的文本导出与导入
  5. 如何用研发流程搞垮一个团队?
  6. commit git 删除文件夹_Git-git删除文件夹/文件(删除/不删除本地文件/文件夹)
  7. python合并文件夹下的文件_Python实现合并同一个文件夹下所有txt文件的方法示例...
  8. oracle+gsm安装,针对Oracle的10G版本提升SCOTT为DBA脚本!
  9. LeetCode Week 4:第 31 ~ 40 题
  10. PyTorch报错“/.../Loss.cu: ... [59,0,0] Assertion input_val >= zero input_val <= one failed.”