PDA

View Full Version : [FIXED] Two way bind grid selection



fruel
25 Apr 2014, 9:57 AM
Hi,

I want to establish a two way data binding between a grid panel selection and a view model. I am able to bind the initial selection of the grid:

bind: {
selection: '{currentModel}'
}

where currentModel is a data entry of my view model but is there a way to automatically push back a new model in my view model when the grid selection change?

Thank you!

dongryphon
26 Apr 2014, 11:29 PM
Indeed there is. The "selection" of a single-select, row selection model grid is available when the grid is given a reference, like so:



items: [{
xtype: 'grid',
reference: 'mainGrid',
}, {
...
bind: '{mainGrid.selection}'
}]


This can be seen in this example: http://dev-devel.sencha.com/ext/5.0.0/examples/kitchensink/?theme=crisp#binding-associations

fruel
29 Apr 2014, 11:09 AM
Thank you dongryphon,

This is very interesting for binding a component to the selected item of a grid. But what I was looking for is a two way data binding between the grid and view model. I tried and when I change the selected item of the grid the view model value is not updated automatically.



Ext.define(SomeViewModel', {
extend: 'Ext.app.ViewModel',


alias: 'viewmodel.someviewmodel',


setCurrentItem: function(item) {
this.set('currentItem', item);
}
});

...

xtype: 'grid',
bind: {
selection: '{currentItem}'
}



With this code, when I set the current item, the grid correctly select the item, but when I change the selection of the grid the current item of the view model is not updated.

Am I missing something?

dongryphon
1 May 2014, 1:09 AM
The selection config of grid does not support two-way binding. At present you would only get the grid selection to follow the value you bind to "seletction". To go the other way you would have to bind to the grid's reference name with ".selection" on it. Thanks for the report! I have opened a bug in our bug tracker.