ComboBox is not working in an EditorGridPanel for ExtJS - javascript

I am new to ExtJS and need to put a ComboBox in an EditorGridPanel.
My code so far does not create the combobox in the EditorGridPanel and the EditorGridPanel does not display as well.
Here is the code and thanks for your help. capturetheflag
/*==== INVOICE DATA START =======================================================*/
/* create the ComboBox editor */
var idCombo = new Ext.form.ComboBox({
id: 'id',
valueField: 'id',
displayField:'id',
store: '', //what do I store here??
triggerAction: 'all'
});
var idRenderer = function(value,metaData,record){
// try record.data.teacher here
return "displayValue"
var iLineItemCM = new Ext.grid.ColumnModel([
{
id: 'i_line_item_id',
header: 'Line Item ID',
dataIndex: 'i_line_item_id',
width: 80,
editor: this.idCombo(),
renderer:idRenderer
}
,{
id:'i_line_item_name',
header: "Line Item Name",
dataIndex: 'i_line_item_name',
width: 315,
resizable: true,
align: 'center',
editor: new Ext.form.TextArea({
allowBlank: false
})
}
,{
header: "Amount",
dataIndex: 'i_line_item_amt',
width: 80,
align: 'right',
renderer: 'usMoney',
editor: new Ext.form.NumberField({
allowBlank: false,
allowNegative: false,
maxValue: 100000
})
}
]);
var iLineItemRec =
new Ext.data.Record.create([
{
name: 'i_line_item_id' ,
mapping: 'i_line_item_id' ,
type: 'string'
}
,{
name: 'i_line_item_name' ,
mapping: 'i_line_item_name' ,
type: 'string'
}
,{
name: 'i_line_item_amt' ,
mapping: 'i_line_item_amt' ,
type: 'string'
}
]);
var iLineItemStore = new Ext.data.Store({
url: '',
reader: new Ext.data.JsonReader({
root: 'rows'
},
iLineItemRec
)
});
var iLineItemGrid = new Ext.grid.EditorGridPanel({
id: 'iLineItemStore',
store: iLineItemStore,
cm: iLineItemCM,
cls: 'iLineItemGrid',
width: 'auto',
height: 'auto',
frame: true,
//title:'Edit Plants?',
//plugins:checkColumn,
clicksToEdit:1,
viewConfig: {
//forceFit: true
autoFit:true
},
tbar: [{
text: 'Add',
tooltip:'Add the line item',
handler : function(){
var r = new iLineItemRec({
i_line_item_name: '',
i_line_item_amt: ''
});
iLineItemGrid.stopEditing();
iLineItemStore.insert(0, r);
iLineItemGrid.startEditing(0, 0);
}
},
{
text: 'Delete',
tooltip:'Remove the selected line item',
handler: function(){
iLineItemGrid.stopEditing();
var r = iLineItemGrid.getSelectionModel().getSelectedCell();
iLineItemStore.removeAt(r[1]);
}
}
]
});
/////////////////// CODE ENDS

You need to create a data store and bind that to your combobox. Then in the combo config, you can tell it which field from the store you want to display and which field you want for the value. Here is an example from ExtJS 4 API Docs:
// The data store containing the list of states
var states = Ext.create('Ext.data.Store', {
fields: ['abbr', 'name'], // fields that your data consists of
data : [
{"abbr":"AL", "name":"Alabama"},
{"abbr":"AK", "name":"Alaska"},
{"abbr":"AZ", "name":"Arizona"}
//...
]
});
// Create the combo box, attached to the states data store
var combo = Ext.create('Ext.form.ComboBox', {
fieldLabel: 'Choose State',
store: states, // here you use the store
queryMode: 'local',
displayField: 'name', // you tell your combobox which field to display from the store
valueField: 'abbr', // you tell your combobox which field to use for value from the store
renderTo: Ext.getBody()
});
If you use combo.getValue(), and if e.g. "Alaska" were selected in your combobox, it would return "AK". You can also use the same field as displayField and valueField if you like.

Related

Setting a Default Value to a ComboBox using Json in ExtJS 3.4

I am trying to set a default value in a ComboBox in ExtJS 3.4 I have tried to set value: 'Standard' in the ComboBox config, but that only places a string in the box. I did some digging around and tried to setup the afterrender function, but still haven't gotten it to populate. The goal is to get the box to actually select the value and populate the box with the Json data, so that the user is able to select from subsequent ComboBoxes.
var hatComboStore = new Ext.data.JsonStore({
autoLoad: true,
fields: [
'BIN_ID',
'BIN_DESC'
],
baseParams: {
method: 'post'
},
url: 'json_bin_hat.php'
});
var hatCombo = new Ext.form.ComboBox({
allowBlank: false,
autoSelect: true,
displayField: 'BIN_DESC',
emptyText: 'Select a hat...',
fieldLabel: 'Hat',
forceSelection: false,
hiddenName: 'hatId',
itemId: 'hatId',
listEmptyText: 'No records found',
listeners: {
afterrender: function(combo, record, index) {
var hat = combo.getValue();
binCombo.store.baseParams.hat = hat;
},
select: function(combo, record, index) {
var hat = combo.getValue();
binCombo.store.baseParams.hat = hat;
},
focus: function(combo) {
binCombo.clearValue();
}
},
mode: 'remote',
msgTarget: 'side',
name: 'hatDesc',
store: hatComboStore,
submitValue: true,
triggerAction: 'all',
valueField: 'BIN_ID'
});
Anyone have any ideas? Thanks for your help!
I've got it to work using an override method.
Basically, the displayValue is what I wanted to display ('Standard') and the value is what I wanted to execute (value: 1). Apparently, the remote store is a little tricky to work with, so that's why the override was necessary.
var hatComboStore = new Ext.data.JsonStore({
autoLoad: true,
fields: [
'BIN_ID',
'BIN_DESC'
],
baseParams: {
method: 'post'
},
url: 'json_bin_hat.php'
});
var hatCombo = new Ext.form.ComboBox({
allowBlank: false,
autoSelect: true,
displayField: 'BIN_DESC',
displayValue: 'Standard',
emptyText: 'Select a hat...',
fieldLabel: 'Hat',
forceSelection: false,
hiddenName: 'hatId',
itemId: 'hatId',
listEmptyText: 'No records found',
listeners: {
afterrender: function(combo, record, index) {
var hat = combo.getValue();
binCombo.store.baseParams.hat = hat;
},
select: function(combo, record, index) {
var hat = combo.getValue();
binCombo.store.baseParams.hat = hat;
},
focus: function(combo) {
binCombo.clearValue();
}
},
mode: 'remote',
msgTarget: 'side',
name: 'hatDesc',
store: hatComboStore,
submitValue: true,
triggerAction: 'all',
valueField: 'BIN_ID'
value: 1
});
//Override method to default hatCombo value to 'Standard' while underlying value = 1
Ext.override(Ext.form.ComboBox, {
initValue: Ext.form.ComboBox.prototype.initValue.createSequence(function() {
if (this.mode == 'remote' && !!this.valueField && this.valueField != this.displayField && this.displayValue) {
if (this.forceSelection) {
this.lastSelectionText = this.displayValue;
}
this.setRawValue(this.displayValue);
}
})
});
Looks like the value prop is not working for remote stores. Probably the combo is rendered before the store has loaded. You can set the value after the store has loaded.
This works for me (tested as an ExtJS 3.4 fiddle).
var hatComboStore = new Ext.data.JsonStore({
autoLoad: true,
fields: [
'id',
'title'
],
url: 'https://jsonplaceholder.typicode.com/todos'
});
var hatCombo = new Ext.form.ComboBox({
fieldLabel: 'Hat',
store: hatComboStore,
displayField: 'title',
valueField: 'id',
mode: 'local',
submitValue: true,
triggerAction: 'all',
// Apparently does not work with remote store.
// value: '1'
});
hatComboStore.on('load', () => hatCombo.setValue('1'));
new Ext.form.FormPanel({
items: hatCombo,
renderTo: Ext.getBody()
});
see example of setting default value:
Combo config:
{
triggerAction: 'all',
id: 'dir_id',
fieldLabel: 'Direction',
queryMode: 'local',
editable: false,
xtype: 'combo',
store : dirValuesStore,
displayField:'name',
valueField:'id',
value: 'all',
width: 250,
forceSelection:true
}
source: Extjs 4 combobox default value

Extjs combobox to display records with value fetched from data store rest/jsp request

With ExtJs (4.2) version, wanted to know how to filter the empty records from the combobox dropdown list.
The code uses Extjs store to fetch a data from jsp, the result is a json object. Say below is the results rendered,
{ "data" : [
{"source":"system1","key1" : "value system1", "key2" : " value for system1"},
{"source":"system2","key1" : "value 11", "key2" : " value for key1, value 12"},
{"source":"system3" }, //Blank or empty key1, key2 record.
....
}
Js code with ExtJs
I am using ExtJs to display a combobox (Dropdown) below is the code fragment
var dataStore = Ext.create('Ext.data.Store', {
autoLoad: true,
fields: ['key1', 'key2'],
proxy:
{
type: 'ajax',
url: '<url to a jsp file which retruns json data>',
reader:
{
type: 'json',
root: 'templates'
}
}
});
...
var screenPanel = {
xtype: 'fieldset',
title: 'Panel - Data',
margin: '0 10 15 15',
padding: 10,
flex: 1,
width:"100%",
layout: 'anchor',
defaults: {
labelWidth: 160,
},
items: [
{
xtype: 'checkbox',
name: 'visibility',
id: 'visibility',
fieldLabel : 'Check to enable',
value:false
},
{
xtype: 'combobox',
anchor: '80%',
name: 'combodropdown',
itemId: 'combo1',
fieldLabel: 'Dropdown selection',
displayField: 'key2',
valueField: 'key1',
store: dataStore,
},{
xtype: 'container',
anchor: '80%',
....
The combox dropdown is listing the empty record as well. Is there a way to filter that value. In the above case the "source" : "system3" doesn't have key1 and key2 value but the xtype:combo (name: combodropdown) is listing a blank item as well.
Is there any example, on using an event to filter the data that are empty. like onClick event to filter the data like below
....
{
xtype: 'combobox',
anchor: '80%',
name: 'combodropdown',
itemId: 'combo1',
fieldLabel: 'Dropdown selection',
displayField: 'key2',
valueField: 'key1',
store: dataStore,
//some thing like this
onClick: function(value1,value2){
alert('clicked combox dropdown')
//data store value empty return something
}
You can use store filter. Something like:
Ext.application({
name: 'Fiddle',
launch: function () {
var dataStore = Ext.create('Ext.data.Store', {
autoLoad: true,
fields: ['key1', 'key2'],
proxy: {
type: 'ajax',
url: 'comboData.json',
reader: {
type: 'json',
root: 'data'
}
},
filters: [
function (item) {
return !Ext.isEmpty(item.get('key1')) && !Ext.isEmpty(item.get('key2'));
}
]
});
Ext.create('Ext.form.Panel', {
title: "Sample Panel",
items: [{
xtype: 'combobox',
name: 'combodropdown',
itemId: 'combo1',
fieldLabel: 'Dropdown selection',
displayField: 'key2',
valueField: 'key1',
store: dataStore,
listeners: {
expand: function(comboBox) {
console.log(comboBox.getStore().getRange());
}
}
}],
renderTo: Ext.getBody()
})
}
});

ExtJS Combobox Error: Cannot read property 'store' of undefined

I have a Tab panel with few tabs. One of them contains grid and the grid contains 3 itmes:
1 item with editor type "textfield", and 2 items with editor of type "combobox".
The problem: I want to filter the combobox store based on the previous combobox. But for some reason it works only the first time. After that the store returns undefined.
Here is my code:
items:[{
xtype: 'grid',
id:'schema',
border: false,
data:[],
columns:
[{
text : 'Size',
dataIndex: 'size',
id: "SizeDropdown",
width : 200,
sortable : true,
editor : {
xtype: 'combobox',
id:'SelectSize',
editable:true,
valueField: 'typeValue',
displayField: 'typeValue',
mode:'local',
lastQuery: '',
listeners:{
},
store: new Ext.data.SimpleStore({
fields: ['size', 'typeValue'],
data: [
['char', '12'],
['char', '30'],
['char', '31'],
['int', '250'],
['int', '500'],
['int', '1000'],
]
}),
allowBlank: false,
validator: function(input){
return true;
}
}
}],
listeners: {
beforeitemclick: function (eventThis, record, rowIndex, e) {
var SizeStore = Ext.getCmp('SizeDropdown').editor.store
SizeStore.clearFilter();
SizeStore.filter('size', record.data.type);
}
}
'record.data.type' returns 'char' or 'int', depending on the previous combobox and the filtering works okay. But only the first time. After that it breaks here:
var SizeStore = Ext.getCmp('SizeDropdown').editor.store
And returns:
Cannot read property 'store' of undefined
I'm using ExtJs "4.0.7"
Declaring the store outside the Tab class did the job.
Here is what I did:
var sizeDropdownStore = new Ext.data.SimpleStore({
fields: ['size', 'typeValue'],
data: [
['char', '12'],
['char', '30'],
['char', '31'],
['int', '250'],
['int', '500'],
['int', '1000'],
]
});
...{
xtype: 'combobox',
id:'SelectSize',
editable:true,
valueField: 'typeValue',
displayField: 'typeValue',
mode:'local',
listeners:{
},
store: sizeDropdownStore,
allowBlank: false
}...

How to search item with keyboard letter key in extjs combobox? Javascript

I have a combobox I can get all data I want but when i enter a letter, the data should be choosen in combobox
forexample,my variables: ankara, aston, amasya, bolu, berlin, ....
When i enter 'a' letter ankara should be selected. if i enter 'as' word aston should be selected how can i do it? thanks..
new Ext.form.ComboBox({
id : 'il3',
fieldLabel: dil('B Merkez İli'),
hiddenName : 'b_il_id_hid',
name : 'b_il_id',
store: ilStore,
valueField:'id',
queryMode: 'local',
displayField:'isim',
typeAhead: true,
triggerAction: 'all',
emptyText: dil('İl Seçiniz...'),
selectOnFocus:true,
anchor: '100%',
listeners:{
select:{
fn:function(combo, value) {
var modelCmp = Ext.getCmp('ilce3');
modelCmp.setDisabled(false);
modelCmp.store.removeAll();
modelCmp.setValue('');
modelCmp.store.reload({
params: {
id: combo.getValue()
}
});
}
}
},
allowBlank:false
})
store:
var ilStore = new Ext.data.JsonStore({
root: 'rows',
totalProperty: 'results',
idProperty: 'id',
remoteSort: true,
autoLoad : true,
fields: [
'id', 'isim'
],
baseParams:{
'tip':'il'
},
listeners:{
beforeload:function(dukan,nesne){
var modelCmp = Ext.getCmp('id-faz-yon1');
dukan.baseParams.faz = modelCmp.getValue();
},
keyup: function() {
this.store.filter('isim', this.getRawValue(), true, false);
}
},
proxy: new Ext.data.HttpProxy({
url: 'phps/sabit_agac_arama.php?lang=dil(lang)',
method : 'POST'
})
});
Add queryMode: 'local', to the config.
See the example in the API

emptyText is submitted with the form

I am using Ext Js 3.4. I run into a problem when using myform.getForm.getValues()
emptytext is also sent in the request.
Below is the code snippets
myForm = new Ext.FormPanel({
id: 'myForm ',
items: [
{
region:'center',
border:false,
items:[center_one]
},{
region:'west',
border:false,
items :[west_one]
},{
region:'east',
border:false,
items :[east_one]
},{
region:'south',
layout:'table',
iborder:false,
items: [south_one]
}
]
});
var west_one= new Ext.form.FieldSet({
width: 282,
height:250,
layout: 'table',
items: [{
id: 'form1',
layout: 'form',
items: [field1]
},{
id: 'form2',
layout: 'form',
items: [field2]
}]
});
var field1 = new Ext.form.ComboBox({
fieldLabel: 'Field',
width: 150,
name: 'field1',
cls: 'fields field1',
id: 'field1',
store: field1Store,
displayField: 'name',
valueField: 'name',
mode: 'local',
emptyText: 'Select Field1', // This value gets submiited when no value is selected
selectOnFocus: true,
triggerAction: 'all',
forceSelection : true,
editable:true,
typeAhead:true,
});
And this is how I submit:
Ext.Ajax.request({
url: 'forms.do?submit',
method: 'POST',
params: myForm.getForm().getValues(),
success: function(response, option){
},
failure: function(){
}
});
You will Need to add this config in submitform xtype :
submitEmptyText: false
By defalut submitEmptyText is set to true.
Check it on Online Documentation HERE.
EDIT:try the following code instead of Ext.ajax:
var myForm = Ext.getCmp('myForm').getForm();
myForm.submit({
url : 'forms.do?submit',
method : 'POST',
fileUpload : true,
submitEmptyText : false,
// waitMsg : 'Saving data',
success : function(form, action) {}
});
If you just want to get the values without emptytext (for example to submit them using Ext.Ajax as in your question), use myForm.getForm().getFieldValues().

Categories