`
loky
  • 浏览: 180955 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

TableSorter

阅读更多
package com.zpec.utils;


import java.text.Collator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDAbstractDropDownByIndex;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDAbstractDropDownByKey;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDAbstractInputField;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDAbstractTableColumn;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDCaption;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDCheckBox;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDLink;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDProgressIndicator;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDRadioButton;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTable;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTableCellEditor;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTableColumn;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTableColumnGroup;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTextEdit;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTextView;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.WDTableColumnSortDirection;
import com.sap.tc.webdynpro.progmodel.api.IWDAction;
import com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent;
import com.sap.tc.webdynpro.progmodel.api.IWDNode;
import com.sap.tc.webdynpro.progmodel.api.IWDNodeElement;
import com.sap.tc.webdynpro.progmodel.api.IWDViewElement;
import com.sap.tc.webdynpro.services.sal.localization.api.WDResourceHandler;

/**
* Helper class that makes a Web Dynpro table UI element sortable (column-wise).
*/
public final class TableSorter {
/**
* @param table
* @param sortAction
* @param comparators
*/

/**
* Creates a table sorter for the given table using the given sort action.
* This constructor must be called from <code>wdDoModifyView()</code>, but
* usually only when that hook is called for the first time. Store the newly
* created instance in a context attribute with Java native type
* <code>com.sap.tc.webdynpro.tests.utils.TableSorter</code>.
* The given sort action's event handler will be bound to the <code>onSort</code>
* event of the table and must at least call this table sorter's
* <code>sort(wdEvent)</code> method.
*
* Every column of the table is made sortable if possible according to the
* following rules.
* If a comparator is given for a column's ID and it is a
* <code>NodeElementByAttributeComparator</code>, then that comparator defines
* both the attribute and the ordering used to sort that column.
* If any other comparator is given and an attribute can be determined from
* that column's table cell editor, then that attribute is used to sort that
* column according to the ordering imposed by the given comparator.
* If no comparator is given but an attribute can be determined from
* that column's table cell editor, then that attribute is used to sort that
* column according to the natural ordering of that attribute's type.
* Else that column is left untouched.
*
* Additionally it is possible to define the sortable columns by their
* TableColumn UI element ids.
*
* @see sort()
* @see NodeElementByAttributeComparator
* @see com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTable
*/
public TableSorter(IWDTable table, IWDAction sortAction, Map comparators) {
init(table, sortAction, comparators, null);
}

public TableSorter(IWDTable table, IWDAction sortAction, Map comparators, String[] sortableColumns) {
init(table, sortAction, comparators, sortableColumns);
}

/**
* Initialisation stuff
*/
private void init(IWDTable table, IWDAction sortAction, Map comparators, String[] sortableColumns){
this.table = table;
if(sortableColumns == null){
sortableCols = null;
}else{
sortableCols = new HashMap();
for (int i = 0; i < sortableColumns.length; i++) {
sortableCols.put(sortableColumns[i], sortableColumns[i]);
}
}

// sanity checks
if (sortAction == null)
throw new IllegalArgumentException("Sort action must be given");
if (table == null)
throw new IllegalArgumentException("Table must be given");
if (table.bindingOfDataSource() == null)
throw new IllegalArgumentException(
"Data source of table with id '" + table.getId() + "' must be bound");

// make the columns sortable
String dataSourcePrefix = table.bindingOfDataSource() + ".";
//TODO: remove the following line since this method is not longer available in later releases
setComparatorsForColumns(dataSourcePrefix, table.iterateColumns(), comparators);
setComparatorsForColumns(dataSourcePrefix, table.iterateGroupedColumns(), comparators);

//set up the table properties
table.setOnSort(sortAction);
table.mappingOfOnSort().addSourceMapping(IWDTable.IWDOnSort.COL, "selectedColumn");
table.mappingOfOnSort().addSourceMapping(IWDTable.IWDOnSort.DIRECTION, "sortDirection");
}

/**
* Try to make the given columns sortable (recusivly, if necessary)
*/
private void setComparatorsForColumns(String dataSourcePrefix, Iterator columnIterator, Map comparators){
int index = 0;
for (Iterator it = columnIterator; it.hasNext(); ++index) { // for every column: try to make it bindable
IWDAbstractTableColumn abstractColumn = (IWDAbstractTableColumn) it.next();
if(abstractColumn instanceof IWDTableColumn){

IWDTableColumn column = (IWDTableColumn)abstractColumn;
if(sortableCols == null || sortableCols.containsKey(column.getId())){
//try to make this column sortable
Comparator comparator = null;
if (comparators != null){
comparator = (Comparator)comparators.get(column.getId());
}

NodeElementByAttributeComparator elementComparator = null;
if (comparator instanceof NodeElementByAttributeComparator) {
// the easy one, attribute and ordering are given
elementComparator = (NodeElementByAttributeComparator)comparator;
} else { // attribute must be determined
String bindingOfPrimaryProperty = bindingOfPrimaryProperty(column.getTableCellEditor());
if (bindingOfPrimaryProperty == null || !bindingOfPrimaryProperty.startsWith(dataSourcePrefix)){
//no attribute found or outside of data source
column.setSortState(WDTableColumnSortDirection.NOT_SORTABLE);
continue;
}
String attributeName = bindingOfPrimaryProperty.substring(dataSourcePrefix.length());
Collection subnodes = new ArrayList();
if (attributeName.indexOf('.') >= 0){
//attribute not immediately below data source
String[] tokens = tokenize (attributeName, ".");
for(int i=0; i<tokens.length-1; i++){
subnodes.add(tokens[i]);
}
attributeName = tokens[tokens.length-1];
}
if(subnodes.size() == 0){
elementComparator = new NodeElementByAttributeComparator(attributeName, comparator);
}else{
elementComparator = new NodeElementByAttributeComparator(attributeName, comparator, subnodes);
}
}

// set up internal data structures
comparatorForColumn.put(column, elementComparator);

//set sort state
column.setSortState(WDTableColumnSortDirection.NONE);
}else{
//column should not be sortable
column.setSortState(WDTableColumnSortDirection.NOT_SORTABLE);
}

}else if (abstractColumn instanceof IWDTableColumnGroup){
//it's just a column group -> try to bind the columns of the column group
IWDTableColumnGroup columnGroup = (IWDTableColumnGroup)abstractColumn;
setComparatorsForColumns(dataSourcePrefix, columnGroup.iterateColumns(), comparators);
}

}
}

/**
* Tokenizes the input string according to the given delimiters. The delimiters will be left out.
* Example: tokenize("Hello_World", "_") results ["Hello", "World"]
*/
private String[] tokenize (String input, String delim){
StringTokenizer tokenizer = new StringTokenizer(input, delim);
String[] tokens = new String[tokenizer.countTokens()];
int index = 0;
while(tokenizer.hasMoreTokens()){
tokens[index] = tokenizer.nextToken();
index++;
}
return tokens;
}

/**
* This method must be called from the event handler of this table sorter's
* sort action. It performs the actual sort operation.
*/
public void sort(IWDCustomEvent wdEvent, IWDNode dataSource) {
// find the things we need
String columnId = wdEvent.getString("selectedColumn");
String direction = wdEvent.getString("sortDirection");
IWDTableColumn column = (IWDTableColumn) table.getView().getElement(columnId);
NodeElementByAttributeComparator elementComparator = (NodeElementByAttributeComparator) comparatorForColumn.get(column);

if (elementComparator == null){
//not a sortable column
column.setSortState(WDTableColumnSortDirection.NOT_SORTABLE);
return;
}

// sorting
elementComparator.setSortDirection(WDTableColumnSortDirection.valueOf(direction));
dataSource.sortElements(elementComparator);
}

/**
* Returns the binding of the given table cell editor's property that is
* considered "primary" or <code>null</code> if no such binding exists or no
* such property can be determined.
*/
private static final String bindingOfPrimaryProperty(IWDTableCellEditor editor) {
return editor instanceof IWDViewElement ? bindingOfPrimaryProperty((IWDViewElement) editor) : null;
}

/**
* Returns the binding of the given view element's property that is
* considered "primary" or <code>null</code> if no such binding exists or no
* such property can be determined.
*/
private static final String bindingOfPrimaryProperty(IWDViewElement element) {
if (element instanceof IWDAbstractDropDownByIndex)
return ((IWDAbstractDropDownByIndex) element).bindingOfTexts();
if (element instanceof IWDAbstractDropDownByKey)
return ((IWDAbstractDropDownByKey) element).bindingOfSelectedKey();
if (element instanceof IWDAbstractInputField)
return ((IWDAbstractInputField) element).bindingOfValue();
if (element instanceof IWDCaption)
return ((IWDCaption) element).bindingOfText();
if (element instanceof IWDCheckBox)
return ((IWDCheckBox) element).bindingOfChecked();
if (element instanceof IWDLink)
return ((IWDLink) element).bindingOfText();
if (element instanceof IWDProgressIndicator)
return ((IWDProgressIndicator) element).bindingOfPercentValue();
if (element instanceof IWDRadioButton)
return ((IWDRadioButton) element).bindingOfSelectedKey();
if (element instanceof IWDTextEdit)
return ((IWDTextEdit) element).bindingOfValue();
if (element instanceof IWDTextView)
return ((IWDTextView) element).bindingOfText();

return null;
}

/**
* Instance of a comparator according to the ordering imposed by the
* implementation of <code>Comparable</code>.
*/
private static final Comparator DEFAULT = new Comparator() {
/**
* Compares the given objects according to the ordering imposed by the first
* ones <code>compareTo(Object)</code> function. Furthermore, <code>null</code>
* is treated to be less than any object.
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(Object o1, Object o2) {
if (o1 == null && o2 == null)
return 0;
if (o1 == null)
return -1;
if (o2 == null)
return +1;
if (o1 instanceof Boolean && o2 instanceof Boolean)
return o1.toString().compareTo(o2.toString()); // false < true
if (o1 instanceof String && o2 instanceof String){
//Use a Collator for sorting according to the given Locale
Collator collate = Collator.getInstance(WDResourceHandler.getCurrentSessionLocale());
return collate.compare(o1, o2);
}
return ((Comparable) o1).compareTo((Comparable) o2);
}
};

/**
* Map of table column to comparator (<code>ReversableComparator</code>)
* used for sorting that column (sortable columns only).
*/
private Map comparatorForColumn = new HashMap();

/**
* The table to be sorted.
*/
private IWDTable table = null;

/**
* Column-IDs of the columns, which should be sortable
*/
private Map sortableCols = null;

/**
* Generic comparator that compares node elements by a given attribute with
* the help of a given comparator.
*/
public final class NodeElementByAttributeComparator implements Comparator {


/**
* Creates a new comparator for the given attribute name that compares values
* of that attribute according to the natural ordering of that attribute's
* type (which must implement <code>java.lang.Comparable</code>).
*/
public NodeElementByAttributeComparator(String attributeName) {
this(attributeName, null, false, new ArrayList());
}

/**
* Creates a new comparator for the given attribute name that compares values
* of that attribute with the help of the given comparator. If no comparator
* is given, the natural ordering of that attribute's type is used.
*/
public NodeElementByAttributeComparator(String attributeName, Comparator comparator) {
this(attributeName, comparator, false, new ArrayList());
}

/**
* Creates a new comparator for the given attribute name that compares values
* of that attribute either as objects (i.e. "in internal format") or as text
* (i.e. "in external format") as indicated. The ordering is the natural
* ordering of that attribute's type (which must implement
* <code>java.lang.Comparable</code>) in case objects are compared or the
* natural ordering of <code>java.lang.String</code> in case texts are compared.
*/
public NodeElementByAttributeComparator(String attributeName, boolean compareAsText) {
this(attributeName, null, compareAsText, new ArrayList());
}

/**
* Creates a new comparator for the given attribute name that compares values
* of that attribute according to the natural ordering of that attribute's
* type (which must implement <code>java.lang.Comparable</code>). In addition it is possible
* to define the path to a child node with the <code>java.util.Collection</code> subnodes.
* (List of child node names in the correct order)
*/
public NodeElementByAttributeComparator(String attributeName, Collection subnodes) {
this(attributeName, null, false, subnodes);
}

/**
* Creates a new comparator for the given attribute name that compares values
* of that attribute with the help of the given comparator. If no comparator
* is given, the natural ordering of that attribute's type is used. In addition it is possible
* to define the path to a child node with the <code>java.util.Collection</code> subnodes.
* (List of child node names in the correct order)
*/
public NodeElementByAttributeComparator(String attributeName, Comparator comparator, Collection subnodes) {
this(attributeName, comparator, false, subnodes);
}

/**
* Creates a new comparator for the given attribute name that compares values
* of that attribute either as objects (i.e. "in internal format") or as text
* (i.e. "in external format") as indicated. The ordering is the natural
* ordering of that attribute's type (which must implement
* <code>java.lang.Comparable</code>) in case objects are compared or the
* natural ordering of <code>java.lang.String</code> in case texts are compared. In addition it is possible
* to define the path to a child node with the <code>java.util.Collection</code> subnodes.
* (List of child node names in the correct order)
*/
public NodeElementByAttributeComparator(String attributeName, boolean compareAsText, Collection subnodes) {
this(attributeName, null, compareAsText, subnodes);
}

/**
* Internal constructor.
*/
private NodeElementByAttributeComparator(
String attributeName,
Comparator comparator,
boolean compareAsText,
Collection subNodes) {
if (attributeName == null)
throw new IllegalArgumentException("Attribute name must not be null");
if (comparator == null)
comparator = DEFAULT;

this.attributeName = attributeName;
this.comparator = comparator;
this.compareAsText = compareAsText;
this.sortDirection = true;
this.subNodes = subNodes;
}

/**
* Sets the sort direction of this comparator to the given direction. The comparator sort in ascending order by default.
* @see com.sap.tc.webdynpro.clientserver.uielib.standard.api.WDTableColumnSortDirection
*/
public void setSortDirection(WDTableColumnSortDirection direction){
if(direction.equals(WDTableColumnSortDirection.UP)){
sortDirection = true;
}else if(direction.equals(WDTableColumnSortDirection.DOWN)){
sortDirection = false;
}
}

/**
* Compares the given objects which must be instances of <code>IWDNodeElement</code>
* according to the values of the attribute given at construction time
* with the help of the comparator given at construction time.
*
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
* @see com.sap.tc.webdynpro.progmodel.api.IWDNodeElement
*/
public int compare(Object o1, Object o2) {
IWDNodeElement element1 = (IWDNodeElement) o1;
IWDNodeElement element2 = (IWDNodeElement) o2;
if(subNodes.size() > 0){
element1 = getSubNodeElement(element1, 0);
element2 = getSubNodeElement(element2, 0);
}
Object attributeValue1 = null;
Object attributeValue2 = null;
if(element1 != null){
attributeValue1 =
compareAsText
? element1.getAttributeAsText(attributeName)
: element1.getAttributeValue(attributeName);
}
if(element2 != null){
attributeValue2 =
compareAsText
? element2.getAttributeAsText(attributeName)
: element2.getAttributeValue(attributeName);
}

if(sortDirection){
return comparator.compare(attributeValue1, attributeValue2);
}else{
return comparator.compare(attributeValue2, attributeValue1);
}
}

/**
* Determines recursivly the child node, which have an attribute with the given name.
* The path to this child node must be specified in the subnodes property of this comparator.
* Start this method with index = 0.
*/
private IWDNodeElement getSubNodeElement(IWDNodeElement currentElement, int index){
if(currentElement == null || index >= subNodes.size()){
//end of recursion
return currentElement;
}else{
return getSubNodeElement(currentElement.node().getChildNode((String)subNodes.toArray()[index], currentElement.index()).getCurrentElement(), index+1);
//return getSubNodeElement(currentElement.node().getChildNode((String)subNodes.toArray()[index], currentElement.index()).getElementAt(0), index+1);
}
}

/**
* Name of the attribute used for comparisons.
*/
private final String attributeName;

/**
* Comparator used for comparing the attribute's values.
*/
private final Comparator comparator;

/**
* Indicates whether attribute values are compared as text (as opposed to
* "as objects").
*/
private final boolean compareAsText;

/**
* Sort direction (true = ascending order, false = descending order)
*/
private boolean sortDirection;

/**
* List of child node names
* (Description of the path from the given context node to the specified attribute)
*/
private Collection subNodes;
}

}
分享到:
评论

相关推荐

    tablesorter页面表格排序工具

    《tablesorter:高效实现页面表格排序的前端利器》 在网页开发中,表格数据的展示和管理是一项常见的任务,而对表格数据进行排序则能极大地提升用户体验。tablesorter是一款强大的JavaScript插件,专为网页表格提供...

    jquery插件tablesorter例子

    **jQuery 插件 Tablesorter 详解** Tablesorter 是一款功能强大的 jQuery 插件,专为HTML表格提供排序功能。它允许用户通过点击表头轻松地对表格数据进行升序或降序排序,极大地提升了网页中数据展示的交互性和用户...

    Jquery中的tablesorter用法

    **jQuery中的tablesorter插件详解** jQuery是一个广泛使用的JavaScript库,它简化了HTML文档遍历、事件处理、动画和Ajax交互。而tablesorter则是jQuery的一个插件,专门用于增强HTML表格的功能,使得用户可以方便地...

    jquery.tablesorter.js +排序、分页、ajax demo

    《jQuery表格排序插件tablesorter的深度解析与实践应用》 在Web开发中,数据展示和管理是一项重要任务,而表格是常见的数据承载形式。jQuery的tablesorter插件为开发者提供了一种便捷的方式,使得表格数据可以实现...

    jQuery 插件 tablesorter 的使用

    接下来,我们将深入探讨tablesorter的使用方法、特性以及实际应用。 首先,安装tablesorter非常简单。如果你的项目已经包含了jQuery库,只需要在HTML文件中引入tablesorter的CSS和JavaScript文件。可以从官方网站...

    tablesorter+行高亮+列高亮

    在提供的压缩包中,可能包含的文件有`tablesorter.js`(`tablesorter`库的核心代码)、`custom.js`(用户自定义的扩展,包括行高亮和列高亮的实现)、以及可能的CSS文件(如`styles.css`,包含了高亮样式的定义)。...

    jquery 可自定义日期排序的tablesorter

    《jQuery实现可自定义日期排序的Tablesorter详解》 在网页开发中,表格数据的展示和排序是一项常见的需求。jQuery的Tablesorter插件提供了一种强大的解决方案,特别是对于日期排序,它允许开发者根据特定格式自定义...

    表格分页显示--tablesorter

    《表格分页显示技术——tablesorter深度解析》 在数据量庞大的网页应用中,高效地展示和管理数据是至关重要的。Tablesorter是一个强大的JavaScript库,它为HTML表格提供了排序、过滤和分页功能,使得用户能够更方便...

    jquery tablesorter,表格排序带分页

    《jQuery TableSorter:实现高效表格排序与分页功能》 在网页开发中,数据的展示往往离不开表格,而对表格进行排序和分页则能极大地提升用户体验。jQuery TableSorter是一款强大的JavaScript插件,它使得表格排序和...

    jquery表格插件Tablesorter插件 表格内容筛选与分页筛选

    本文将深入探讨Tablesorter插件的核心特性以及如何实现表格内容筛选与分页筛选。 **1. Tablesorter插件介绍** Tablesorter是一款轻量级的JavaScript库,它可以自动检测表格中的数据类型并进行相应的排序。用户只需...

    jquery.tablesorter js排序 页面物理排序

    首先,我们要理解`jQuery.tablesorter`的基本用法。它依赖于jQuery库,所以在使用之前需要确保页面已经引入了jQuery。接着,通过在HTML中引用`jquery.tablesorter.js`文件,我们可以初始化这个插件。例如: ```html...

    jquery.tablesorter.min.js

    jquery table排序插件 tablesorter

    jquery_tablesorter.js

    `jQuery Tablesorter` 是一个基于 `jQuery` 的开源插件,主要用于表格数据的排序,提供了用户友好的交互式表格排序功能。这个插件能够轻松地将任何HTML表格转化为可排序的状态,支持多种排序方式,如升序、降序以及...

    tablesorter-master表格排序.zip

    《表格排序神器:tablesorter-master插件深度解析》 在网页开发中,表格数据的展示和排序是一项常见的需求。为了提升用户体验,许多开发者选择使用JavaScript库或插件来实现这一功能。今天我们要深入探讨的是一款名...

    tablesorter插件

    在网页设计和开发中,tablesorter是提升用户体验的利器,尤其适用于数据密集型应用。 ### 1. 插件核心功能 - **排序**:tablesorter允许用户点击表格的列头进行升序或降序排序。它支持多种类型的排序,包括数字、...

    jquery tablesorter对表格进行排序

    《jQuery Tablesorter:高效实现表格排序》 在网页开发中,数据的展示往往离不开表格。当表格数据较多时,用户通常需要对数据进行排序,以便快速查找或分析信息。jQuery Tablesorter 是一个强大的开源插件,它使得...

    tablesorter.rar

    TableSorter是一个流行的数据排序和过滤插件,它允许用户轻松地对网页上的表格进行升序或降序排序,并支持自定义排序规则和实时查询功能。 **TableSorter 知识点详解** 1. **基本功能**: TableSorter 提供了对...

    jquery 表格插件:Tablesorter 2.0 插件 表格用户体验内容筛选与分页筛选

    **Tablesorter 2.0 插件详解:提升表格数据管理体验** Tablesorter 2.0 是一个强大的 jQuery 插件,专为改善网页中的表格用户体验而设计。它提供了一套完整的解决方案,用于对表格数据进行排序、筛选和分页,极大地...

    tablesorter例子

    通常,这会包括`jquery.tablesorter.js`(主要的tablesorter脚本)和可能的附加模块,如`jquery.tablesorter.widgets.js`(包含额外的插件和小部件)。如果在描述中提到的"js文件绝对能用",那么这些文件应能正常...

    jquery.tablesorter.js

    jquery.tablesorter.js jquery 排序 很不错的排序

Global site tag (gtag.js) - Google Analytics