给表格添加排序功能

   在日常工作中,我们在使用表格浏览数据的时候,希望数据能按照一定的规则进行排序,这样查看起来会非常的方便,这时我们可以在表格中加入排序的功能,这样表格中的内容便可以按照一定的规则进行排序,下面我们来看看怎么给表格加上排序的功能,其实这个功能实现起来很简单,下面我们来看看怎么实现,
  实现我们新建一个实现了 ViewerSorter 的类,比如名称为 UserInfoSorter .java
我们只有实现 compare 方便就可以了,其中这里的返回值为int型,返回类型一般有三种-1、0和1,其中-1表示倒序,0表示不进出操作,1表示正序,
 我们来看看这里例子中的内容
package org.vwpolo.tablesorter;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.vwpolo.tablesorter.model.User;

public class UserInfoSorter extends ViewerSorter {
  
/** 用户名     */
  
public static final int USERNAME = 1;
  
  
/**
   * 用户ID
   
*/
  
public static final int USERID = 2;

  
/**
   * 用户职位
   
*/
  
public static final int POSITION = 3;
  
  
/**  机构ID    */
  
public static ViewerSorter userNameSorter0 = new UserInfoSorter(USERNAME);
  
  
/**      */
  
public static ViewerSorter userNameSorter1 = new UserInfoSorter(-USERNAME);
  
  
/**      */
  
public static ViewerSorter userIdSorter0 = new UserInfoSorter(USERID);
  
  
/**      */
  
public static ViewerSorter userIdSorter1 = new UserInfoSorter(-USERID);
  
  
/**      */
  
public static ViewerSorter positionSorter0 = new UserInfoSorter(POSITION);
  
  
/**      */
  
public static ViewerSorter positionSorter1 = new UserInfoSorter(-POSITION);
  
  
/**
   * 所要排序的列
   
*/
  
private int sortType;
  
  
public UserInfoSorter(int sortType) {
    
this.sortType = sortType;
  }
  
  
public int compare(Viewer viewer, Object obj1, Object obj2) {
    User param1 
= (User) obj1;
    User param2 
= (User) obj2;

    
switch (sortType) {
      
case USERNAME: {
        String id1 
= param1.getUserName();
        String id2 
= param2.getUserName();
        
return id1.compareTo(id2);
      }
      
case -USERNAME: {
        String id1 
= param1.getUserName();
        String id2 
= param2.getUserName();
        
return id2.compareTo(id1);
      }
      
case USERID: {
        String id1 
= param1.getUserId();
        String id2 
= param2.getUserId();
        
return id1.compareTo(id2);
      }
      
case -USERID: {
        String id1 
= param1.getUserId();
        String id2 
= param2.getUserId();
        
return id2.compareTo(id1);
      }
      
case POSITION: {
        String id1 
= param1.getPosition();
        String id2 
= param2.getPosition();
        
return id1.compareTo(id2);
      }
      
case -POSITION: {
        String id1 
= param1.getPosition();
        String id2 
= param2.getPosition();
        
return id2.compareTo(id1);
      }
    }
    
return 0;
  }
}    

我们从 compare方法中的参数提取出要比较的对象,进行对比排序,然后我们在相应的类中给表格添加事件处理排序,假如我们希望在显示名称的列名上单击的时候将这个列下所有的单元格进行排序可以这样

 userNameCol.addSelectionListener(new SelectionAdapter() {
      
boolean sortType = true;
      
public void widgetSelected(SelectionEvent e) {
        sortType 
= !sortType;
        userInfoTableViewer.setSorter(sortType 
? UserInfoSorter.userNameSorter0 : UserInfoSorter.userNameSorter1);
      }
    });    

当点击名称列的时候就会排序了,我们看看一个比较完整的类

package org.vwpolo.tablesorter;

import java.util.List;

public class UserInfoSorterTableApp extends ApplicationWindow {
  
private Table userInfoTable;
  
private TableViewer userInfoTableViewer;
  
/**
   * Create the application window
   
*/
  
public UserInfoSorterTableApp() {
    
super(null);
  }

  
/**
   * Create contents of the application window
   * 
@param parent
   
*/
  @Override
  
protected Control createContents(Composite parent) {
    Composite container 
= new Composite(parent, SWT.NONE);
    container.setLayout(
new FillLayout());

    userInfoTableViewer 
= new TableViewer(container, SWT.FULL_SELECTION | SWT.BORDER);
    userInfoTable 
= userInfoTableViewer.getTable();
    userInfoTable.setLinesVisible(
true);
    userInfoTable.setHeaderVisible(
true);

    
final TableColumn userNameCol = new TableColumn(userInfoTable, SWT.NONE);
    userNameCol.addSelectionListener(
new SelectionAdapter() {
      
boolean sortType = true;
      
public void widgetSelected(SelectionEvent e) {
        sortType 
= !sortType;
        userInfoTableViewer.setSorter(sortType 
? UserInfoSorter.userNameSorter0 : UserInfoSorter.userNameSorter1);
      }
    });
    userNameCol.setWidth(
100);
    userNameCol.setText(
"用户名");

    
final TableColumn userIdCol = new TableColumn(userInfoTable, SWT.NONE);
    userIdCol.addSelectionListener(
new SelectionAdapter() {
      
boolean sortType = true;
      
public void widgetSelected(SelectionEvent e) {
        sortType 
= !sortType;
        userInfoTableViewer.setSorter(sortType 
? UserInfoSorter.userIdSorter0 : UserInfoSorter.userIdSorter1);
      }
    });
    userIdCol.setWidth(
100);
    userIdCol.setText(
"用户编号");

    
final TableColumn positionCol = new TableColumn(userInfoTable, SWT.NONE);
    positionCol.addSelectionListener(
new SelectionAdapter() {
      
boolean sortType = true;
      
public void widgetSelected(SelectionEvent e) {
        sortType 
= !sortType;
        userInfoTableViewer.setSorter(sortType 
? UserInfoSorter.positionSorter0 : UserInfoSorter.positionSorter1);
      }
    });
    positionCol.setWidth(
100);
    positionCol.setText(
"职位");
    userInfoTableViewer.setContentProvider(
new ContentProvider());
    userInfoTableViewer.setLabelProvider(
new TableLabelProvider());
    userInfoTableViewer.setInput(UserFactory.getUsers());
    
//
    return container;
  }


  
/**
   * Launch the application
   * 
@param args
   
*/
  
public static void main(String args[]) {
    
try {
      UserInfoSorterTableApp window 
= new UserInfoSorterTableApp();
      window.setBlockOnOpen(
true);
      window.open();
      Display.getCurrent().dispose();
    }
    
catch (Exception e) {
      e.printStackTrace();
    }
  }

  
/**
   * Configure the shell
   * 
@param newShell
   
*/
  @Override
  
protected void configureShell(Shell newShell) {
    
super.configureShell(newShell);
    newShell.setText(
"支持排序的表格");
  }

  
/**
   * Return the initial size of the window
   
*/
  @Override
  
protected Point getInitialSize() {
    
return new Point(500375);
  }
}    

 这样表格排序功能就实现了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值