Date formatting in Store - extjs

How to format store data, In my case it is returning date and I am binding it to Comobox
var comboDate = new Ext.form.ComboBox({
displayField: 'date',
valueField: 'date',
store: {
type: 'webapi',
autoLoad: true,
api: {
read: 'api/Report/GetDate'
}
}
});
It is binding it as "Mon May 9 00:00:00 EDT 2016"
I tried few things to resolve it -
dateFormat: 'c'
and
renderer: Utility.Formatting.ShortDateTime
Please suggest me how can we apply Date formatting in Store
UPDATING
Here i am adding the complete code - I am trying to bind date to a combobox in a grid.
header: 'From Date', dataIndex: 'date', editor: comboDate, renderer: comboBoxDateRenderer(comboDate)
var comboBoxDateRenderer = function (comboDate) {
return function (value) {
var idx = comboDate.store.find(comboDate.valueField, value);//Ext.util.Format.date(value, "Y-m-d")
var rec = comboDate.store.getAt(idx);
return (rec === null ? '' : rec.get(comboDate.displayField));
};
}
var comboDate = new Ext.form.ComboBox({
displayField: 'date',
valueField: 'date',
//dateFormat: 'c',
store: {
type: 'webapi',
autoLoad: true,
fields: [
{ name: 'date', type: 'date', dateFormat: 'c' }
],
api: {
read: 'api/Report/GetDate'
}
}
});

The value is a standard date object. You can use XTemplates to format the displayfield: https://fiddle.sencha.com/#fiddle/1a37
List Template:
tpl: Ext.XTemplate(
'<tpl for=".">'+
'<div class="x-boundlist-item">' +
'{[Ext.util.Format.date(values.date, "d/m/y")]}' +
'</div>'+
'</tpl>'
),
Displayfield Template:
displayTpl: Ext.XTemplate(
'<tpl for=".">'+
'{[Ext.util.Format.date(values.date, "d/m/y")]}'+
'</tpl>'
),
Keep in mind that this is only pure formatting the display output.

Related

ExtJS :Pass groupname in grouped combobox tpl dynamically

I am working on a group combobox where I need to pass group-name dynamically(from its config).
var data = [{
group: 'Fubar',
key: '1',
name: '2015 Product Development'
}, {
group: 'Fubar',
key: '2',
name: 'Message Filter'
}, {
group: 'Fubar',
key: '3',
name: '2014 Product Development (Little)'
}, {
group: 'Other',
key: '4',
name: 'Global Structure'
}, {
group: 'Other',
key: '5',
name: 'My SW'
}];
Ext.apply(combo, {
listConfig: {
tpl = new Ext.create('Ext.XTemplate',
'<tpl for=".">',
'<tpl for="group" if="this.shouldShowHeader(group)"><div class="group-header">{[this.showHeader(values.group)]}</div></tpl>',
'<div class="x-boundlist-item"><input type="checkbox" />{name}</div>',
'</tpl>', {
shouldShowHeader: function(group) {
return this.currentGroup !== group;
},
showHeader: function(group) {
this.currentGroup = group;
return group;
}
});
}
});
var combo = Ext.create('Ext.data.Store', {
fields: ['group', 'key', 'name'],
data: data
});
items: [{
xtype: 'combobox',
id: 'searchInput',
store: combo,
multiSelect: true,
labelWidth: 50,
queryMode: 'local',
displayField: 'name',
fieldLabel: 'Choose',
listConfig: {
cls: 'grouped-list'
},
tpl: tpl,
groupName: 'group'
}]
I have tried with code but not working. It giving group, property itself instead of its value.
<tpl for="combo.groupName" if="this.shouldShowHeader(combo.groupName)">
Here combo is combobox instance being used.
Tpl should be used in this way to get desired output.
'<tpl for=".">',
'<tpl for="' + combo.groupName + '" if="this.shouldShowHeader(' + combo.groupName + ')"><div class="group-header">{[this.showHeader(values.' + combo.groupName + ')]}</div></tpl>',
'<div class="x-boundlist-item"><input type="checkbox" />{name}</div>',
'</tpl>'

How to access the view from within dataview's tpl in ExtJS6?

I am trying to test against a combobox value from inside dataview's tpl:
Ext.define('MyForm', {
extend: 'Ext.form.Panel',
items: [
{
xtype: 'combo',
name: 'my_combo',
},
{
xtype: 'dataview',
tpl: new Ext.XTemplate(
'<tpl for=".">',
'<tpl if="this.test()">pass</tpl>',
'</tpl>'
,
{
test: function(){
//doesn't work
return this.getView().down('[name=my_combo]').getValue() == 'ok';
}
}),
}
]
});
This doesn't work because this is referencing to the template itself and I can't figure out how to access the view from the inside.
It is not possible to access a view in XTemplate. To achieve this you can use ViewModel, here is the code for it.
And working sencha fiddle https://fiddle.sencha.com/#fiddle/175s
Update: I updated the code to use the DataView, DataView is little tricky, i overwritten the prepareData method to pass in extra information to the template and also updating the DataView whenever the combo value is changed. Here is the fiddle with updated changes https://fiddle.sencha.com/#fiddle/175s
Ext.define('MyApp.MyPanel', {
extend: 'Ext.Panel',
xtype: 'myForm',
defaults: {
padding: 10
},
viewModel: {
stores: {
employeeStore: {
fields: ['name'],
data: [{
name: 'John'
}, {
name: 'Tempel'
}, {
name: 'George'
}, {
name: 'Milinda'
}]
},
}
},
items: [
{
xtype: 'combobox',
fieldLabel: 'Name',
name: 'nameField',
queryMode: 'local',
displayField: 'name',
valueField: 'name',
reference: 'emp',
bind: {
store: '{employeeStore}',
value: '{name}'
}
},{
xtype: 'dataview',
itemId: 'empList',
tpl: new Ext.XTemplate(
'<tpl for=".">',
'<div class="dataview-multisort-item">',
'<h3>{name}</h3>',
'<tpl if="passed">Selected</tpl>',
'</div>',
'</tpl>'
),
itemSelector: 'div.dataview-multisort-item',
bind: {
store: '{employeeStore}'
},
prepareData: function(data, index, record) {
var name = this.up().getViewModel().get('name');
var passed = record.get('name') == name;
return Ext.apply({passed: passed}, data);
}
}
],
initComponent: function() {
this.callParent(arguments);
var me = this;
// refresh the dataview when name is changed.
this.getViewModel().bind('{name}', function() {
var dataview = me.down('#empList');
dataview.refresh();
});
}
});

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;
}

Unable to select from combobox after adding empty item/row

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.

Extjs DataView ArrayStore problem

I have the following JS:
http://monobin.com/__m1c171c4e
and the following code:
Code:
var tpl = new Ext.XTemplate(
'<tpl for=".">',
'<div class="thumb-wrap" id="{Name}">',
'<div class="thumb"><img src="{ImageMedium}" title="{Name}"></div>',
'<span class="x-editable">{Name}</span></div>',
'</tpl>',
'<div class="x-clear"></div>'
);
var store = new Ext.data.ArrayStore({
fields: [{ name: 'name' }, { name: 'ImageMedium'}],
data: res.data.SimilarArtists
});
var panel = new Ext.Panel({
frame: true,
width: 535,
autoHeight: true,
collapsible: true,
layout: 'fit',
title: 'Simple DataView (0 items selected)',
items: new Ext.DataView({
store: store,
tpl: tpl,
autoHeight: true,
multiSelect: true,
overClass: 'x-view-over',
itemSelector: 'div.thumb-wrap',
emptyText: 'No images to display',
prepareData: function (data) {
data.Name = Ext.util.Format.ellipsis(data.Name, 15);
return data;
},
plugins: [
new Ext.DataView.DragSelector(),
new Ext.DataView.LabelEditor({ dataIndex: 'name' })
],
listeners: {
selectionchange: {
fn: function (dv, nodes) {
}
}
}
})
});
So binding the DataView to the child array of res.data.SimilarArtists
But nothing seems to happen?
prepareData doesnt even get called?
What am i doing wrong?
w://
The data structure you've linked to is JSON, not array data. Try switching to a JsonStore instead. Note that a JsonStore is preconfigured with a JsonReader and an HttpProxy (remote data source) and is intended for loading the data from a url. If you need JSON loaded from local data, then you'll have to create a generic store with a JsonReader and MemoryProxy.

Resources