ExtJS6.0之後臺排序

一般在前臺排序只需要在Grid的列里加上一個屬性即可:

columns: [
    new Ext.grid.RowNumberer({width:50}),
    {
        header: '工號',
        width: 120,
        sortable: true,//表明該列可以前臺排序,即當前頁面的排序
        dataIndex: 'empId',
        align: 'center'
    },
]

如果要後臺排序,需要在創建數據源的時候添加如下代碼:

var extField = ["empId", "ename", "creditCard", "telephone", "workName", "projectNameDesc", "workStatus", "deptName", "workStatusDesc"];
store = Ext.create('Ext.data.Store', {
    pageSize: limit,
    fields: extField,
    proxy: {
        type: 'ajax',
        url: basePath + 'trialEmployee/trial-employee!getTrialEmpListByDeptExt',
        reader: {
            type: 'json',
            root: 'root',
            totalProperty: 'count'
        }
    },
    //***********************後臺排序**********//
    //*********具體後臺如何接收參數,看下面講解**//
    remoteSort: true,  
    /*sortInfo: {//ext4 之前
        field: 'empId',
        direction: 'ASC'
    }*/
    sorters:[//ext6.0排序方式,可以傳多個字段,以{},{}分割
        {
            property :'empId',
            direction:'DESC'
        }
    ],
    //******************************************//
});

後臺接收方式

在http請求中,排序是這樣傳到後臺的:

sort:[{"property":"empId","direction":"DESC"}]

所以,在後臺接收的時候,需將其轉換爲實體類,獲取其中的排序信息。

  • 新增Sort排序實體類,接收sorters中的字段proerty(排序字段)和direction(排序方式)
package com.juttec.account.entity;

/**
 * 
 * 排序實體類
 * 
 * <pre>
 * 接收前臺傳過來的排序相關參數
 * </pre>
 * 
 * @author 李曉東
 * 
 * 2017.03.23
 * 
 * @since 1.0
 *
 */
public class Sort {

    private String property;

    private String direction;

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

}
  • 在請求的action中,接收參數,將其轉換,使用google的開源工具Gson包
//可能是多個排序字段
List<Sort> sortEntity = gson.fromJson(getRequestPara("sort"), new TypeToken<List<Sort>>(){}.getType());
String sort = sortEntity.get(0).getProperty();//排序的列名
String dir = sortEntity.get(0).getDirection();//排序方式
//前臺傳過來的是實體類映射的字段,sql查詢需將其轉換爲數據庫字段
if ("empId".equals(sort)) {
    sort = "emp_id";
}

如果字段在數據庫創建的時候是下劃線命名方式(下劃線命名法),使用eclipse自帶的工具,生成的字段是小駝峯命名的(駝峯命名法)。即emp_id對應empId,這個時候可以使用封裝的工具類進行轉換。

/**
 * 將字段從小駝峯轉換爲下劃線方式
 */
public static String camelToUnderline(String param) {
    if ((param == null) || ("".equals(param.trim()))) {
        return "";
    }
    int len = param.length();
    StringBuilder sb = new StringBuilder(len);
    for (int i = 0; i < len; ++i) {
        char c = param.charAt(i);
        if (Character.isUpperCase(c)) {
            sb.append('_');
            sb.append(Character.toLowerCase(c));
        } else {
            sb.append(c);
        }
    }
    return sb.toString();
}

排序到此結束!

發佈了110 篇原創文章 · 獲贊 19 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章