Row editing does not seem to update the underlying store - javascript

continuing with my app whose aim is to be able to edit an aggregated field and store the result of a computation on the various components of that aggregate in another field...
I am now able to properly retrieve my fields using an extension of the UserStory model, but I still cannot save my changes.
I am trying to check what's done in Ext.grid.plugin.RowEditing's edit event, and I notice that when I reach it, e.store.data.items[rowIdx].data contains all my expected values, its dirty and editing flags are false, but e.store.data.items[rowIdx].raw does NOT reflect that (it contains the Rally's original values, unmodified) - even if I try to edit the value in raw, it does not work:
plugins: [
Ext.create('Ext.grid.plugin.RowEditing', {
clicksToEdit: 1,
listeners: {
'edit': function (editor, e) {
e.store.data.items[e.rowIdx].raw.BusinessValues =
e.store.data.items[e.rowIdx].data.BusinessValues;
e.store.commitChanges();
}
}
})
]
Whole code follows, but I'm wondering if I should add a listener at model level instead?
Rally.onReady(function() {
Ext.define('BVApp', {
extend: 'Rally.app.App',
componentCls: 'app',
launch: function() {
Rally.data.ModelFactory.getModel({
type: 'UserStory',
success: function(model) {
var weights = new Array(5, 3, 1, 3, 4, 4, 2, 2);
var BvTitles = new Array("Customers Impact", "Critical Path", "Usability", "Functionality", "Security", "Performance", "Integration", "Integrity", "Business Value");
//var BvTitlesFrench = new Array("Parc Client", "Chemin Critique", "Ergonomie", "Fonctionnalité", "Sécurité", "Performance", "Intégration", "Intégrité", "Valeur Métier");
// Thanks to question http://stackoverflow.com/questions/12517383/sdk2-links-in-rally-grids-and-column-width I can now remove flex from FormattedID column...
var fixedIDwithLink = Rally.ui.grid.FieldColumnFactory.getColumnConfigFromField( model.getField( 'FormattedID' ) );
fixedIDwithLink.flex = false;
fixedIDwithLink.width = 70;
function getOneBV( record, pos, newValue ) {
var ls_BvFieldStart, ls_BvFieldEnd, ls_BvFromBvField;
if ( pos < 1 ) return newValue; // ERROR in fact...
if ( pos > 8 ) return newValue;
ls_BvFieldStart = record.data.BusinessValues.substring( 0, pos-1 );
ls_BvFromBvField = record.data.BusinessValues.substring( pos-1, pos );
ls_BvFieldEnd = record.data.BusinessValues.substring( pos, 8 );
if ( newValue ) {
ls_BvFromBvField = newValue;
record.data.BusinessValues = ls_BvFieldStart + ls_BvFromBvField + ls_BvFieldEnd;
}
return ls_BvFromBvField;
}
function getbv( as_bvHolder ) {
var li_bv1, li_bv2, li_bv3, li_bv4, li_bv5, li_bv6, li_bv7, li_bv8, li_bvtotal;
li_bv1 = as_bvHolder.substring( 0,1 );
li_bv2 = as_bvHolder.substring( 1,2 );
li_bv3 = as_bvHolder.substring( 2,3 );
li_bv4 = as_bvHolder.substring( 3,4 );
li_bv5 = as_bvHolder.substring( 4,5 );
li_bv6 = as_bvHolder.substring( 5,6 );
li_bv7 = as_bvHolder.substring( 7,8 );
li_bv8 = as_bvHolder.substring( 8,9 );
li_bvtotal =
li_bv1*weights[0] +
li_bv2*weights[1] +
li_bv3*weights[2] +
li_bv4*weights[3] +
li_bv5*weights[4] +
li_bv6*weights[5] +
li_bv7*weights[6] +
li_bv8*weights[7];
return li_bvtotal;
}
this.grid = this.add({
xtype: 'rallygrid',
model: Ext.define('BVModel', {
extend: model,
alias : 'BVModel',
fields: [
{name: 'Bv1', type: 'string', persist: false,
convert: function(v, record){ return getOneBV( record, 1, v ); }
},
{name: 'Bv2', type: 'string', persist: false,
convert: function(v, record){ return getOneBV( record, 2, v ); }
},
{name: 'Bv3', type: 'string', persist: false,
convert: function(v, record){ return getOneBV( record, 3, v ); }
},
{name: 'Bv4', type: 'string', persist: false,
convert: function(v, record){ return getOneBV( record, 4, v ); }
},
{name: 'Bv5', type: 'string', persist: false,
convert: function(v, record){ return getOneBV( record, 5, v ); }
},
{name: 'Bv6', type: 'string', persist: false,
convert: function(v, record){ return getOneBV( record, 6, v ); }
},
{name: 'Bv7', type: 'string', persist: false,
convert: function(v, record){ return getOneBV( record, 7, v ); }
},
{name: 'Bv8', type: 'string', persist: false,
convert: function(v, record){ return getOneBV( record, 8, v ); }
},
{name: 'BvTotal', type: 'string', persist: false,
convert: function( v, record ) {
var ls_scoreInfo = '';
if ( record.data.BusinessValues ) {
ls_scoreInfo = getbv( record.data.BusinessValues ) + ' ';
}
if ( record.data.Score ) {
ls_scoreInfo += '(previous: ' + record.data.Score + ')';
}
return ls_scoreInfo;
}
}
]
}),
storeConfig: {
pageSize: 30, autoLoad: true, filters: [
{
property: 'ScheduleState',
operator: '=',
value: 'Backlog'
}
//,{ property: 'FormattedID', value: 'US85792' } // US85792, US84529, US81387, US77032
],
context: this.getContext().getDataContext()
},
columnCfgs: [
fixedIDwithLink, // might want to add a select listener later to display details in a child pane ?
'Name',
'BusinessValues',
'AffectedCustomers',
{
text: BvTitles[0], dataIndex: 'Bv1', editor: { xtype: 'textfield' }, width: 70
},
{
text: BvTitles[1], dataIndex: 'Bv2', editor: { xtype: 'textfield' }, width: 70
},
{
text: BvTitles[2], dataIndex: 'Bv3', editor: { xtype: 'textfield' }, width: 70
},
{
text: BvTitles[3], dataIndex: 'Bv4', editor: { xtype: 'textfield' }, width: 70
},
{
text: BvTitles[4], dataIndex: 'Bv5', editor: { xtype: 'textfield' }, width: 70
},
{
text: BvTitles[5], dataIndex: 'Bv6', editor: { xtype: 'textfield' }, width: 70
},
{
text: BvTitles[6], dataIndex: 'Bv7', editor: { xtype: 'textfield' }, width: 70
},
{
text: BvTitles[7], dataIndex: 'Bv8', editor: { xtype: 'textfield' }, width: 70
},
{
text: BvTitles[8], dataIndex: 'BvTotal', editor: { xtype: 'textfield' }
}
],
selType: 'rowmodel',
plugins: [
Ext.create('Ext.grid.plugin.RowEditing', {
clicksToEdit: 1,
listeners: {
'edit': function (editor, e) {
e.store.data.items[e.rowIdx].raw.BusinessValues = e.store.data.items[e.rowIdx].data.BusinessValues;
e.store.commitChanges();
}
}
})
]
});
}, // end of getModel success
scope: this
});
}
});
Rally.launchApp('BVApp', {
name: 'Business Values App'
});
});

My suggestion would be to use the get & set functions of the record object to retrieve and change the data instead of digging into the "data" or "raw" fields. This way, the Ext library can manage the changes better to update the store/records properly.
So, when you want to get the value of the "BusinessValues" field, use record.get('BusinessValues'). Likewise, try record.set('BusinessValues', newValue) to set the new value in the getOneBV function. In doing this, you might be able to comment out the RowEditing plugin you added.

Related

extjs combo box getCount() on store returns 0

I am trying to get the number of items in the combo box so that I can make the first value by default visible in the combo box using the getCount() method but I see it always return 0 so cannot get the first item to be displayed in the combo box.
Code for my combo box is as shown below:
Ext.define('something....', {
controller: 'some Controller',
initComponent: function() {
var me,
me = this;
me.items = [{
xtype: 'form',
items: [{
xtype: 'combo',
itemId: 'nameId',
name:'nameId',
labelAlign: 'top',
fieldLabel: 'Name',
store: me._getNames(),
//disabled:some condition?true:false,//doesn't gray out combo
valueField:'dataId',
displayField: 'firstName',
editable: false,
listeners:{
afterrender: function(combo,component) {
var combo = me.down('#nameId');
var nameStore = combo.getStore();
var setFirstRecord = function(combo){
var nameStore = combo.getStore();
if(nameStore.getCount() === 1){
combo.setValue(nameStore.getAt(0));
}
}
if(nameStore.isLoaded() === false){
nameStore.on('load', function(nameStore){
setFirstRecord(combo);
},this,{
single:true
});
}else{
setFirstRecord(nameStore);
}
},
}
}]
}];
}
Code for the store is as below:
_getNames: function (){
var nameStore = Ext.create('Ext.data.Store', {
autoLoad: true,
proxy: {
type: 'ajax',
url: 'name.json',
reader: {
type: 'json',
rootProperty:'items',
transform: function (data) {
var data = {
items: [{
dataId: data[0].dataId,
firstName: data[0].name.firstName,
nameDetails: data[0].nameDetails
}]
}
return data;
}
},
},
fields: ['dataId', 'firstName','nameDetails']
});
return namesStore;
}
})
The result returned from the api to populate the store is as follows:
[
{
"dataId":1,
"name":{
"dataId":1,
"firstName":"Julie",
"code":"10",
"connectionList":[
"EMAIL"
]
},
"nameDetails":{
"EMAIL":{
"dataId":1,
"detail":"EMAIL"
}
}
}
]
Any suggestions on what's missing would be great!
I am written that example for you in Sencha Fiddle: https://fiddle.sencha.com/#view/editor&fiddle/3cdl
That solve your problem:
combo.getStore().on("load",
function (store, records, successful, operation, eOpts) {
if (store.getData().length > 0)
combo.setValue(store.getData().get(0).getData().id)
},
this
)
You must check if store is loaded or not and write appropriate code:
...
...
xtype: 'combo',
itemId: 'nameId',
name: 'nameId',
labelAlign: 'top',
fieldLabel: 'Name',
store: this._getNames(),
valueField: 'dataId',
displayField: 'firstName',
editable: false,
listeners: {
afterrender: function (combo) {
var store = combo.getStore();
var setFirstRecord = function (combo) {
var store = combo.getStore();
if (store.getCount() === 1) {
combo.setValue(store.getAt(0));
}
}
if (store.isLoaded() === false) {
store.on('load', function (store) {
setFirstRecord(combo);
}, this, {
single: true
});
} else {
setFirstRecord(combo);
}
}
}
...
...

Datatables date sort vs date display

I am using Datatables to display a table and I am pulling a list of datestimes from a MySQL database. These date times are not standard dates and look like this:
12/30/19 # 04:17 pm
How can I sort these accurately with Datatables?
Here is my code:
getRes(function (result) { // APPLIED CALLBACK
$('#resdatatable').DataTable({
data: result, // YOUR RESULT
order: [[ 0, "desc" ]],
autoWidth: false,
responsive: true,
columns: [
{ data: 'id', title: 'ID' },
{ data: 'bookingdatetime', title: 'Booking Date' },
{ data: 'name', title: 'Name' },
{ data: 'class', title: 'Class' },
{ data: 'pickupdatetime', title: 'Pick up' },
{ data: 'duration', title: 'Duration' },
{ data: 'dropdatetime', title: 'Drop off' },
{ data: 'age', title: 'Age' },
{ data: 'coverage', title: 'Coverage' },
{ data: 'quote', title: 'Quote' },
{
data: 'status',
title: 'Status',
render: function(data, type, row) {
let isKnown = statusList.filter(function(k) { return k.id === data; }).length > 0;
if (isKnown) {
return $('<select id="resstatus'+row.id+'" onchange="changeResStatus('+row.id+')" data-previousvalue="'+row.status+'">', {
id: 'resstatus-' + row.id, // custom id
value: data
}).append(statusList.map(function(knownStatus) {
let $option = $('<option>', {
text: knownStatus.text,
value: knownStatus.id
});
if (row.status === knownStatus.id) {
$option.attr('selected', 'selected');
}
return $option;
})).on('change', function() {
changeresstatus(row.id); // Call change with row ID
}).prop('outerHTML');
} else {
return data;
}
}
}
]
});
});
/**
* jQuery plugin to convert text in a cell to a dropdown
*/
(function($) {
$.fn.createDropDown = function(items) {
let oldTxt = this.text();
let isKnown = items.filter(function(k) { return k.id === oldTxt; }).length > 0;
if (isKnown) {
this.empty().append($('<select>').append(items.map(function(item) {
let $option = $('<option>', {
text: item.text,
value: item.id
});
if (item.id === oldTxt) {
$option.attr('selected', 'selected');
}
return $option;
})));
}
return this;
};
})(jQuery);
// If you remove the renderer above and change this to true,
// you can call this, but it will run once...
if (false) {
$('#resdatatable > tbody tr').each(function(i, tr) {
$(tr).find('td').last().createDropDown(statusList);
});
}
function getStatusList() {
return [{
id: 'Confirmed',
text: 'Confirmed'
}, {
id: 'Unconfirmed',
text: 'Unconfirmed'
}, {
id: 'Communicating',
text: 'Communicating'
}, {
id: 'Open',
text: 'Open'
}, {
id: 'Closed',
text: 'Closed'
}, {
id: 'Canceled',
text: 'Canceled'
}, {
id: 'Reallocated',
text: 'Reallocated'
}, {
id: 'No Show',
text: 'No Show'
}];
}
I need to sort bookingdatetime, pickupdatetime, dropdatetime accurately (they are currently being converted into MM/DD/YY in the PHP script)
Maybe you can prepend hidden <span> elements containing the respective unix timestamps in the cells that have dates (by manually parsing the dates). Then using such columns to sort alphabetically would practically sort time-wise.

Unable to retrieve data from jqGrid in beforeSelectRow

Im using a jqGrid:
colModel: [
{ name: 'IdTarifaAcceso', index: 'IdTarifaAcceso', hidden: true },
{ name: 'TipoTension.IdTipoTension', index: 'TipoTension.IdTipoTension' , hidden: true},
{ name: 'DsTarifaAcceso', index: 'DsTarifaAcceso', width: (pageWidth * (9.9 / 100)), stype: 'text', align: "center" },
{ name: 'TipoTension.DsTipoTension', index: 'IdTarifaAcceso', hidden: true }
],
beforeSelectRow: function (rowid, e) {
var $self = $(this),
iCol = $.jgrid.getCellIndex($(e.target).closest("td")[0]),
cm = $self.jqGrid("getGridParam", "colModel");
var rowData = $(this).jqGrid('getRowData', rowid);
When I want get the value of TipoTension.DsTipoTension in the beforeSelectRow section, I get the error of null reference:
var = rowData.TipoTension.IdTipoTension;
}
Any idea?
Thanks!

GridBooleanColumn with data first by 0 and 1 but no true and false

In my db I have a column Actif with values 0 and 1 (false and true)
I tried to use a column. Boolean with my db values 0 and 1 ... but it doesn't work.
{
xtype: 'booleancolumn',
dataIndex: 'Actif',
text: 'MyBooleanColumn',
falseText: 'Non',
trueText: 'Oui'
}
Help me please :)
My Model
Ext.define('ModuleGestion.model.UtilisateursApplicatifs', {
extend: 'Ext.data.Model',
fields: [
{
name: 'Nom'
},
{
name: 'Prenom'
},
{
name: 'Identification'
},
{
name: 'MotDePasse'
},
{
name: 'IUtilisateurApplicatif'
},
{
name: 'FonctionRepertoire'
},
{
name: 'FonctionAnimation'
},
{
name: 'FonctionFormation'
},
{
name: 'FonctionAdministration'
},
{
name: 'Actif'
}
]});
the solution is delete the double quote of the return of the values :
$resultat = json_encode($resultat );
$resultat = str_replace('"Actif":"0"', '"Actif":0', $resultat);
$resultat = str_replace('"Actif":"1"', '"Actif":1', $resultat);
I have similar situation and for work with 0 and 1 from db I use not boolean column. From db I send 0 and 1, in my column renderer I check this and make some conversions. Check my example(here's actioncolumn - it's for show icons in cells). It's one way to decision your situation.
My view:
xtype: 'actioncolumn',
width: 55,
align: 'center',
dataIndex: 'Actif',
menuDisabled: 'true'
text: '',
sortable: false,
fixed: 'true',
renderer: function (value, metadata, record) {
if (value == '0') {
//some code
}
else {
//some code
}
},
getTip: function (value, metadata, record) {
if (value == '0') {
return 'here 0';
} else {
return 'here 1';
}
}
You don't need to change model.
In your example I think you should add type of you'r field in model.
here's example
{ name: 'Actif', type: 'boolean' }

exjts hide column if all cells of the column are empty

I am trying to hide the column if all the cells in the column are empty. I am trying to do this in the column listener by iterating through the store but I guess the store isnt populated at that time. any suggestions to achieve this functionality?
Ext.define('com.abc.MyGrid' , {
extend: 'Ext.grid.Panel',
store : 'MyStore',
columns : [{
text : 'col1',
sortable : true,
dataIndex : 'col1'
}, {
text : 'col2 ',
sortable : true,
dataIndex : 'col2',
listeners:{
"beforerender": function(){
console.log(this.up('grid').store);
this.up('grid').store.each(function(record,idx){
// if all are null for record.get('col1')
// hide the column
console.log(record.get('col1'));
});
}
}
}
})
But this is isnt working. Basically the store loop in the column listener "before render" is not executing where as the above console(this.up('grid').store) prints the store with values.
Here you go, it doesn't handle everything but should be sufficient.
Ext.define('HideColumnIfEmpty', {
extend: 'Ext.AbstractPlugin',
alias: 'plugin.hideColumnIfEmpty',
mixins: {
bindable: 'Ext.util.Bindable'
},
init: function(grid) {
this.grid = grid;
this._initStates();
this.grid.on('reconfigure', this._initStates, this);
},
_initStates: function(store, columns) {
var store = this.grid.getStore(),
columns = this.grid.columns;
this.bindStore(store);
this.columns = columns;
if(store.getCount() > 0) {
this._maybeHideColumns();
}
},
/**
*#implement
*/
getStoreListeners: function() {
return {
load: this._maybeHideColumns
};
},
_maybeHideColumns: function() {
var columns = this.columns,
store = this.store,
columnKeysMc = new Ext.util.MixedCollection();
Ext.Array.forEach(columns, function(column) {
columnKeysMc.add(column.dataIndex, column);
});
Ext.Array.some(store.getRange(),function(record){
//don't saw off the branch you are sitting on
//eachKey does not clone
var keysToRemove = [];
columnKeysMc.eachKey(function(key) {
if(!Ext.isEmpty(record.get(key))) {
keysToRemove.push(key);
}
});
Ext.Array.forEach(keysToRemove, function(k) {
columnKeysMc.removeAtKey(k);
});
return columnKeysMc.getCount() === 0;
});
columnKeysMc.each(function(column) {
column.hide();
});
}
});
Here is an example:
Ext.create('Ext.data.Store', {
storeId:'simpsonsStore',
fields:['name', 'email', 'phone'],
data:{'items':[
{ 'name': 'Lisa', "email":"lisa#simpsons.com", "phone":"555-111-1224" },
{ 'name': 'Bart', "email":"bart#simpsons.com", "phone":"555-222-1234" },
{ 'name': 'Homer', "email":"home#simpsons.com", "phone":"555-222-1244" },
{ 'name': 'Marge', "email":"marge#simpsons.com", "phone":"555-222-1254" }
]},
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'items'
}
}
});
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
{ text: 'Name', dataIndex: 'name' },
{ text: 'Email', dataIndex: 'email', flex: 1 },
{ text: 'Phone', dataIndex: 'phone' },
{ text: 'Says Doh', dataIndex: 'saysDoh'}
],
plugins: {ptype: 'hideColumnIfEmpty'},
height: 200,
width: 400,
renderTo: Ext.getBody()
});
You can see in the example that saysDoh column is hidden.
If you want to iterate over the store, you need to put a listener on the load event of your store. The beforerender doesn't mean that your store is already loaded.
I would put the creation of you store in the initComponent. Something like this:
Ext.define('com.abc.MyGrid', {
extend: 'Ext.grid.Panel',
columns: [{
text: 'col1',
sortable: true,
dataIndex: 'col1'
}, {
text: 'col2 ',
sortable: true,
dataIndex: 'col2'
},
initComponent: function () {
var me = this;
//Create store
var myStore = Ext.create('MyStore');
myStore.load(); // You can remove this if autoLoad: true on your store.
//Listen to load event (fires when loading has completed)
myStore.on({
load: function (store, records, success) {
store.each(function (record, idx) {
console.log(record.get('col1'));
});
}
});
//Apply the store to your grid
Ext.apply(me, {
store: myStore
});
me.callParent();
}
});

Categories