Hi,
We are attempting to set the default values of new rows in a grid based on some user-specified filters. The desired functionality is that if, for example, the "Year Level" filter is set to "Year 1", when a new row is created it will have its Year Level value automatically set to Year 1 AND will be visible in an edit state in the grid.
We have read the previous Q&A here, however this solution is not suitable as it makes ALL rows with no "Year Level" value become visible when a new row is created.
As the "edit" event does not seem to fire when creating a new row whilst filters are applied, the implementation we are attempting involves saving those filters when the "Add" button is clicked and then re-applying them after the row has been created. This mostly works, but the new row is inserted as an un-editable dirty record and clicking its "Edit" button does nothing.
The code below is a combination of a couple of different approaches. Any guidance would be greatly appreciated!
let currentEditRow;
let currentModel;
$(".k-grid-add").click(function () {
var grid = $("#grid").data("kendoGrid");
// Save the current filters before clearing them
savedFilters = grid.dataSource.filter();
grid.dataSource.filter([]);
// Add a new row and enter edit mode
currentEditRow = grid.addRow();
grid.editRow(currentEditRow); // Attempting to force new row into edit mode here does not work
});
$("#grid").kendoGrid({
dataSource: ds,
sortable: true,
toolbar: ["create"],
columns: [
{
field: "Yearlevel",
sortable: false,
title: "Year Level",
editor: cmbEditorYearlevelForHomegroup,
template: "#=Yearlevel?.Name ?? ''#",
editable: isEditable
},
// Other columns here
{ command: ["edit", "destroy"], title: "Action", width: "180px" }],
editable: "inline",
edit: function (e) {
currentEditRow = $(e.container); // Save reference to the editing row
currentModel = e.model; // Save the current model
if (e.model.isNew()) {
var yearLevelDropdown = $("#yearLevel").data("kendoDropDownList");
e.model.set("Yearlevel", { // Update the values in the new row based on the selected filters
Code: yearLevelDropdown.value(),
Id: 0,
Name: yearLevelDropdown.text()
});
var grid = $("#grid").data("kendoGrid");
if (savedFilters) {
grid.dataSource.filter(savedFilters); // Re-apply the filters
}
if (currentEditRow && currentModel && currentModel.isNew()) {
var grid = this;
setTimeout(function () {
grid.editRow(currentEditRow); // Attempting to re-enter edit mode for the new row; this also does not work
});
}
}
}
});