Reputation: 1
I have one Form panel with one textfield and a grid. Now, I want to take userinput
and get the value Through ViewModel as json
to send it to server.
Problem here is, I am able to bind textfield so I am getting textfield value as one parameter in view model but how can I get selected grid row data as second parameter in viewMolde.getData()
.
For Example:
Model:
Ext.define('UserModel', {
extend: 'Ext.data.Model',
fields: [{
name: "name",
type: "string"
}, {
name: "gridRecord",
type: "auto"
}]
});
View Model :
Ext.define('QAVM', {
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.QAVM',
model: 'UserModel'
});
View :
Ext.define('View', {
extend: 'Ext.form.Panel',
xtype: 'app-test',
viewModel: 'QAVM',
items: [{
xtype: 'textfield',
fieldLabel: 'TestInt',
bind: '{name}' /**ABLE TO BIND and GETTING VALUE In viewModel.getData()*/
}, {
xtype: 'grid',
title: 'Simpsons',
formBind: true,
store: 'storeName',
bind: {
selection: 'gridSelectedRecord'
}, //HOW TO BIND GRID SELECTED RECORD TO MODEL'S SECOND FIELD "gridRecord"
columns: [{
text: 'Address-1',
dataIndex: 'addr'
}, {
text: 'Pincode',
dataIndex: 'pincode',
flex: 1
}]
}]
});
Upvotes: 4
Views: 16438
Reputation: 3152
First of all I want to say that imo your approach is wrong. You don't want to send your data to the server by getting your data from your viewmodel
. You want to send your record to the server through a proxy
on your model
or store
. The data from your viewmodel
should only be used to bind to your view
. The store
and the models
are your server data. But a store
can be bind (and filtered) through a viewmodel
to your view
.
Model-View-ViewModel
is a pattern where the model
is for your data (and migration through proxy
), the view
to represent your data and the viewmodel
to glue your model
and your view
together.
Now my answer.
You can do that using formulas
on your viewmodel
. You can then bind deep the current selection of the grid to your form.
ViewModel:
Ext.define('MyApp.view.group.GroupsModel', {
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.group-groups',
stores: {
allgroups: {
source: 'Groups',
sorters: {
property: 'name',
direction: 'ASC'
}
}
},
data: {
title: 'Groups'
},
formulas: {
currentRecord: {
// We need to bind deep to be notified on each model change
bind: {
bindTo: '{groupGrid.selection}', //--> reference configurated on the grid view (reference: groupGrid)
deep: true
},
get: function(record) {
return record;
},
set: function(record) {
if (!record.isModel) {
record = this.get('records').getById(record);
}
this.set('currentRecord', record);
}
}
}
});
View:
Ext.define('MyApp.view.group.Groups', {
extend: 'Ext.container.Container',
xtype: 'groups',
requires: [
'MyApp.view.group.GroupsController',
'MyApp.view.group.GroupsModel',
'Ext.grid.column.Boolean',
'Ext.form.field.Checkbox',
'Ext.form.field.TextArea',
'Ext.form.field.Text'
],
controller: "group-groups",
viewModel: {
type: "group-groups"
},
layout: {
type: 'hbox',
pack: 'start',
align: 'stretch'
},
style: {
backgroundColor: '#f5f5f5'
},
items: [{
xtype: 'grid',
reference: 'groupGrid', //--> used in the viewmodel
flex: 1,
bind: {
title: '{title}',
store: '{allgroups}'
},
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Description',
dataIndex: 'description',
flex: 1
}, {
xtype: 'booleancolumn',
text: 'Active',
trueText: 'Yes',
falseText: 'No',
dataIndex: 'isActive'
}]
}, {
xtype: 'form',
title: 'Details',
bodyPadding: 15,
minWidth: 300,
split: true,
defaults: {
xtype: 'textfield',
anchor: '100%',
allowBlank: false,
enforceMaxLength: true
},
items: [{
fieldLabel: 'Name',
bind: '{currentRecord.name}' //--> get current selected record from viewmodel
}, {
xtype: 'textarea',
fieldLabel: 'Description',
bind: '{currentRecord.description}',
height: 120
}, {
xtype: 'checkboxfield',
fieldLabel: 'Active',
bind: '{currentRecord.isActive}'
}]
}]
});
After editing a model
or adding models to your store
save/sync your store
or save your model
.
Upvotes: 5
Reputation: 84
Tested on extjs 6.
{
xtype: 'grid',
bind: {
store: '{gridStore}',
selection: '{selectedRow}' //--> used in the viewmodel
}
}
Upvotes: 1