PDA

View Full Version : pre-select checkboxes of gridpanel of "checkboxmodel" from store



christophercheng
3 Dec 2011, 10:53 PM
I am trying to load data from store into a gridpanel with checkboxmodel, however, it produces an error "views is undefined". What's missing here?



Ext.define('MyGridPanel', {
extend: 'Ext.grid.Panel',
alias: 'widget.myGridPanel',
preventHeader: true,
store: Ext.create('StoreA',{
autoLoad: false,
listeners: {
load: function() {
}
}
}),
columns: [
{ header: 'ID', dataIndex: 'id', hidden: true },
{ header: 'checked', dataIndex: 'checked', hidden: false },
{ header: 'Name', dataIndex: 'passengerName', flex: 1 }
],
selModel: new Ext.selection.CheckboxModel({
mode: 'MULTI'
}),
selType: 'checkboxmodel',
multiSelect: true,
loadForm: function(bookingHotelItemId) {
var gridpanel = this;
var store = this.store;
this.store.load({
method: 'get',
url : '/passenger!list.action',
callback: function(records, operation, success) {
Ext.each(records, function(item) {
var record = store.findRecord("id", item.data.id);
if (item.data.checked == "y") {
var record = this.getById(item.data.id);
gridpanel.getSelectionModel().select(record, false, false);
}
});
},
failure: function(form, action) {
}
});
}
});

skirtle
4 Dec 2011, 10:16 AM
When does loadForm get called? Could you post the full stacktrace, including line numbers?

A couple of things I notice.

Firstly, you are putting a fully instantiated store into your define config. The store object will be put on the grid class's prototype. This means that all instances of your grid will share the same store.

Secondly, you seem to be specifying your selection model options twice. I don't think it'll hurt but you might want to remove the duplication to avoid any confusion.

christophercheng
4 Dec 2011, 4:01 PM
after a few trials and tests, I found out that I should "check" those checkboxes after render, because after the store is loaded, the form's view has not been created yet.
By the way, if I do not create the store over there, what's the appropriate way of doing that? I thought about using StoreManager or 'define' it somewhere or even in the constructor



listeners: {
itemdblclick: function(){
},
afterrender: function() {
var selectedRecords = [];
this.store.each(function(record) {
if (record.data.checked == "y") {
selectedRecords.push(record);
}
});
this.getSelectionModel().select(selectedRecords, false, false);
}
},

skirtle
4 Dec 2011, 4:16 PM
Using afterrender is a little risky. It makes 2 assumptions: the store is loaded before the grid renders and the store only loads once. I think using the refresh event might be a safer bet.

I don't understand your question about the store.