Ext 自定义排序(重写排序方法)
ext 列模型 设置sortable : true 属性后可使用ect封装的排序方法,对这列字段根据ASCII排序,工作中遇到需求,将某几行数据置顶显示(如置顶产品),并且始终置顶显示,不会被任何排序影响到。
经过查询资料发现,重写Ext.data.Store.prototype.applySort = function(){} 可实现自定义排序
代码如下
Ext.onReady(function() { // Ext入口函数,相当于java中的主函数
Ext.QuickTips.init();
/**
* 置顶的产品不参与排序,重写排序方法
*/
Ext.data.Store.prototype.applySort = function({ // 重写函数方法,实现置顶在排序最上方
if (this.sortInfo && !this.remoteSort) {
var s = this.sortInfo, f = s.field;
direction = s.direction || 'ASC';
var dir = direction == 'ASC' ? 1 : -1;
var st = this.fields.get(f).sortType;
var fn = function(v1, v2) {
//关键地方,重写排序排序规则
if(__unitlevel == 2 ){
if(v1.data.PRIORITY_PROVINCE != '0' || v2.data.PRIORITY_PROVINCE != '0' ){
//置顶数据永远在上面
return 0;
}else{
var v1 = st(v1.data[f]), v2 = st(v2.data[f]);
return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
}
}else if(__unitlevel == 3){
if(v1.data.PRIORITY_CITY != '0' || v2.data.PRIORITY_CITY != '0' ){
//置顶数据永远在上面
return 0;
}else{
var v1 = st(v1.data[f]), v2 = st(v2.data[f]);
return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
}
}else{
var v1 = st(v1.data[f]), v2 = st(v2.data[f]);
return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
}
};
this.data.sort(direction, fn);
if (this.snapshot && this.snapshot != this.data) {
this.snapshot.sort(direction, fn);
}
}
};
/********用于悬浮显示名称过长(by:zwq)*******/
var updateTip = function (field, t) {
Ext.QuickTips.init();
Ext.QuickTips.register({
target: field.el,
text: field.getValue()
})
};
*最主要功能如下
Ext.data.Store.prototype.applySort = function() { // 重写函数方法,实现置顶在排序最上方
if (this.sortInfo && !this.remoteSort) {
var s = this.sortInfo, f = s.field;
direction = s.direction || ‘ASC’;
var dir = direction == ‘ASC’ ? 1 : -1;
var st = this.fields.get(f).sortType;
var fn = function(v1, v2) { //关键地方,重写排序排序规则
console.log(v1);
console.log(v2);
if(v1.data.PRIORITY_PROVINCE != ‘0’ || v2.data.PRIORITY_PROVINCE != ‘0’ ){
//列模型中的数据的一个字段PRIORITY_PROVINCE>0为置顶数据
//置顶数据永远在上面
return 0;
}else{
var v1 = st(v1.data[f]), v2 = st(v2.data[f]);
return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
}
};
this.data.sort(direction, fn);
if (this.snapshot && this.snapshot != this.data) {
this.snapshot.sort(direction, fn);
}
}
};*
// 定义自动当前页行号
var rownum = new Ext.grid.RowNumberer({
header: ‘No.’,
width: 28,
renderer:function renderName(value, cellmeta, record, rowIndex, columnIndex, store){
if(__unitlevel == 2){
if(store.data.items[rowIndex].data.PRIORITY_PROVINCE>0 && store.data.items[rowIndex].data.MERCHANT_ORG == __units){
return “↑”+ ++rowIndex;
}else{
return ++rowIndex;
}
}
if(__unitlevel == 3 ){
if(store.data.items[rowIndex].data.PRIORITY_CITY>0 && store.data.items[rowIndex].data.MERCHANT_ORG == __units){
return “↑”+ ++rowIndex;
}else{
return ++rowIndex;
}
}
return ++rowIndex;
}
});
// 定义复选框
var sm = new Ext.grid.CheckboxSelectionModel();
// 定义列模型
var cm = new Ext.grid.ColumnModel([rownum,sm,
{header: ‘产品编号’,dataIndex: ‘GIFT_NO’,sortable: true,width: 150,hidden:true},
{header: ‘产品名称’,dataIndex: ‘GIFT_NAME’,sortable: true,width: 150},
{header: ‘市产品置顶优先级’,dataIndex: ‘PRIORITY_CITY’,sortable: true,width: 50,hidden:true},
{header: ‘省产品置顶优先级’,dataIndex: ‘PRIORITY_PROVINCE’,sortable: true,width: 50,hidden:true}
);