欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入淺析Extjs中store分組功能的使用方法

 更新時(shí)間:2016年04月20日 12:00:22   作者:雞啄米  
這篇文章主要介紹了深入淺析Extjs中store分組功能的使用方法的相關(guān)資料,需要的朋友可以參考下

在項(xiàng)目實(shí)踐的過程中,遇到需要將grid中數(shù)據(jù)根據(jù)某一字段分組的要求,當(dāng)然,這個(gè)功能在api中有,在此列出來以供大家查找:

兩點(diǎn)需要注意的地方:

1、在創(chuàng)建store時(shí),需要設(shè)置groupField屬性的值,即為需要分組的值

for example:

JavaScript代碼

Ext.define('Person', { 
extend: 'Ext.data.Model', 
fields: ['name', 'sex'] 
}); 

在這個(gè)數(shù)據(jù)模型中,我們需要以性別(sex)分組,那么請(qǐng)看下面的store

JavaScript代碼

var PersonStore = Ext.create('Ext.data.Store', { 
storeId: 'PersonStore', 
model: 'Person', 
groupField: 'sex', 
data: [{ 
name: 'hongmei li', 
sex: 'female' 
},{ 
name: 'san zhang', 
sex: 'male' 
},{ 
name: 'Jim Green', 
sex: 'male' 
},{ 
name: 'Lily', 
sex: 'female' 
},{ 
name: 'Lucy', 
sex: 'female' 
}] 
}); 

接下來,我們需要定義分組顯示的tpl

JavaScript代碼

var groupingFeature= Ext.create('Ext.grid.feature.Grouping',{ 
groupHeaderTpl: 'sex: {name} ({rows.length} Item{[values.rows.length > 1 ? "s" : ""]})' 
});//注意其中{name}即為store中sex列所對(duì)應(yīng)的值 

在gridPanel中,代碼如下:配置features為上述定義的groupingFeature

JavaScript代碼

var grid = Ext.create('Ext.grid.Panel', { 
renderTo: Ext.getBody(), 
store: PersonStore, 
width: 600, 
height: 400, 
title: 'Person', 
features: [groupingFeature], 
columns: [{ 
text: 'Name', 
flex: 1, 
dataIndex: 'name' 
},{ 
text: 'sex', 
flex: 1, 
dataIndex: 'sex' 
}] 
}); 

效果圖如下:

 

當(dāng)然實(shí)現(xiàn)分組后,在gridPanel中sex這一列就可以不用顯示。

需要注意的是store中的數(shù)據(jù)如果在變化的時(shí)候,分組是不是也可以正常顯示呢?

現(xiàn)在給grid增加一個(gè)itemclick事件,代碼如下:

JavaScript代碼

listeners:{ 
itemclick:function(thisview,record){ 
PersonStore.<span style="color:#ff0000;">add</span>([{name:"li",sex:"male"},{name:"zhang",sex:"female"}]); 
} 
} 

效果如下圖

 

可以看出來,界面并不是我們想要的,那么如何解決呢?(最開始愚笨的解決方案是我將此gridPanel移除并銷毀掉,重新加載)我將listeners監(jiān)聽事件的代碼做了一些變換

JavaScript代碼

listeners:{ 
itemclick: function (thisview,record){ 
PersonStore.loadData([{name: "li" ,sex: "male" },{name: "zhang" ,sex: "female" }], true ); 
} 
} 

再看效果:

 

這個(gè)就是我們想要的效果,在動(dòng)態(tài)變化store中的數(shù)據(jù)時(shí),分組也要實(shí)現(xiàn),而不是將數(shù)據(jù)追加在gridPanel的最后。對(duì)應(yīng)這兩段代碼的區(qū)分,主要在,store添加數(shù)據(jù)的方法,前者是add(record),后者是loadData(records,[append])

起初不能理解為什么同樣是store添加數(shù)據(jù),效果卻不一樣,看官方文檔的解釋,add(),The new Model instances will be added at the end of the existing collection.(將數(shù)據(jù)追加在集合的最后)恍然大悟,loadData是按照store的規(guī)則將數(shù)據(jù)加載進(jìn)來。

另外,如何移除組中最舊一行,自己動(dòng)手查了一下,文檔實(shí)現(xiàn)了,在這里與大家分享:

//將前面的listeners監(jiān)聽事件修改如下:

注意其中first([boolean group])方法,如果不傳參數(shù),獲取的是store中的第一條數(shù)據(jù),傳參為true時(shí),返回的是store分組以組名為key,組內(nèi)第一條數(shù)據(jù)為value的多個(gè)對(duì)象,PersonStore.first(true).female獲取female組內(nèi)的第一條數(shù)據(jù),想獲取male中的,可以使用PersonStore.first(true).male

JavaScript代碼

listeners:{ 
itemclick: function (thisview,record){ 
PersonStore.loadData([{name: "li" ,sex: "male" },{name: "zhang" ,sex: "female" }], true ); 
alert(PersonStore.first( true ).female.get( 'name' )); 

console.log(PersonStore.first( true ).female); 
PersonStore.remove(PersonStore.first( true ).female); 
// console.log(PersonStore.getAt(0));
} 
}

為避免removedRecords占用內(nèi)存,進(jìn)行了進(jìn)一步的處理,功能可以實(shí)現(xiàn),但是方法有些笨,大家有好的辦法可以交流交流

看代碼:

listeners:{ 
itemclick:function(thisview,record){ 
PersonStore.loadData([{name:"li",sex:"male"},{name:"zhang",sex:"female"}],true); 
alert(PersonStore.first(true).female.get('name')); 
console.log(PersonStore.first(true)); 
PersonStore.remove(PersonStore.first(true).female); 
var recs = PersonStore.getRange(); 
console.log(recs); 
//PersonStore.removeAll(true);//這句有沒有都可以 
PersonStore.loadRecords(recs);//重新load數(shù)據(jù),內(nèi)存中記錄的removed掉的就沒有了 
console.log(PersonStore); 
alert(PersonStore.getRemovedRecords.length);//這句alert結(jié)果為0 
// console.log(PersonStore.getAt(0)); 
} 
}

相關(guān)文章

最新評(píng)論