深入淺析Extjs中store分組功能的使用方法
在項(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)文章
前端面試知識(shí)點(diǎn)錦集(JavaScript篇)
這篇文章主要為大家分享了前端面試知識(shí)點(diǎn)錦集JavaScript篇,細(xì)致的給出了每個(gè)js面試知識(shí)點(diǎn)的答案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12javascript 閃爍的圣誕樹實(shí)現(xiàn)代碼
用js實(shí)現(xiàn)非常漂亮的帶閃爍效果的圣誕樹代碼。很佩服作者的想法。效果如下圖。2009-12-12一次圍繞setTimeout的前端面試經(jīng)驗(yàn)分享
這篇文章主要跟大家分享了一次圍繞setTimeout展開的前端面試經(jīng)驗(yàn),是一篇有意思的圍繞setTimeout展開的前端開發(fā)考題,考察了for循環(huán)、定時(shí)器setTimeout()、JavaScript閉包、匿名函數(shù)和Promise等,一不小心你可能就會(huì)做錯(cuò),快來看看你有沒有掌握了上面的知識(shí)。2017-06-06優(yōu)化網(wǎng)頁之快速的呈現(xiàn)我們的網(wǎng)頁
優(yōu)化網(wǎng)頁之快速的呈現(xiàn)我們的網(wǎng)頁...2007-06-06JS實(shí)現(xiàn)倒計(jì)時(shí)圖文效果
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)倒計(jì)時(shí)圖文效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11微信小程序?qū)崿F(xiàn)頁面浮動(dòng)導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)頁面浮動(dòng)導(dǎo)航,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01實(shí)現(xiàn)lightBox時(shí)的樣式與行為分離減少JS
本教程旨在實(shí)現(xiàn)lightBox時(shí)的樣式與行為分離,減少JS在各方面(全屏遮蔽、ie6中遮蔽select、雙向居中、高度自適應(yīng)內(nèi)容等)的工作。2009-07-07