Extjs 的表格自帶排序功能,這個功能在大部分情況下能夠滿足我們的需求,但是在某種情況下,例如IP排序,默認情況下,按照字符串進行排序,

此時我們需要自定義排序規則,這個時候就需要我們重寫方法了,

具體代碼如下:
var grid = Ext.create('Ext.grid.Panel',{
//...
columns: [
{ text: 'name', dataIndex: 'name', sortable: true },
{
text: 'Custom',
sortable : true,
dataIndex: 'customsort',
//重寫此方法
doSort: function(state) {
var ds = this.up('grid').getStore();
var field = this.getSortParam();
ds.sort({
property: field,
direction: state,
//排序規則(重點)
sorterFn: function(v1, v2){
v1 = v1.get(field);
v2 = v2.get(field);
return v1.length > v2.length ? 1 : (v1.length < v2.length ? -1 : 0);
}
});
}
}
]
//....
})
解決方案參考:http://stackoverflow.com/questions/17795019/ext-js-sorting-custom-column-by-contents
