About datetimefield in UTC format - extjs

I am new to ExtJS.
I have 2 datecolumns I would like to be in UTC format. To do that, I use (from the web) :
renderer: function (value) {
return moment.utc(value).format('YYYY-MM-DD HH:mm:ssZZ');
},
The 2 datecolumns are defined like this :
..., {
xtype: 'datecolumn',
header: 'Start Date',
dataIndex: 'start_date',
flex: 1,
editor: {
xtype: 'datetimefield',
itemId: 'startdt',
allowBlank: false,
format: 'd/m/Y H:i:s',
},
renderer: function (value) {
return moment.utc(value).format('YYYY-MM-DD HH:mm:ssZZ');
},
},
{
xtype: 'datecolumn',
header: 'End Date',
dataIndex: 'end_date',
flex: 1,
editor: {
xtype: 'datetimefield',
itemId: 'enddt',
allowBlank: false,
format: 'd/m/Y H:i:s',
},
renderer: function (value) {
return moment.utc(value).format('YYYY-MM-DD HH:mm:ssZZ');
},
},...
For the model :
Ext.define('xxxxx.model.Deployment', {
extend: 'Ext.data.Model',
fields: [{
name: 'id',
type: 'int',
persist: true
}, {
name: 'name',
type: 'string'
}, {
name: 'start_date',
type: 'date',
}, {
name: 'end_date',
type: 'date'
}, ...
When I click on one of the datetimefield to edit it, it adds one hour which corresponds to the local time (french) and the format is not the same :
Display :
Edition :
How to prevent this during the edition please ? I spent almost 2 days trying to fix this ...
Thanks for any help
Karim

Try to put the 'moment.utc(value).format('YYYY-MM-DD HH:mm:ssZZ');' in the convert or calculate of the model. Something like:
{
name: 'end_date',
type: 'date',
convert: function(value) { // or calculate, have a look in the api doc
return moment.utc(value).format('YYYY-MM-DD HH:mm:ssZZ');
}
}
After that remove the timezone conv from renderer.

Related

Blank Number field using Extjs

enter image description hereI have 4 columns, namely Quantity, wastage, cost, Final Cost in a grid. at the last row of my grid, I have to show consolidated Final Cost, and nothing else.
My problem is,in last row Quantity, wastage, and Cost column, 0 is shown, whereas in store data for these column aren't there for last row.
I just want to show empty value on these columns.
data: [{"Name":"Part-0000112-01","Wastage":"4.63","Unit_Cost":"7.00","Quantity":"7.2200","Type":"Fabric","Net_Consumption":"11.8500","totalCost":"82.95"},{"Name":"Part-0000114-01","Wastage":"0.0","Unit_Cost":"1.00","Quantity":"10.0000","Type":"Fabric","Net_Consumption":"10.0000","totalCost":"10.00"},{"Name":"Part-0000116-01","Wastage":"2.0","Unit_Cost":"1.00","Quantity":"10.0000","Type":"Trim","Net_Consumption":"12.0000","totalCost":"12.00",},{"Name":"Part-0000118-01","Wastage":"0.0","Unit_Cost":"0.00","Quantity":"10.0000","Type":"Trim","Net_Consumption":"10.0000","totalCost":"0.00"},{"totalCost":"104.95","Name":"Total Material Costs (InDollar)"}]
Code:
Ext.create('Ext.grid.Panel', {
store: BOMStore,
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Quantity',
dataIndex: 'Quantity',
editor: {
xtype : 'numberfield',
allowBlank : false,
listeners : {
change : function(field, newValue,o ,e) {
var quantity = field.value;
var selectedModel = this.up('grid').getSelectionModel().getSelection()[0];
var wastage = selectedModel.get('Wastage');
var unitCost = selectedModel.get('Unit_Cost');
var updateTotalCost = getTotalCost(quantity,wastage,unitCost);
selectedModel.set('totalCost', updateTotalCost);
updateRowCosting(selectedModel);
}
}
}
}, {
text: 'Wastage',
dataIndex: 'Wastage',
editor: {
xtype : 'numberfield',
allowBlank : false,
}
},{
text: 'Cost',
dataIndex: 'Unit_Cost',
editor: {
xtype : 'numberfield',
allowBlank : false,
}
},{
text: 'Total Cost',
dataIndex: 'totalCost',
editor: {
xtype : 'numberfield',
allowBlank : false,
}
}]
});
You can use renderer which can be used as a converter, so you can check if the row is last one and return empty string.
FIDDLE
Ext.create('Ext.grid.Panel', {
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'numm',
renderer: function (value, metaData, record, rowIndex, collIndex, store) {
if (store.indexOf(record) === (store.totalCount - 1)) {
return '';
}
return value;
}
}]
});
The issue got resolved.
Below is my model.
fields: [ 'Type', 'matid', 'ebomid' , 'srmid' ,'Name', 'Component_Location','Description',
'Article','Supplier_Item_Label','Supplier_Item_Description','UOM',
{ name: 'Quantity', type: 'float' },{ name: 'Wastage', type: 'float' },'Wastage_Percentage','Net_Consumption','Material_Currency','Target_Cost',
'markupInViewMode',{ name: 'Markup', type: 'float' },'markup_Percentage','Final_Material_Cost','Sourcing_Classification','Brand_Cost','Vendor_Cost','Total_Cost']
});
So, if I give type float/number in Model, and dont pass the data for corresponding columns, then defaults the value to 0.
We got to set the field type in column. xtype : 'numberfield',

Extjs Modern Grid column cell tool conditional iconCls

In a grid column config, I have a cell tool for which I'd like the iconCls of the tool to be conditional on the row record data.
},{
text: 'Favorite',
//dataIndex: 'favorite',
width: 80,
align: 'center',
cell: {
tools: {
play : {
iconCls:'x-fa yellow fa-star',
align:'center',
tooltip : 'Toggle Favorites',
handler: 'onFavoriteToggle'
}
}
}
}, {
I'd like the icon cls to be based on the row record favorite property (true/false), to be fa-star or fa-star-o
Does anyone know how to accomplish this?
I answered this elsewhere so I'll post it here as well for anyone else interested. You can use binding to do it:
Ext.application({
name : 'Fiddle',
launch : function() {
Ext.Viewport.add({
xtype: 'grid',
itemConfig: {
viewModel: true
},
store: {
data: [{
name: 'A',
fav: false
}, {
name: 'B',
fav: true
}]
},
columns: [{
dataIndex: 'name'
}, {
text: 'Favorite',
width: 80,
align: 'center',
cell: {
tools: {
play : {
bind: {
iconCls:'x-fa yellow {record.fav ? "fa-star" : "fa-star-o"}',
},
align:'center',
tooltip : 'Toggle Favorites',
handler: function(grid, context) {
var r = context.record;
r.set('fav', !r.get('fav'));
}
}
}
}
}]
});
}
});
Fiddle
You can use renderer method of gridcolumn.
In renderer function you need to use gridcell.setTools() method.
In this FIDDLE, I have created a demo as per you requirement. Hope this will guide you or help you to achieve you requirement.
var companyStore = Ext.create('Ext.data.Store', {
fields: ['name', 'price', {
name: 'lastChange',
type: 'date'
}, {
name: 'favorite',
type: 'boolean'
}],
autoLoad: true,
pageSize: null,
proxy: {
type: 'ajax',
url: 'company.json',
reader: {
type: 'json',
rootProperty: 'items'
}
}
});
Ext.create('Ext.grid.Grid', {
title: 'Company Data',
store: companyStore,
columns: [{
text: 'Company',
flex: 1,
dataIndex: 'name'
}, {
text: 'Price',
flex: 1,
dataIndex: 'price',
formatter: 'usMoney'
}, {
text: 'Last Updated',
flex: 1,
dataIndex: 'lastChange',
formatter: 'date("d M Y")'
}, {
width: 100,
text:'Favorite',
align: 'center',
renderer: function (value, rec, col, cell) {
cell.setTools({
play: {
iconCls: rec.get('favorite') ? 'x-fa yellow fa-star' : 'x-fa yellow fa-star-o',
tooltip: 'Toggle Favorites'
}
});
}
}],
height: 500,
layout: 'fit',
renderTo: Ext.getBody(),
fullscreen: true
});

ExtJS debbuging issue in jsfiddle

Trying to create an extjs app that reads csv data and parses it into a grid, I'm working from a prior jsfiddle that had a simple simpson grid now adding to it the data dynamically.
I've confirmed the csv data retrieval works and parsing it into a json format, I need assistance in how to debug this since the screen doesn't show any bug.
The fiddle is https://jsfiddle.net/32nb4aey/
...
var result_data = parsetoJSON(parsed_result)
Ext.create('Ext.data.Store', {
storeId: 'nordanStore',
fields:[ 'name', 'firstname', 'lastname', 'department', 'login', 'status', 'email'],
data: result_data
});
Ext.create('Ext.grid.Panel', {
title: 'Norda Tech User Data',
store: Ext.data.StoreManager.lookup('nordanStore'),
columns: [
{ text: 'Name', dataIndex: 'name' },
{ text: 'First Name', dataIndex: 'firstname' },
{ text: 'Last Name', dataIndex: 'lastname' },
{ text: 'Department', dataIndex: 'department' },
{ text: 'Login', dataIndex: 'login' },
{ text: 'Status', dataIndex: 'status' },
{ text: 'Email', dataIndex: 'email', flex: 1 },
],
height: 200,
width: 400,
renderTo: Ext.getBody()
});

ExtJs - Column Editor with different xtype based on record value

I have a gridpanel with rowediting plugin enabled. I was wondering if it is possible to display in the same cell either checkbox control or numberfield based on data that's being returned from server. I have not seen anything like that before and googling has not yield any results so it may be impossible at all. It looks like I have to specify different editor types not per each column but per each cell.
How can I achieve that?
P.S. I must have a chance to edit that cell, i.e. change number value or check/uncheck checkbox.
That is very easy to achieve, you will need to use the getEditor method of your grid column and get it to return the form field you want:
Example:
{
xtype: 'gridcolumn',
getEditor: function(record) {
var grid = this.up('grid'),
cellediting = grid.findPlugin('cellediting'),
editors = cellediting.editors,
editor = editors.getByKey(this.id),
fieldType;
if (editor) {
// Do this to avoid memory leaks
editors.remove(editor);
}
fieldType = isNaN(parseFloat(record.get('salary'))) ? 'textfield' : 'numberfield';
return {
xtype: fieldType,
allowBlank: false
};
},
dataIndex: 'salary',
text: 'Salary',
flex: 1
}
I have created a fiddle demonstrating the use of this method, if the column Salary holds a string it will edit with a textfield, if it holds a number, it will edit with a Numberfield.
Hope it helps
Fiddle: https://fiddle.sencha.com/#fiddle/c2m
My fiddle is working with the CellEditor plugin, you will have to make the adjustments to make it work with your RowEditor plugin, for further reference, check the documentation for Grid Column and the getEditor method.
Many thanks to Guilherme Lopes for the nice code to begin with. Here is the sample of what I finally got:
var data = [{
name: 'Richard Wallace',
age: 24,
hired: '9/21/2013',
active: true,
salary: 1,
checkbox: true
}, {
name: 'Phyllis Diaz',
age: 29,
hired: '1/27/2009',
active: false,
salary: 41244,
checkbox: false
}, {
name: 'Kathryn Kelley',
age: 23,
hired: '9/14/2011',
active: false,
salary: 98599.9,
checkbox: false
}, {
name: 'Annie Willis',
age: 36,
hired: '4/11/2011',
active: true,
salary: 0,
checkbox: true
}];
var store = Ext.create('Ext.data.Store', {
data: data,
fields: [{
name: 'name'
}, {
type: 'float',
name: 'age'
}, {
type: 'date',
name: 'hired'
}, {
type: 'boolean',
name: 'active'
}, {
name: 'salary'
}]
});
Ext.define('MyApp.view.MyGridPanel', {
extend: 'Ext.grid.Panel',
alias: 'widget.mygridpanel',
height: 315,
width: 784,
title: 'Employees',
store: store,
viewConfig: {
listeners: {
beforecellclick: function (view, cell, cellIndex, record, row, rowIndex, e) {
if (cellIndex == 4 && record.get('checkbox')) {
if (record.get('salary')) {
record.set('salary', 0);
} else {
record.set('salary', 1);
}
return false;
}
return true;
}
}
},
columns: [{
xtype: 'gridcolumn',
dataIndex: 'name',
text: 'Name',
flex: 1,
editor: {
xtype: 'textfield'
}
}, {
xtype: 'gridcolumn',
dataIndex: 'age',
text: 'Age'
}, {
xtype: 'datecolumn',
dataIndex: 'hired',
text: 'Hired',
flex: 1
}, {
xtype: 'checkcolumn',
dataIndex: 'active',
text: 'Active'
}, {
xtype: 'gridcolumn',
getEditor: function (record) {
var fieldType = record.get('checkbox') ? 'checkboxfield' : 'textfield';
return Ext.create('Ext.grid.CellEditor', {
field: {
xtype: fieldType,
allowBlank: false
}
});
},
renderer: function (value, metaData) {
if (metaData.record.get('checkbox')) {
if (metaData.record.get('salary')) {
return '<div style="text-align: center"><img class="x-grid-checkcolumn x-grid-checkcolumn-checked" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="></div>';
} else {
return '<div style="text-align: center"><img class="x-grid-checkcolumn" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="></div>';
}
}
return value;
},
dataIndex: 'salary',
text: 'Salary',
flex: 1
}],
plugins: [{
ptype: 'cellediting',
autoCancel: false,
clicksToEdit: 1
}]
});
Ext.create('MyApp.view.MyGridPanel', {
renderTo: Ext.getBody()
});
Working example on Sencha's fiddle https://fiddle.sencha.com/#fiddle/c3p
Editor contains one field, and this editor is used for the whole column. You can't specify two xtypes or multiple editors for one column.
That said, it is not completely impossible, but it will require some work.
You will have to define a new custom field with custom xtype.
Tell this field to either render a checkboxfield or a numberfield, depending on the value.
This will require you to override/implement most if not all functions that a Ext.form.field.Base has...

Displaying JSON serialized date in ext js grid

The server returns date in JSON as below,
{
"LastUpdated": "\/Date(1310117748850)\/"
}
I'm using ExtJs grid and the date is not showing up. How I can display it in M/dd/yyyy format?
this.store = new Ext.data.JsonStore({
autoLoad: {
params: {
start: 0,
limit: 10
}
},
autoDestroy: true,
url: '/home/jobs',
idProperty: 'Name',
fields: ['Name',
'Description',
'Type',
'Group',
'Data',
'Schedule.Name',
'Schedule.Description', {
name: 'LastUpdated',
type: 'date'
},
'Schedule.Expression',
'Status'],
root: 'data',
sortInfo: {
field: 'Name',
direction: 'ASC'
}
});
In Grid colModel:
{
header: 'Last Updated',
dataIndex: 'LastUpdated',
width: 80,
renderer: Ext.util.Format.dateRenderer('m/d/Y')
},
//Grid:
columns: [
{header: 'Last Updated', dataIndex: 'LastUpdated', renderer: Ext.util.Format.dateRenderer('m/d/Y')}
]
//store:
{
field:'LastUpdated',
type:'date',
convert:function(v,j){
return new Date(v.replace(/\/Date((\d+))\//, '$1'));
}
}
In the column section, you could create you own custom date renderer:
columns: [
{header: 'Last Updated', dataIndex: 'LastUpdated', renderer: dateRenderer}
]
And then create a function that processes your date:
function dateRenderer(value, id, r) {
var yourDate = r.data['uploadDate'];
// some js stuff here to strip out just the epoch time
var datevar = new Date(yourDateEpoch);
return Ext.Date.format(datevar, 'm/d/Y')
}
For date field you need also specify dateFormat. E.g.:
{name: 'LastUpdated', type: 'date', dateFormat: 'Y-m-d'}
But in your case you receive date in other format...
The only thing you need to do is to have the following in your model
{name: 'LastUpdated', type: Ext.data.Types.DATE, dateFormat: 'time' }
It tells ExtJS that the data coming into the model for this field is of form epoch time.
Use
return new Date(parseInt(v.replace('/Date(', '')));
full example below,
columns: [
{
header: 'Last Updated',
dataIndex: 'LastUpdated',
renderer: Ext.util.Format.dateRenderer('m/d/Y')
}
]
//store:
{
field:'LastUpdated',
type:'date',
convert:function(v, j) {
return new Date(parseInt(v.replace('/Date(', '')));
}
}

Resources