|
gridInstance.updateCell(rowIndex, field, value); |
gridInstance.updateCell(rowIndex, field, value); |
|
[index.js]
function cellEdit(args) {
if (args.columnName == 'Total') {
args.cancel = true;
}
}
function totalChangeFn(e) {
var value = e.value == '' ? 1 : parseInt(e.value, 10);
var currentRow = grid.getRowInfo(e.container);
var rowIndex = currentRow.rowIndex;
var addedValue = grid.getBatchChanges().addedRecords;
var rate = addedValue[0]['Rate'];
grid.updateCell(rowIndex, 'Total', rate * value);
}
function rateChangeFn(e) {
var value = e.value == '' ? 1 : parseInt(e.value, 10);
var currentRow = grid.getRowInfo(e.container);
var rowIndex = currentRow.rowIndex;
var addedValue = grid.getBatchChanges().addedRecords;
var qty = addedValue[0]['Quantity'];
grid.updateCell(rowIndex, 'Total', qty * value);
} |
Hi Balaji,
Thanks for the reply. Your suggestion helped me a lot. I can now easily make updates as I want. I have also set the primary key as you have said.
Going further into adding the next row, I came to the issue.
In both of the functions below, you are using index "0" for addedValue. But, in real application, there can be rows that are added, updated or deleted and always keeping index "0" to get added or updated values cannot get us the correct object of added or updated Value. So, how can we identify correct addedValue or updatedValue in that case?
var qty = addedValue[0]['Quantity'];
var rate = addedValue[0]['Rate'];
|
[index.js]
function totalChangeFn(e) {
var value = e.value == '' ? 1 : parseInt(e.value, 10);
var currentRow = grid.getRowInfo(e.container);
var rowIndex = currentRow.rowIndex;
var addedValue = grid.getBatchChanges().addedRecords;
var row = null;
var cell = e.container.closest('.e-rowcell');
var isAdd = cell.closest('tr').classList.contains('e-insertedrow');
if (isAdd) {
row = addedValue.length == 1 ? addedValue[0] : addedValue[rowIndex];
}
var rate = isAdd
? row['Rate']
: Number(
e.container.closest('tr').children[grid.getColumnByField('Rate').index]
.innerText
);
grid.updateCell(rowIndex, 'Total', rate * value);
}
function rateChangeFn(e) {
var value = e.value == '' ? 1 : parseInt(e.value, 10);
var currentRow = grid.getRowInfo(e.container);
var rowIndex = currentRow.rowIndex;
var addedValue = grid.getBatchChanges().addedRecords;
var row = null;
var cell = e.container.closest('.e-rowcell');
var isAdd = cell.closest('tr').classList.contains('e-insertedrow');
if (isAdd) {
row = addedValue.length == 1 ? addedValue[0] : addedValue[rowIndex];
}
var qty = isAdd
? row['Quantity']
: Number(
e.container.closest('tr').children[
grid.getColumnByField('Quantity').index
].innerText
);
grid.updateCell(rowIndex, 'Total', qty * value);
} |
Hi Balaji,
Thank you for your assistance. This helped me achieve what I wanted to do. I'm really grateful.
The other thing is, I have another grid as well but in this new grid the Edit Mode needs to be "Normal".
Here, When I change the selection of Product combobox, I want to update the Values of Rate and Total (NumericEdit fields) and IsTaxable (boolean edit field) of the same row.
I found out that "grid.updateCell()" function does not work in Normal Mode.
Thanks for helping me out?
Regards,
Prabesh
|
[index.js]
var grid = new ej.grids.Grid({
dataSource: [],
toolbar: ['Add', 'Cancel', 'Update'],
editSettings: {
showConfirmDialog: true,
showDeleteConfirmDialog: true,
allowEditing: true,
allowAdding: true,
allowDeleting: true,
mode: 'Normal'
},
columns: [
{
field: 'Id',
headerText: 'ID',
type: 'number',
isPrimaryKey: true,
width: 80
},
{
field: 'ProductId',
headerText: 'ProductId',
type: 'number',
visible: false
},
{
field: 'Product',
headerText: 'Product',
type: 'string',
edit: {
create: function() {
productElem = document.createElement('input');
return productElem;
},
read: function() {
return productDDL.text;
},
destroy: function(e) {
productDDL.destroy();
},
write: function(args) {
productDDL = new ej.dropdowns.ComboBox({
dataSource: dropdownData,
fields: { text: 'product', value: 'product' },
placeholder: 'Select a Product',
allowFiltering: true,
filtering: e => {
if (!e.text || e.text.length < 3) {
return;
} else {
var query = new ej.data.Query().addParams(
'searchText',
e.text.trim()
);
e.updateData(productDDLSource, query);
}
},
change: item => {
var tr = item.element.closest('tr');
[].slice.call(tr.children).forEach(td => {
var comp = td.querySelector('.e-control');
if (comp) {
if (comp.id == grid.element.id + 'Rate') {
comp.ej2_instances[0].value = item.itemData.rate;
}
}
});
}
});
productDDL.appendTo(productElem);
}
}
},
{
field: 'Rate',
headerText: 'Rate',
type: 'number',
editType: 'numericedit',
edit: { params: { change: rateChangeFn } }
},
{
field: 'Quantity',
headerText: 'Quantity',
type: 'number',
editType: 'numericedit',
edit: { params: { change: totalChangeFn } }
},
{ field: 'Total', headerText: 'Total', type: 'number' }
],
height: 315
});
grid.appendTo('#Grid');
function totalChangeFn(e) {
qty = e.value;
if (e.isInteracted) {
var tr = e.event.target.closest('tr');
[].slice.call(tr.children).forEach(td => {
var comp = td.querySelector('.e-control');
if (comp) {
if (comp.id == grid.element.id + 'Total') {
comp.ej2_instances[0].value = rate * e.value;
}
}
});
}
}
function rateChangeFn(e) {
rate = e.value;
if (e.isInteracted) {
var tr = e.event.target.closest('tr');
[].slice.call(tr.children).forEach(td => {
var comp = td.querySelector('.e-control');
if (comp) {
if (comp.id == grid.element.id + 'Total') {
comp.ej2_instances[0].value = qty * e.value;
}
}
});
}
} |
Dear Balaji,
I have achieved almost everything I wanted to so far with this grid. Thanks to you.
But I had to make a few changes. The rateChangeFn and totalChangeFn did not execute when rate/quantity fields were updated when editType was set to 'numerictext'. But when I removed editType entirely, the changeFn were executed as I wanted.
Also, js could not find the "e.isInteracted" property so I removed the if clause to check "e.isInteracted" entirely. And I have achieved almost everything so far.
However, I also need the ProductId to be in the row object to save this all to the database.
I use "grid.currentViewData" to get row objects but the ProductId is not set. I tried to udpate the productId the same way you updated Total Using td.querySelector('.e-control') But turns out it does not provide an input control if it's not visible. Is there a way to set ProductId property to set to the row so that whenever Product is changed, it sets a productId to row object.
I tried making it happen with Product Combobox by setting the Value portion of Product Combobox as ProductId. I did following to set value of Combobox as ProductId
and this to read function of Combobox
But turns out when the row is set back to view mode from edit mode, the ProductId is shown instead of Product in Grid view.
I hope you get what I mean. Please let me know if you need any information from me.
Thanks and Regards,
Prabesh
|
[index.js]
change: item => {
var tr = item.element.closest('tr');
[].slice.call(tr.children).forEach(td => {
var comp = td.querySelector('.e-control');
if (comp) {
if (comp.id == grid.element.id + 'Rate') {
comp.ej2_instances[0].value = item.itemData.rate;
}
}
});
if (grid.isEdit && grid.currentViewData.length > 0) {
var currentRow = grid.getRowInfo(item.element.closest('tr'));
var row = currentRow.rowData;
row['productId'] = item.value;
row['Product'] = item.itemData['product'];
grid.updateRow(currentRow.rowIndex, row);
}
}
function actionBegin(args) {
if (args.requestType == 'save' && args.action == 'add') {
var row = dropdownData.filter(x => x.product == args.data['Product'])[0];
args.data['productId'] = row['productId'];
}
} |