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

來源:互聯網
上載者:User

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

在項目實踐的過程中,遇到需要將grid中資料根據某一欄位分組的要求,當然,這個功能在api中有,在此列出來以供大家尋找:

兩點需要注意的地方:

1、在建立store時,需要設定groupField屬性的值,即為需要分組的值

for example:

JavaScript代碼

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

在這個資料模型中,我們需要以性別(sex)分組,那麼請看下面的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列所對應的值 

在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' }] }); 

如下:

 

當然實現分組後,在gridPanel中sex這一列就可以不用顯示。

需要注意的是store中的資料如果在變化的時候,分組是不是也可以正常顯示呢?

現在給grid增加一個itemclick事件,代碼如下:

JavaScript代碼

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

效果如

 

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

JavaScript代碼

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

再看效果:

 

這個就是我們想要的效果,在動態變化store中的資料時,分組也要實現,而不是將資料追加在gridPanel的最後。對應這兩段代碼的區分,主要在,store添加資料的方法,前者是add(record),後者是loadData(records,[append])

起初不能理解為什麼同樣是store添加資料,效果卻不一樣,看官方文檔的解釋,add(),The new Model instances will be added at the end of the existing collection.(將資料追加在集合的最後)恍然大悟,loadData是按照store的規則將資料載入進來。

另外,如何移除組中最舊一行,自己動手查了一下,文檔實現了,在這裡與大家分享:

//將前面的listeners監聽事件修改如下:

注意其中first([boolean group])方法,如果不傳參數,擷取的是store中的第一條資料,傳參為true時,返回的是store分組以組名為key,組內第一條資料為value的多個對象,PersonStore.first(true).female擷取female組內的第一條資料,想擷取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佔用記憶體,進行了進一步的處理,功能可以實現,但是方法有些笨,大家有好的辦法可以交流交流

看代碼:

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資料,記憶體中記錄的removed掉的就沒有了 console.log(PersonStore); alert(PersonStore.getRemovedRecords.length);//這句alert結果為0 // console.log(PersonStore.getAt(0)); } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.