I have a grid, and I want to get all the values of a column and get the sum. I've tried grid summary but I can't get it working.
This is my grid code:
Ext.ns('dlti.view.widget');
Ext.define('dlti.view.widget.PlaylistDetailsGrid' ,{
extend: 'Ext.grid.Panel',
id: 'playlist-details',
alias: 'widget.PlaylistDetailsGrid',
forceFit: true,
stripeRows: true,
selType: 'rowmodel',
autosync: true,
height: 150,
width: 950,
store: new dlti.store.PlaylistDetailsStore(),
columns: [
{
text: 'Filename',
dataIndex: 'filename',
renderer: function renderDescTarget(val, p, record) {
var desc = '';
desc = '<p style="color:#000;font-size:12px;">' + val + '</p>';
return desc;
}
},
{
text: 'Transition',
dataIndex: 'transition',
renderer: function renderDescTarget(val, p, record) {
var desc = '';
desc = '<p style="color:#000;font-size:12px;">' + val + '</p>';
return desc;
}
},
{
text: 'Stay Time',
dataIndex: 'timeframe',
renderer: function renderDescTarget(val, p, record) {
var desc = '';
desc = '<p style="color:#000;font-size:12px;">' + val + '</p>';
return desc;
}
}
]
});
Please help me. I really need to finish my project.
EDIT
Ext.define('dlti.store.PlaylistDetailsStore', {
extend: 'Ext.data.Store',
model: 'dlti.model.PlaylistDetailsModel',
storeId: 'playlist-details',
proxy: {
type: 'memory',
autosync: true,
reader: {
type: 'json',
root: 'result'
}
}
});
maecy, try this if it helps:
myStore.on('load', function(store, records){
console.log(records); //see if store has any records
console.log(store.sum('fieldName')); //if store has any records, it would print sum of all values of fieldName column.
}, this);
The store already has this functionality:
Ext.define('MyModel', {
extend: 'Ext.data.Model',
fields: ['visits']
});
Ext.require('*');
Ext.onReady(function() {
var s = new Ext.data.Store({
model: MyModel,
data: [{
visits: 1
}, {
visits: 2
}, {
visits: 3
}]
});
console.log(s.sum('visits'));
});
Doc link: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.Store-method-sum
Try this new code for your store.
Ext.define('dlti.store.PlaylistDetailsStore', {
extend: 'Ext.data.Store',
config: {
model: 'dlti.model.PlaylistDetailsModel',
storeId: 'playlist-details',
proxy: {
type: 'memory',
autosync: true,
reader: {
type: 'json',
root: 'result'
}
}
}
});
i assume your grid has an ID that named "YourGridID"
and you have a grid with 3 column that named "goods | qty | price"
var grid = Ext.getCmp('YourGridID'),
store = grid.getStore();
var totalPrice = store.sum('price');
console.log(totalPrice);
if you're not familiar with console.log you can use alert to show totalPrice
summaryRenderer: function(value, summaryData, field) {
// TODO: assign value to an in-scope variable
// or pass value to another function
console.log(value.toString());
return value;
}
Related
I try to create a combobox(with autoCompleted) and a remote store.When a user types some letters in the combobox,it goes to the server to take a new Datastore.
And I get nothing in my combobox.What's wrong with my code?How can I do for this?
Here's my combobox:
Ext.define('HDDTest.view.mod.searchDetails', {
extend: 'Ext.Panel',
controller: 'home',
items: [
{
xtype: 'combobox',
width: 450,
id: 'createRelatedConceptComboBox',
name: 'createRelatedConceptComboBox',
fieldLabel: 'Test',
//hideTrigger:true,
valueField: 'text',
emptyText: 'Select Concept',
typeAhead: true,
typeAheadDelay: 350,
minChars: 1,
listeners: {
change: 'onRelatedConceptComboBoxClicked'
},
store: {
type: 'GetRelatedConceptStore'
}
}
]
});
Here's my controller:
Ext.define('HDDTest.controller.main.HomeController', {
extend: 'Ext.app.ViewController',
alias: 'controller.home',
onRelatedConceptComboBoxClicked: function (constructors, text) {
var getRelatedConceptStore = Ext.create('HDDTest.store.GetRelatedConceptStore');
getRelatedConceptStore.load({
params: {
sValue: 'text'
},
callback: function (records, success) {
},
scope: this
});
}
});
Here's my store:
Ext.define('HDDTest.store.GetRelatedConceptStore', {
extend: 'Ext.data.Store',
field: ['value', 'text'],
alias: 'store.GetRelatedConceptStore',
storeId: 'GetRelatedConceptStore',
autoSync: true,
autoLoad: true,
proxy: {
type: 'ajax',
url: "http://127.0.0.1/api/TSGH/GetAllSearchResults",
method: 'GET',
reader: {
type: 'json',
rootProperty: '',
transform: function (records) {
var data = new Array();
for (var i = 0; i < records.length; i++) {
data[i] = new Array();
data[i][0] = records[i].NCID;
data[i][1] = records[i].DEFAULT_NAME + '(' + records[i].NCID + ')';
}
console.log(data);
return data;
}
}
}
});
When I type some letters in my combobox I get nothing and without any error messages in my console window. How can I do for this?
Thanks in advance, Ben
There seems to be some error in the processing of transform() method or the Model field name for the displayField property.
Here is the working code with transform() method commented out.
Im new in extjs.
I have a grid.
When i add new row script automaticaly puts there time time_start = Ext.Date.format(new Date(), '2008-01-01\\TH:i:s'); and its work fine, extjs automaticaly sends to server time.
But when i look at a grid there is a time +3 hours.
For example if my local time is 23.00.00, extjs send to server "2008-01-01T23:00:00", but shows in a grid "02:00".
But it must show "23:00".
Its add +3 hours to a cell view, whats wrong ?
Ext.require(['Ext.data.*', 'Ext.grid.*']);
// Создаем model
Ext.define('Users', {
extend: 'Ext.data.Model',
//idProperty: 'id',
fields: [{
name: 'id',
type: 'int'
}
]
});
Ext.onReady(function() {
// Создаем store
var store = Ext.create('Ext.data.Store', {
autoLoad: true,
autoSync: true,
model: 'Users',
proxy: {
type: 'ajax',
url: 'server.php',
api: {
create: 'server.php?action=create',
read: 'server.php?action=read',
update: 'server.php?action=update',
destroy: 'server.php?action=delete'
},
reader: {
type: 'json',
rootProperty: 'data'
},
writer: {
type: 'json',
encode: true,
rootProperty: 'dataUpdate',
allowSingle: false,
writeAllFields: true,
//root:'records'
},
actionMethods: {
create: 'GET',
read: 'GET',
update: 'GET',
destroy: 'GET'
}
},
listeners: {
write: function(store, operation) {
var record = operation.getRecords()[0],
name = Ext.String.capitalize(operation.action),
verb;
if (name == 'Destroy') {
verb = 'Destroyed';
} else {
verb = name + 'd';
}
//Ext.example.msg(name, Ext.String.format("{0} user: {1}", verb, record.getId()));
}
}
}
);
var grid = Ext.create('Ext.grid.Panel', {
renderTo: document.body,
//plugins: [rowEditing],
// Редактирование
plugins: {
ptype: 'cellediting',
clicksToEdit: 1
},
listeners: {
edit: function() {
}
},
width: 1000,
height: 330,
frame: true,
title: 'Users',
store: store,
iconCls: 'icon-user',
columns: [{
text: 'id',
width: 50,
sortable: true,
dataIndex: 'id',
renderer: function(v, meta, rec) {
return rec.phantom ? '' : v;
}
},
{
header: 'Дата',
width: 70,
// sortable: true,
dataIndex: 'date',
renderer: Ext.util.Format.dateRenderer('d/m/Y'),
editor: {
completeOnEnter: false,
field: {
xtype: 'datefield',
dateFormat: 'd/m/Y',
allowBlank: false
}
}
},
{
header: 'Время начала',
width: 120,
// sortable: true,
dataIndex: 'time_start',
//format: 'H:i',
// Нужно для верного отображеия времени после редактирования в таблице
renderer: Ext.util.Format.dateRenderer('H:i'),
editor: {
completeOnEnter: false,
field: {
xtype: 'timefield',
format: 'H:i',
//name: 'timeStart1',
//fieldLabel: 'Time In',
minValue: '8:00',
maxValue: '20:00',
increment: 30,
anchor: '100%',
allowBlank: false
}
}
}
],
dockedItems: [{
xtype: 'toolbar',
items: [{
text: 'Add',
iconCls: 'icon-add',
handler: function() {
// Создаем новую задачу
// Для корректной работы с БД нужно задать ID новой строки, равной +1 от последней ID из таблицы.
var rec = new Users();
//console.log (x);("rec data= " + rec.id + " -- " + rec.data.id);
var idArr = grid.store.data.items;
var idValue = [];
for (var i = 0; i < idArr.length; i++) {
idValue.push(idArr[i].id);
}
idValue.sort(function(a, b) {
return a - b;
});
var maxId = idValue[idValue.length - 1];
console.log(maxId);
rec.id = maxId + 1;
rec.data.id = maxId + 1;
rec.date = Ext.Date.format(new Date(), 'Y-m-d');
rec.data.date = Ext.Date.format(new Date(), 'Y-m-d');
rec.time_start = Ext.Date.format(new Date(), '2008-01-01\\TH:i:s');
rec.data.time_start = Ext.Date.format(new Date(), '2008-01-01\\TH:i:s');
store.insert(0, rec);
//store.add(rac);
//grid.getView().refresh();
// rowEditing.startEdit(rec, 0);
}
}, '-', {
itemId: 'delete',
text: 'Delete',
iconCls: 'icon-delete',
disabled: false,
handler: function() {
var selection = grid.getView().getSelectionModel().getSelection()[0];
if (confirm('Вы действительно хотите удалить задачу №' + selection.id + " ?")) {
// Удлаяем
if (selection) {
store.remove(selection);
}
}
}
}]
}]
});
});
You have to write renderer on column
renderer : function(value){
var serverDate = new Date(value{2015-12-04T10:39:22});
var newFrmDate = Ext.Date.format(serverDate,'m-d-Y h:i A');
return newFrmDate;
}
you can specify your own date time format.
I have a grid. When I add new row script automatically puts there date in format "Y-m-d" and its work fine, Ext JS automatically sends to server date in format "2017-06-24" ("Y-m-d"). But when I update this date, Ext JS send to server date in format 2017-06-24T00:00:00.
Whats wrong?
Help me send to server "2017-06-24" date ("Y-m-d"), after updating.
Ext.require(['Ext.data.*', 'Ext.grid.*']);
// Создаем model
Ext.define('Users', {
extend: 'Ext.data.Model',
//idProperty: 'id',
fields: [{
name: 'id',
type: 'int'
}
]
});
Ext.onReady(function() {
// Создаем store
var store = Ext.create('Ext.data.Store', {
autoLoad: true,
autoSync: true,
model: 'Users',
proxy: {
type: 'ajax',
url: 'server.php',
api: {
create: 'server.php?action=create',
read: 'server.php?action=read',
update: 'server.php?action=update',
destroy: 'server.php?action=delete'
},
reader: {
type: 'json',
rootProperty: 'data'
},
writer: {
type: 'json',
encode: true,
rootProperty: 'dataUpdate',
allowSingle: false,
writeAllFields: true,
//root:'records'
},
actionMethods: {
create: 'GET',
read: 'GET',
update: 'GET',
destroy: 'GET'
}
},
listeners: {
write: function(store, operation) {
var record = operation.getRecords()[0],
name = Ext.String.capitalize(operation.action),
verb;
if (name == 'Destroy') {
verb = 'Destroyed';
} else {
verb = name + 'd';
}
//Ext.example.msg(name, Ext.String.format("{0} user: {1}", verb, record.getId()));
}
}
}
);
var grid = Ext.create('Ext.grid.Panel', {
renderTo: document.body,
//plugins: [rowEditing],
// Редактирование
plugins: {
ptype: 'cellediting',
clicksToEdit: 1
},
listeners: {
edit: function() {
}
},
width: 1000,
height: 330,
frame: true,
title: 'Users',
store: store,
iconCls: 'icon-user',
columns: [{
text: 'id',
width: 50,
sortable: true,
dataIndex: 'id',
renderer: function(v, meta, rec) {
return rec.phantom ? '' : v;
}
},
{
header: 'Дата',
width: 70,
// sortable: true,
dataIndex: 'date',
renderer: Ext.util.Format.dateRenderer('d/m/Y'),
editor: {
completeOnEnter: false,
field: {
xtype: 'datefield',
dateFormat: 'd/m/Y',
allowBlank: false
}
}
},
{
header: 'Время начала',
width: 120,
// sortable: true,
dataIndex: 'time_start',
//format: 'H:i',
// Нужно для верного отображеия времени после редактирования в таблице
renderer: Ext.util.Format.dateRenderer('H:i'),
editor: {
completeOnEnter: false,
field: {
xtype: 'timefield',
format: 'H:i',
//name: 'timeStart1',
//fieldLabel: 'Time In',
minValue: '8:00',
maxValue: '20:00',
increment: 30,
anchor: '100%',
allowBlank: false
}
}
}
],
dockedItems: [{
xtype: 'toolbar',
items: [{
text: 'Add',
iconCls: 'icon-add',
handler: function() {
// Создаем новую задачу
// Для корректной работы с БД нужно задать ID новой строки, равной +1 от последней ID из таблицы.
var rec = new Users();
//console.log (x);("rec data= " + rec.id + " -- " + rec.data.id);
var idArr = grid.store.data.items;
var idValue = [];
for (var i = 0; i < idArr.length; i++) {
idValue.push(idArr[i].id);
}
idValue.sort(function(a, b) {
return a - b;
});
var maxId = idValue[idValue.length - 1];
console.log(maxId);
rec.id = maxId + 1;
rec.data.id = maxId + 1;
rec.date = Ext.Date.format(new Date(), 'Y-m-d');
rec.data.date = Ext.Date.format(new Date(), 'Y-m-d');
rec.time_start = Ext.Date.format(new Date(), '2008-01-01\\TH:i:s');
rec.data.time_start = Ext.Date.format(new Date(), '2008-01-01\\TH:i:s');
store.insert(0, rec);
//store.add(rac);
//grid.getView().refresh();
// rowEditing.startEdit(rec, 0);
}
}, '-', {
itemId: 'delete',
text: 'Delete',
iconCls: 'icon-delete',
disabled: false,
handler: function() {
var selection = grid.getView().getSelectionModel().getSelection()[0];
if (confirm('Вы действительно хотите удалить задачу №' + selection.id + " ?")) {
// Удлаяем
if (selection) {
store.remove(selection);
}
}
}
}]
}]
});
});
The datefield xtype has a submitFormat config where you can specify the date format string to be submitted to the server.
https://docs.sencha.com/extjs/6.2.0/classic/Ext.form.field.Date.html#cfg-submitFormat
Update: As Selmaril mentioned in the comment, this goes into the section where you define a datefield.
{
header: 'Дата',
width: 70,
// sortable: true,
dataIndex: 'date',
renderer: Ext.util.Format.dateRenderer('d/m/Y'),
editor: {
completeOnEnter: false,
field: {
xtype: 'datefield',
dateFormat: 'd/m/Y',
submitFormat: 'd/m/Y'
allowBlank: false
}
}
}
You are working with a Model here. On that model, you have a fields config where you can define which fields contain which data ype, and in case of date, in which format it should be submitted to the server.
fields:[{
name: 'id',
type: 'int'
},{
name: 'date',
type:'date',
dateFormat:'Y-m-d'
}]
Furthermore, I see some other red flags in your code:
Don't use record.date = or record.data.date = to set the date on the record.
Don't format the date using Date.format() unless absolutely necessary. Internally, you should always work with javascript dates, not strings. Use a xtype:'gridcolumn' in your grid with the appropriate format (e.g. format:'d/m/Y') for display.
So, for instance, when programmatically updating an existing record or creating a new grid row or a new record outside store, use record.set('date', new Date()); or store.add({date:new Date()}) or Ext.create('Users',{date: new Date()}).
I use dateWriteFormat: "Y-m-d" in my models and it works perfectly to send US-classic dates to my postgres backend.
I am trying the get a combox in a grid which select something in the editor function. But the rendering value is 0 each time. After saving it works correctly.
Ext.define('Shopware.apps.Order.view.detail.BackPosition', {
override: 'Shopware.apps.Order.view.detail.Position',
initComponent: function () {
var me = this;
me.mdsupplierStore = Ext.create('Shopware.apps.Order.store.MDSupplier').load();
me.callParent(arguments);
},
getColumns: function(grid) {
var me = this;
var col = me.callOverridden(arguments);
var md_supplier = {};
grid.mdsupplierStore = me.mdsupplierStore;
var MDSupplier= {
header: 'Lieferant',
dataIndex: 'md_supplier',
flex:2,
renderer: me.supplierColumn,
editor: {
xtype: 'combobox',
editable: false,
queryMode: 'local',
allowBlank: false,
store: grid.mdsupplierStore,
displayField: 'name',
valueField: 'id',
}
};
col = Ext.Array.insert(col, 9, [MDSupplier]);
return col;
},
supplierColumn: function(value, metaData, rowRecord) {
var me = this;
console.log(value);
//value is 0 when I click the editor combobox <---
},
});
Model:
Ext.define('Shopware.apps.Order.model.MDSupplier', {
extend:'Shopware.data.Model',
idProperty : 'id',
fields:[
{ name : 'id', type: 'int'},
{ name : 'name', type: 'string'}
]
});
Store:
Ext.define('Shopware.apps.Order.store.MDSupplier',{
configure: function() {
return { controller: 'MDSupplier' };
},
/**
* Define that this component is an extension of the Ext.data.Store
*/
extend: 'Ext.data.Store',
/**
* Auto load the store after the component
* is initialized
* #boolean
*/
autoLoad: false,
/**
* Define the used model for this store
* #string
*/
model: 'Shopware.apps.Order.model.MDSupplier',
proxy : {
type : 'ajax',
url: '/backend/MDSupplier/load',
reader:{
type: 'json',
root: 'data',
totalProperty: 'total'
}
}
});
Can anybody tell me, what am I doing wrong ?
I got it. The processing of the data was wrong, so the field was everytime NULL
RowEditing with Combobox - changes (click row editing) shown "id",
should be displayed "displayField"
pic - i65.fastpic.ru/big/2014/0724/46/d7cef656f6d993bc17657486ba5b6b46.gif
when editing a row - to display the value of the field displayfield from storeServer
now displays dataindex from store
Ext.define('ModelLib', {
extend: 'Ext.data.Model',
fields: [
'trID',
'trName'
]
});
var storeServer = Ext.create('Ext.data.Store', {
autoDestroy: true,
model: 'ModelLib',
proxy: {
type: 'ajax',
api: {
read: '/api.php?lib=server&act=get'
},
reader: {
type: 'json',
root: 'fields',
idProperty: "trID"
}
},
sorters: [{
property: 'trID',
direction: 'ASC'
}]
});
storeServer.load();
Ext.define('ModelMainobjects', {
extend: 'Ext.data.Model',
fields: [
{name: 'trServerID', type: 'int'}
]
});
var store = Ext.create('Ext.data.Store', {
// destroy the store if the grid is destroyed
autoDestroy: true,
autoSync : true,
model: 'ModelMainobjects',
proxy: {
type: 'ajax',
api: {
read: '/api.php?lib=mainobjects&act=get',
update: '/api.php?lib=mainobjects&act=update'
},
reader: {
type: 'json',
root: 'fields',
idProperty: "trID"
},
writer: {
type: 'json'
}
},
sorters: [{
property: 'trID',
direction: 'ASC'
}]
});
store.load();
var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
clicksToMoveEditor: 1,
autoCancel: false
});
var rowRenderer = function(val) {
var rec = storeServer.findRecord('id', val);
return rec !== null ? rec.get("trName") : ''
};
var grid = Ext.create('Ext.grid.Panel', {
store: store,
columns: [
{
header: 'Сервер',
dataIndex: 'trServerID',
renderer: rowRenderer,
editor: {
xtype: 'combobox',
store: storeServer,
queryMode: 'local',
displayField: 'trName',
valueField: 'trID'
}
}],
width: 600,
height: 400,
plugins: [rowEditing]
});