Unable to select from combobox after adding empty item/row - extjs

I implemented as said at How to add an empty item to ExtJS combobox? I can see a blank row/item as desired but I am unable to select any of the item from combobox !
Any guess ?
My code is as follow
var rModel = Ext.regModel('State', {
fields: [
{type: 'string', name: 'fips_state_code'},
{type: 'string', name: 'state_name'}
]
});
// The data store holding the states
var store = Ext.create('Ext.data.Store', {
model: 'State',
data: [{fips_state_code: '0', state_name: ' '}]
});
store.add(obj.results);
{
xtype:'combo',
id:'filterstate',
width: 250,
fieldLabel: 'Filter By State',
store: store,
queryMode: 'local',
displayField: 'state_name',
valueField: 'fips_state_code',
editable: false,
forceSelection : true,
triggerAction : 'all',
typeAhead: true,
selectOnFocus:true,
allowBlank:true,
tpl : '<tpl for=\".\"><div class=\"x-combo-list-item\">{state_name} <\/div><\/tpl>'
}

The problem is the tpl attribute, in order to select an attribute you need to add the x-boundlist-item class to your tpl. Just like this
tpl : '<tpl for=".">'+
'<div class="x-boundlist-item">'+
'<div class="list-item">{state_name} </div>'+
'</div>'+
'</tpl>'
http://jsfiddle.net/alexrom7/CnwpD/
But if you only want to apply a custom css class to every item in the combobox list. I would recommend you to do it this way
listConfig: {
// Custom rendering template for each item
getInnerTpl: function() {
return '<div class="list-item">{state_name} <\/div>';
}
}
http://jsfiddle.net/alexrom7/6Jt5T/
Working directly with the tpl could give you some trouble.

Related

ExtJS ComboBox: allow user to select no value (null)

I have an Ext ComboBox where the user should be able to choose no value. ExtJS doen't support that out of the box.
What I've tried:
use a second trigger that clears the value
Works but is not very usable. I want a better solution.
add "fake" null item to store:
While this does kind of work I would have to modify the model for that to allow null value for id. And this looks more like a hack.
set custom tpl like
'<ul class="x-list-plain">',
'<li role="option" unselectable="on" class="x-boundlist">(no selection)</li>',
'<tpl for=".">',
'<li role="option" unselectable="on" class="x-boundlist-item">{name}</li>',
'</tpl>',
'</ul>'
But now it's getting really difficult, now idea how to get that working properly.
jsfiddle:
http://jsfiddle.net/q5e3J/1/
with custom tpl: http://jsfiddle.net/q5e3J/2/
Please refer this link How to add an empty item to ExtJS combobox?
Update: jsfiddle with that solution implemented: http://jsfiddle.net/q5e3J/3/
var combo = Ext.create('Ext.form.field.ComboBox', {
renderTo: Ext.getBody(),
displayField: 'name',
valueField: 'abbr',
value: 'AL',
store: Ext.create('Ext.data.Store', {
model: 'State',
data: states
}),
queryMode: 'local',
editable: false,
emptyText: 'No Selection',
listConfig: {
tpl: '<div class="my-boundlist-item-menu">No Selection</div>'
+ '<tpl for=".">'
+ '<div class="x-boundlist-item">{name}</div></tpl>',
listeners: {
el: {
delegate: '.my-boundlist-item-menu',
click: function() {
combo.clearValue();
}
}
}
}
});
You could use the "change" listener on the combo config and check for null values:
change: function() {
if (this.getValue() === null) {
// Set default Value here
}
};

Live Search Combo Box Isn't Working ExtJS 4.2.1

I'm not sure what's wrong with my code. However after I type 4 characters on combobox, all of value will be displayed, not filtered based on characters that I have typed. Because of that my live search is broken. Please see attached image for better illustration.
I'm creating combobox inside panel as one of item
{
xtype: 'combobox',
fieldLabel: 'Guest Name',
padding: '10px 10px 20px 10px',
allowBlank: false,
id: 'guest_id_payment',
name: 'guest_id',
// Template for the dropdown menu.
// Note the use of "x-boundlist-item" class,
// this is required to make the items selectable.
tpl: Ext.create('Ext.XTemplate',
'<tpl for=".">',
'<div class="x-boundlist-item">{identity_number} - {name}</div>',
'</tpl>'
),
// template for the content inside text field
displayTpl: Ext.create('Ext.XTemplate',
'<tpl for=".">',
'{identity_number} - {name}',
'</tpl>'
),
valueField: 'identity_number',
store: 'SGuest',
height: 20,
queryMode: 'remote'
}
This is the store:
Ext.define('ghb.store.SGuest', {
extend: 'Ext.data.Store',
model: 'ghb.model.MGuest',
autoLoad: true,
autoSync: true,
proxy: {
pageParam: false,
startParam: false,
limitParam: false,
type: 'ajax',
api: {
create: '/ghb_manager/add_guest',
read: '/ghb_manager/data_guest',
update: '/ghb_manager/edit_guest',
destroy: '/ghb_manager/delete_guest'
},
reader: {
type: 'json',
root: 'data'
},
writer: {
type: 'json',
encode: true,
writeAllFields: true,
root: 'data'
},
root: 'data'
}
});
I'm also add change event listener
'#guest_id_payment':{
change: this.changeGuestCombo
},
This is the function of change event listener, loading another store (not the store of ComboBox)
changeGuestCombo: function(self, newValue, oldValue, eOpts){
var store = Ext.getStore('SReservation');
store.load({
params: {
data: self.getValue(),
}
});
},
N.B. I'm using 4.2.1
The way you currently have it setup the filtering should be handled server side. If you change queryMode: 'remote' to queryMode: 'local' then the filter should work the way you want.
queryMode: 'remote' tells the combo box to call the proxy with the value you typed and the server would have to return only matching values. This is helpful if you have a huge dataset to search
I found the problem. When we use tpl & displayTpl on Combobox, the livesearch feature won't work
When you are using custom tpl and displayTpl in your Combobox, you can define a custom Filter function, for example on key up:
// Clear the filter collection without updating the UI
store.clearFilter(true);
store.filter([
{filterFn: function(item) { return item.get("identity_number") == "[....]" }}
]);
For more information check the ExtJS documentation.

How to put a component in Triggerfield popup in ExtJS?

Is there any way to implement a component popup on Trigger Field click? For example, I have a Trigger Field, and I would like to display a Tree Grid when I click the Triggerfield. When I select a value from the Tree Grid, the Trigger Field also set the same value.
There's an example from Ext.Net that is similar to this: http://examples.ext.net/#/Form/DropDownField/Overview/
I use Sencha Arhitect 3 and ExtJS 4.2. Any help is appreciated!
Try this.
Ext.create('Ext.form.ComboBox', {
store: Ext.create('Ext.data.Store', {
fields: ['group_name', 'property'],
data: [{
"group_name": "Armed Clash",
"property": "Border Clash"
}, {
"group_name": "Armed Clash",
"property": "Militia Clash"
}, {
"group_name": "Smuggling",
"property": "Fuel"
}, {
"group_name": "Smuggling",
"property": "Humans"
}]
}),
listConfig: {
tpl: Ext.create('Ext.XTemplate',
'<ul><tpl for=".">',
'<tpl if={group_name}>',
'<tpl if="xindex == 1 || this.getGroupStr(parent[xindex - 2]) != this.getGroupStr(values)">',
'<li class="x-combo-list-group"><b>{[this.getGroupStr(values)]}</b></li>',
'</tpl>',
'</tpl>',
'<li role="option" class="x-boundlist-item" style="padding-left: 12px">{property}</li>',
'</tpl>' +
'</ul>', {
getGroupStr: function (values) {
return values.group_name
}
}
)
},
queryMode: 'local',
valueField: 'property',
displayField: 'property',
renderTo: Ext.getBody()
});
Make list collapsible using js and add icons using styles.
Can refer this fiddle http://jsfiddle.net/gilsha/82TzM/1/
Or else use Ext.ux.TreeCombo, Fiddle: http://jsfiddle.net/gilsha/ZvnaM/83/
If I were you I firstly consider using some already existing component.
Base treepicker exists as bundled extension in ExtJS framework - Ext.ux.TreePicker
Another useful implementation of treepicker is user extension Ext.ux.TreeCombo
If you want to create your own picker component it should extends from Ext.form.field.Picker
For inspiration how to create your own picker you can look into source code of Ext.ux.TreePicker or Ext.picker.Date components.
Thanks to everyone's answer, I found another solution: use the createPicker function of the TriggerField. For example, here's how I extend the TriggerField` for a Grid Picker:
Ext.define('Custom.view.GridPicker', {
extend: 'Ext.form.field.Picker',
alias: 'widget.gridpicker',
requires: [
'Ext.grid.View',
'Ext.grid.column.Column'
],
store: 'none',
idDataIndex: 'id',
nameDataIndex: 'name',
fieldLabel: 'Grid Picker',
initComponent: function() {
var me = this;
me.callParent(arguments);
},
createPicker: function() {
picker = new Ext.create('Ext.grid.Panel', {
floating: true,
hidden: true,
height: 150,
width: 400,
header: false,
store: this.store,
columns: [
{
xtype: 'gridcolumn',
width: 95,
text: 'ID',
dataIndex: this.idDataIndex
},
{
xtype: 'gridcolumn',
width: 300,
text: 'Name',
dataIndex: this.nameDataIndex
}
]
});
return picker;
}
});
Fiddle: https://fiddle.sencha.com/#fiddle/2fb
This custom component accepts 3 config : store, idDataIndex, nameDataIndex; all of them needed to display data to the grid. I think you can base on this to extend your own picker, such as Tree Grid Picker :)

How to work with combo having images inside in extjs 4.1

I try to create a combo with an image (or something else) and when I choose an option, value in combo has some options.
I create a combo box look like:
But when I choose an option that looks like:
Here is my code http://jsfiddle.net/QZqeK/
// The data store containing the list of states
var states = Ext.create('Ext.data.Store', {
fields: ['abbr', 'name'],
data : [{
"abbr":"AL",
"name":"<img src='http://icons.iconarchive.com/icons/famfamfam/silk/16/folder-picture-icon.png'>"
},
{
"abbr":"AK",
"name":"<img src='http://icons.iconarchive.com/icons/famfamfam/silk/16/folder-picture-icon.png'>"
},
{
"abbr":"AZ",
"name":"<img src='http://icons.iconarchive.com/icons/famfamfam/silk/16/folder-picture-icon.png'>"
}]
});
// Create the combo box, attached to the states data store
Ext.create('Ext.form.ComboBox', {
fieldLabel: 'Choose',
store: states,
tpl: '<tpl for="."><div class="x-boundlist-item" >{name} {abbr}</div></tpl>',
displayTpl: Ext.create('Ext.XTemplate',
'<tpl for=".">',
'{name} {abbr}',
'</tpl>'
),
queryMode: 'local',
displayField: 'abbr',
valueField: 'abbr',
renderTo: Ext.getBody()
});
How to fix that? Thanks!
You won't be able to solve this with templates. The display value of a ComboBox is used as the value of the text input field, which is why your HTML is displayed literally.
It might be kind of hackish, but you can listen for the select event and update some styles directly on the inputEl.
Note that this sample is an approximation. You may have to experiment to get the desired effect.
var urlBase = 'http://icons.iconarchive.com/icons/famfamfam/silk/16/';
// Don't use image tag, just URL of icon
var states = Ext.create('Ext.data.Store', {
fields: ['abbr', 'name'],
data: [
{abbr: 'AL', name: urlBase + 'folder-picture-icon.png'},
{abbr: 'AK', name: urlBase + 'folder-picture-icon.png'},
{abbr: 'AZ', name: urlBase + 'folder-picture-icon.png'}
]
});
Ext.create('Ext.form.field.ComboBox', {
fieldLabel: 'Choose',
store: states,
queryMode: 'local',
displayField: 'abbr',
valueField: 'abbr',
renderTo: Ext.getBody(),
tpl: [
'<tpl for=".">',
'<div class="x-boundlist-item">',
'<img src="{name}"/>{abbr}',
'</div>',
'</tpl>'
],
listeners: {
select: function (comboBox, records) {
var record = records[0];
comboBox.inputEl.setStyle({
'background-image': 'url(' + record.get('name') + ')',
'background-repeat': 'no-repeat',
'background-position': '3px center',
'padding-left': '25px'
});
}
}
});
If you are going to use this approach and combobox will be marked as invalid the red ribbon will be hiden because it is set as background image like your custom icon (combo will be only in a red frame).
To fix this you can listen to select and validitychange events and set proper style there.
Example how to get style for valid/invalid combo:
getComboBoxInputStype: function(imgPath, valid) {
return {
'background-image': valid ? 'url(' + imgPath + ')' : 'url(' + imgPath + '), url(../../Scripts/ext/images/grid/invalid_line.gif)',
'background-repeat': valid ? 'no-repeat' : 'no-repeat, repeat-x',
'background-size': valid ? '18px 18px' : '18px 18px, 4px 3px',
'background-position': valid ? '3px center' : '3px center, bottom',
'padding-left': '25px'
};
}
Remove {name} from displayTpl like below:
displayTpl:
Ext.create('Ext.XTemplate',
'<tpl for=".">',
'{abbr}',
'</tpl>'
),

How do I apply a store to a combobox in ExtJS?

{
fieldLabel: 'Tip',
name: 'SubjectType',
allowBlank: false,
xtype: 'combo',
displayField: 'name',
valueField: 'type',
typeAhead: true,
forceSelection: true,
queryMode: 'local',
store: subjectTypeStore,
listeners: {
select: function(a,selected,c){
//console.log(a);
var tets = selected[0].data.type;
console.log(tets);
//console.log(c);
}
}
},
{
name: 'SubjectID',
allowblank: false,
xtype: 'combo',
displayField: 'name',
valuefield: 'name',
typeAhead: true,
forceSelection: true,
queryMode: 'local'
}
What I want to do is to apply a combobox store to the second combobox according to the selected item in the first combobox. For example if you select Pokemons then the second combobox should load pokemonStore. You change your mind and you select Smurfs, then the second combobox loads the smurfsStore.
What I want to learn is how to apply the store to an existent combobox.
Here's a simple example JSFiddle
select: function(checkbox,records) {
comp.clearValue();
comp.bindStore(Ext.StoreMgr.lookup(records[0].data.store));
// you can change the value field if needed
comp.displayField = 'petname';
// you can change the display field if needed
comp.valueField = 'id';
// you can change the display template if needed
comp.displayTpl = new Ext.XTemplate(
'<tpl for=".">' +
'{[typeof values === "string" ? values : values["' + comp.displayField + '"]]}' +
'<tpl if="xindex < xcount">' + comp.delimiter + '</tpl>' +
'</tpl>'
);
comp.picker = null;
}

Resources