Ext Js combobox value after onchange is value instead of label - extjs

Just getting frustrated by something that should be an easy fix, but I'm too simple minded to see it :)
I'm having a grid where 1 column is a combobox. The thing works just fine and the correct value is beeing sent through my ajax request, but after I edited the grid row, the combobox disappread and the value that comes into place is not the label, but the value.
editor: new Ext.form.field.ComboBox({
typeAhead: true,
lazyRender: true,
store: new Ext.data.ArrayStore({
fields: ['contact', 'contactLabel'],
data: [
[1,'Jan'],
[2,'Jeroen'],
[3,'Mattijs'],
[4,'Sven'],
[5,'Thomas'],
[6,'Yoran']
]
}),
valueField: 'contact',
displayField: 'contactLabel',
hiddenName: 'contact'
})
So what happens is that when I change the combobox to i.e.. "Thomas", the value of the cell becomes "5", instead of "Thomas". I thought that defining value/display fields would make a difference, but it does not.
Anyone that knows the answer?

I am not quite sure if I got you right. If so you will need a renderer for that. I guess the example below the code snipped should show you if you are meaning such a case.
// the renderer. You should define it within a namespace
var comboBoxRenderer = function(combo) {
return function(value) {
var idx = combo.store.find(combo.valueField, value);
var rec = combo.store.getAt(idx);
return (rec === null ? '' : rec.get(combo.displayField) );
};
}
// the edit combo
var combo = new Ext.form.ComboBox({
store: store,
valueField: "value",
displayField: "text"
});
See this full working example for both (cellEditing + rowEditing) JSFiddle ()
Here's the complete code
Ext.create('Ext.data.Store', {
storeId:'simpsonsStore',
fields:['name', 'email', 'phone', 'id'],
data:{'items':[
{"name":"Lisa", "email":"lisa#simpsons.com", "phone":"555-111-1224","id": 0},
{"name":"Bart", "email":"bart#simpsons.com", "phone":"555-222-1234","id": 1},
{"name":"Homer", "email":"home#simpsons.com", "phone":"555-222-1244","id": 2},
{"name":"Marge", "email":"marge#simpsons.com", "phone":"555-222-1254","id": 3}
]},
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'items'
}
}
});
// the combo store
var store = new Ext.data.SimpleStore({
fields: [ "value", "text" ],
data: [
[ 0, "Option 0" ],
[ 1, "Option 1" ],
[ 2, "Option 2" ],
[ 3, "Option 3" ]
]
});
// the renderer. You should define it within a namespace
var comboBoxRenderer = function(combo) {
return function(value) {
var idx = combo.store.find(combo.valueField, value);
var rec = combo.store.getAt(idx);
return (rec === null ? '' : rec.get(combo.displayField) );
};
}
// the edit combo
var combo = new Ext.form.ComboBox({
store: store,
valueField: "value",
displayField: "text"
});
// demogrid
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
{header: 'Name', dataIndex: 'name', editor: 'textfield'},
{header: 'Email', dataIndex: 'email', flex:1,
editor: {
xtype: 'textfield',
allowBlank: false
}
},
{header: 'Phone', dataIndex: 'phone'},
{header: 'id', dataIndex: 'id', editor: combo, renderer: comboBoxRenderer(combo)}
],
selType: 'cellmodel',
plugins: [
Ext.create('Ext.grid.plugin.CellEditing', {
clicksToEdit: 1
})
],
height: 200,
width: 400,
renderTo: 'cell'
});
// demogrid
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
{header: 'Name', dataIndex: 'name', editor: 'textfield'},
{header: 'Email', dataIndex: 'email', flex:1,
editor: {
xtype: 'textfield',
allowBlank: false
}
},
{header: 'Phone', dataIndex: 'phone'},
{header: 'id', dataIndex: 'id', editor: combo, renderer: comboBoxRenderer(combo)}
],
selType: 'rowmodel',
plugins: [
Ext.create('Ext.grid.plugin.RowEditing', {
clicksToEdit: 1
})
],
height: 200,
width: 400,
renderTo: 'row'
});
html
<div id="cell"></div>
<div id="row"></div>

Try:
data: [
{contact: 1, contactLabel: 'Jan'},
...
]

Related

How to change default title of errorSummary from "Errors" to something else in EXTJs

How to customize errorSummary in Extjs? The default title for errorSummary is "Errors"(screenshot attached for reference), is there any way to change it to something else?
Image
Ext.create('Ext.data.Store',{
storeId:'simpsonsStore',
fields:['name', 'email', 'phone'],
data: [
{"name":"Lisa", "email":"lisa#simpsons.com", "phone":"555-111-1224"},
{"name":"Bart", "email":"bart#simpsons.com", "phone":"555--222-1234"},
{"name":"Homer", "email":"home#simpsons.com", "phone":"555-222-1244"},
{"name":"Marge", "email":"marge#simpsons.com", "phone":"555-222-1254"}
]
});
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
{header: 'Name', dataIndex: 'name', editor: 'textfield'},
{header: 'Email', dataIndex: 'email', flex:1,
editor: {
xtype: 'textfield',
allowBlank: false
}
},
{header: 'Phone', dataIndex: 'phone'}
],
selType: 'rowmodel',
plugins: [
Ext.create('Ext.grid.plugin.RowEditing', {
clicksToEdit: 1,
errorSummary:true,
})
],
height: 200,
width: 400,
renderTo: Ext.getBody()
});
plugins: [
Ext.create('Ext.grid.plugin.RowEditing', {
clicksToEdit: 1,
errorsText:'test',
errorSummary:true
})
],
If you want to change it globally,We can override the editing plugin by using below code.
Ext.define('OverridedRowEditing',{
override: 'Ext.grid.plugin.RowEditing',
config: {
errorsText: 'Test'
}
});

Setting combobox value with extjs

I have a combobox with prefilled values.
On click of approve all button I want to set its value to some value.
Here is my code
var myStore = new Ext.data.ArrayStore({
sortInfo: {field: 'Name', direction: "ASC"},
data: arrHoursData,
fields: [{name: 'Id', type: 'string'},
{name: 'Name', type: 'string'},
{name: 'Hours', type: 'string'},
{name: 'AssignmentId', type: 'string'},
{name: 'Status', type: 'string'}
]
});
var statusStore = new Ext.data.Store({
data: arrStatus,
fields: ['Id', 'Name']
});
var hoursGrid = Ext.create('Ext.grid.Panel', {
store: myStore,
width: 340,
height: 270,
collapsible: false,
selType: 'cellmodel',
plugins: [
Ext.create('Ext.grid.plugin.CellEditing', {
clicksToEdit:1
})
],
columns: [
{header: 'Id', dataIndex: 'Id',hidden: true},
{header: 'Date',dataIndex: 'Name', width:140},
{header: 'Hours',dataIndex: 'Hours', width:100, editor: {xtype: 'numberfield', minValue: 0, allowBlank: false}},
{header: 'Status',dataIndex: 'Status', width:100, editor: { xtype: 'combobox',store: statusStore, queryMode: 'local', displayField: 'Name', valueField: 'Id',id:'status'}, renderer: function (value) {
var label = '';
jQuery.each(arrStatus, function(k,v)
{
if(v['Id'] == value)
label = v['Name'];
});
return label;
}
}
]
});
var win = new Ext.Window({
closable: true,
title: "Edit Hours",
layout: 'form',
modal: true,
width: 360,
height: 300,
plain: true,
border: false,
items: [
{
flex: 1,
xtype: 'container',
style:'margin-top:15px',
layout: {
type: 'hbox',
align: 'stretch'
},
items:[hoursGrid]
}
],
buttons: [
{
text: 'Approve All',
handler: function ()
{
Ext.getCmp('status').setValue(approvedStatusId);
}
}
],
});
win.show(g);
I tried to set its value using getCmp too but it gives me error that
Ext.getCmp('status') is not defined.
You have to set the value on the records, not the combobox. The combobox always uses the value that is set in the record.
grid.getStore().each(function(record) {
record.set("Status",approvedStatusId);
};

ExtJS 4.2 grid cellediting how to bind column value with combo display value

I have a grid with cellediting plugin.
One of my columns is a int field which represents a value in a combo store when editing the cell.
I want to know how can I have this column to show the displayfield instead of the value before I edit the cell.
Here are the images for reference:
The values 2,0,0 etc are my "accesstype" field that is an int.
If I click the cell to edit that why I get:
And If I select a value then instead of showing the text I get again the int value.
How can I show the display field instead of the value field?
For the ease of access here's the answer from the other question:
You can set a default value for the combo. That should then get that rendered at startup.
Use cell renderer to render the displayField of the combo into your grid. Following a working example that can be poster in one of the API code boxes
Working JSFiddle
Ext.create('Ext.data.Store', {
storeId:'simpsonsStore',
fields:['name', 'email', 'phone', 'id'],
data:{'items':[
{"name":"Lisa", "email":"lisa#simpsons.com", "phone":"555-111-1224","id": 0},
{"name":"Bart", "email":"bart#simpsons.com", "phone":"555-222-1234","id": 1},
{"name":"Homer", "email":"home#simpsons.com", "phone":"555-222-1244","id": 2},
{"name":"Marge", "email":"marge#simpsons.com", "phone":"555-222-1254","id": 3}
]},
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'items'
}
}
});
// the renderer. You should define it within a namespace
var comboBoxRenderer = function(combo) {
return function(value) {
var idx = combo.store.find(combo.valueField, value);
var rec = combo.store.getAt(idx);
return (rec === null ? '' : rec.get(combo.displayField) );
};
}
// the combo store
var store = new Ext.data.SimpleStore({
fields: [ "value", "text" ],
data: [
[ 1, "Option 1" ],
[ 2, "Option 2" ]
]
});
// the edit combo
var combo = new Ext.form.ComboBox({
store: store,
valueField: "value",
displayField: "text"
});
// demogrid
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
{header: 'Name', dataIndex: 'name', editor: 'textfield'},
{header: 'Email', dataIndex: 'email', flex:1,
editor: {
xtype: 'textfield',
allowBlank: false
}
},
{header: 'Phone', dataIndex: 'phone'},
{header: 'id', dataIndex: 'id', editor: combo, renderer: comboBoxRenderer(combo)}
],
selType: 'cellmodel',
plugins: [
Ext.create('Ext.grid.plugin.CellEditing', {
clicksToEdit: 1
})
],
height: 200,
width: 400,
renderTo: Ext.getBody()
});
Just remove the config property 'valueField' for that combo,it will display the displayField value after the select of the value from the dropdown.

extJs - GridPanel with combobox cell editing

I have a grid panel populated from local store. I added combo box for editing type cell. I want my combo box to be populated from a remote source. But I can't make it load any data. Here is my code:
Ext.define('System.view.MainTab.NewConnection.Contacts', {
extend: 'Ext.grid.Panel',
alias: 'widget.newConnectionContactsPanel',
requires: [
'Ext.grid.View',
'Ext.grid.column.Column',
'Ext.form.field.ComboBox',
'Ext.toolbar.Toolbar',
'Ext.toolbar.Fill',
'Ext.button.Button',
'Ext.grid.plugin.RowEditing'
],
height: 250,
width: 400,
itemId: 'contactsGridPanel',
title: 'My Grid Panel',
initComponent: function() {
var records = [];
var store = new Ext.data.ArrayStore({
fields: [
{name: 'type'},
{name: 'value'}
]
});
store.loadData(records);
var me = this;
Ext.applyIf(me, {
columns: [
{
dataIndex: 'type',
xtype: 'gridcolumn',
text: 'Type',
flex: 1,
editor: {
xtype: 'combobox',
displayField: 'neme',
valueField: 'id',
queryMmode: 'remote',
store: new Ext.data.JsonStore({
proxy: {
type: 'ajax',
url: '/ws/rest/contacts/getKeywords.json?keywordType=CONTACTS',
reader: {
type: 'json',
root: 'data',
idProperty: 'id'
}
},
autoLoad: true,
fields: [
{type: 'integer', name: 'id'},
{type: 'string', name: 'name'}
]
})
}
},
{
dataIndex: 'value',
xtype: 'gridcolumn',
text: 'Value',
flex: 1,
editor: {
xtype: 'textfield'
}
}
],
dockedItems: [
{
xtype: 'toolbar',
dock: 'bottom',
items: [
{
xtype: 'tbfill'
},
{
xtype: 'button',
itemId: 'removeBtn',
text: 'Remove'
},
{
xtype: 'button',
itemId: 'addBtn',
text: 'Add',
listeners: {
click: function (button, e, eOpts) {
var grid = button.up('#contactsGridPanel');
var store = grid.getStore();
var rowEditing = grid.getPlugin('rowediting');
rowEditing.cancelEdit();
var record = store.add({})[0];
rowEditing.startEdit(record, 0);
}
}
}
]
}
],
plugins: [
Ext.create('Ext.grid.plugin.RowEditing', {
pluginId: 'rowediting',
listeners: {
edit: function() {
}
}
})
]
});
me.callParent(arguments);
}
});
Here is a json response I get from the server:
{
"success": true,
"data": [
{
"id": 1,
"name": "Fax"
},
{
"id": 2,
"name": "Home page"
}
]
}
Can't make it all work. The combo box is empty. What am I doing wrong?
The most likely is that you have a typo in your displayField config.
As a side note, you should really say what kind of debugging you've done. Saying you "can't make it work" doesn't really help. Does the request hit the server? Does the store get data in it? Mentioning what you've done would help someone answering you.
You should catch the column Values from your remote source. Try adding the mapping attribute.
autoLoad: true,
fields: [
{type: 'integer', name: 'id', **mapping:'id'**},
{type: 'string', name: 'name', **mapping :'name'**}
]

ExtJs Grid BufferRenderer doesnot display the grid rows

I want to implement grid bufferrenderer in my simple grid panel that shows a list of information using ExtJS 4.2.1. Without using the bufferrenderer plugin, it shows all the data, but when i used this plugin, my grid contains no data.
This is my grid without using the plugin
This is my grid using the plugin
The grid panel's code is:
Ext.define('MyApp.view.MyPanel', {
extend: 'Ext.panel.Panel',
itemId: 'myPanel',
title: '',
requires: ['Ext.grid.plugin.BufferedRenderer'],
initComponent: function() {
var me = this;
Ext.applyIf(me, {
items: [
{
xtype: 'gridpanel',
autoRender: true,
autoShow: true,
itemId: 'gridPanel',
title: 'My Grid Panel',
store: 'MyJsonStore',
columns: [
{
xtype: 'gridcolumn',
dataIndex: 'id',
text: 'Id'
},
{
xtype: 'gridcolumn',
dataIndex: 'firstName',
text: 'FirstName'
},
{
xtype: 'gridcolumn',
dataIndex: 'middleName',
text: 'MiddleName'
},
{
xtype: 'gridcolumn',
dataIndex: 'lastName',
text: 'LastName'
},
{
xtype: 'gridcolumn',
dataIndex: 'age',
text: 'Age'
},
{
xtype: 'gridcolumn',
dataIndex: 'country',
text: 'Country'
},
{
xtype: 'gridcolumn',
dataIndex: 'city',
text: 'City'
},
{
xtype: 'gridcolumn',
dataIndex: 'street',
text: 'Street'
},
{
xtype: 'gridcolumn',
dataIndex: 'mobile',
text: 'Mobile'
},
{
xtype: 'gridcolumn',
dataIndex: 'phone',
text: 'Phone'
},
{
xtype: 'gridcolumn',
dataIndex: 'zip',
text: 'Zip'
}
],
plugins: 'bufferedrenderer'
/*plugins: {
ptype: 'bufferedrenderer',
trailingBufferZone: 20,
leadingBufferZone: 25
}*/
}
]
});
me.callParent(arguments);
}
});
The Store's code is:
Ext.define('MyApp.store.MyJsonStore', {
extend: 'Ext.data.Store',
requires: [
'MyApp.model.GridModel'
],
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
autoLoad: true,
model: 'MyApp.model.GridModel',
storeId: 'MyJsonStore',
buffered: true,
clearOnPageLoad: false,
clearRemovedOnLoad: false,
leadingBufferZone: 25,
pageSize: 500,
purgePageCount: 10,
trailingBufferZone: 20,
proxy: {
type: 'ajax',
url: 'data/users.json',
reader: {
type: 'json',
root: 'users',
totalProperty: 'total_user'
}
}
}, cfg)]);
}
});
Can anyone help me with this? Thanks
Setting the height property in grid will fix this issue.
height: 300
Make sure that all panels up to the viewport have their layout set to “fit”. Also, region of the grid should be “center”.
I have not tested, but something like this should work:
var grid = Ext.create('MyApp.view.MyPanel', {
layout: 'fit',
region: 'center'
});
var viewport = Ext.create('Ext.container.Viewport', {
renderTo: Ext.getBody(),
items: [
grid
]
});

Resources