Hi!
I've setup a pure client side grid that does non-batch CRUD on a local array. There is a dropdown cascading involved. After selecting Terminal, the list of Services is filtered. The data that is received for this list by ajax contains the fee of that service. I want that fee value to be taken an update the current new/edit row's fee cell in grid after the selection. Here is what I have so far:
The Grid and supporting functionality:
var terminals = [];var services = [];var kendoDataSource = new kendo.data.DataSource({ data: createRandomData(), pageSize: 5, schema: { model: { id: "ParkingServiceId", fields: { ParkingServiceId: { type: 'number', editable: false }, TerminalId: { type: 'number', validation: { required: true } }, ServiceId: { type: 'number', validation: { required: true } }, ParkingCardNumber: { type: 'string' }, ParkingCardIssueDate: { type: 'date', validation: { required: true } }, ParkingCardExpiryDate: { type: 'date', validation: { required: true } }, ParkingCardGroup: { type: 'string' }, ServiceFee: { type: 'number', validation: { min: 0, required: true } } } } }});var gridMain = $("#serviceDetailsGrid").kendoGrid({ dataSource: kendoDataSource, pageable: false, sortable: false, toolbar: ["create"], columns: [ { field: "ParkingServiceId", width: 90, title: "Id", hidden: true }, { field: "TerminalId", width: 130, title: "Terminal", template: getTerminalTitle, editor: terminalDropDownEditor }, { field: "ServiceId", title: "Service", template: getServiceTitle, editor: serviceDropDownEditor }, { field: "ParkingCardNumber", width: 100, title: "Card #" }, { field: "ParkingCardIssueDate", width: 112, title: "Issued On", format: "{0:dd-MMM-yyyy}", template: "#= kendo.toString(kendo.parseDate(ParkingCardIssueDate, 'dd-MMM-yyyy'), 'MM/dd/yyyy') #" }, { field: "ParkingCardExpiryDate", width: 112, title: "Expiry", format: "{0:dd-MMM-yyyy}", template: "#= kendo.toString(kendo.parseDate(ParkingCardExpiryDate, 'dd-MMM-yyyy'), 'MM/dd/yyyy') #" }, { field: "ServiceFee", editable: false, width: 100, title: "Fee" }, { command: ["edit", "destroy"], title: " ", width: "200px" } ], editable: "inline", cancel: function (e) { $('#serviceDetailsGrid').data('kendoGrid').dataSource.cancelChanges(); }}).data("kendoGrid");function createRandomData() { var data = []; data.push({ ParkingServiceId: 1, TerminalId: 0, ServiceId: 54, ParkingCardNumber: 'CARD113', ParkingCardIssueDate: parseDateMonthStyle('01-May-16'), ParkingCardExpiryDate: parseDateMonthStyle('01-May-17'), ParkingCardGroup: 'SomeText1', ServiceFee: 940 }); data.push({ ParkingServiceId: 2, TerminalId: 1, ServiceId: 164, ParkingCardNumber: 'CARD114', ParkingCardIssueDate: parseDateMonthStyle('01-May-16'), ParkingCardExpiryDate: parseDateMonthStyle('01-Nov-16'), ParkingCardGroup: 'SomeText2', ServiceFee: 470 }); data.push({ ParkingServiceId: 3, TerminalId: 2, ServiceId: 1554, ParkingCardNumber: 'CARD115', ParkingCardIssueDate: parseDateMonthStyle('01-May-16'), ParkingCardExpiryDate: parseDateMonthStyle('01-Aug-16'), ParkingCardGroup: 'SomeText3', ServiceFee: 235 }); return data;}function getTerminalOffline() { return terminals;}function setTerminalOffline(setter) { console.log("Terminals from cache: " + setter.length); terminals = setter;}function getServiceOffline() { return services;}function setServiceOffline(setter) { console.log("Services from cache: " + setter.length); services = setter;}function filterServices(filter) { var temp = []; $.each(getServiceOffline(), function () { if ($(this).text.indexOf(filter) > 0) { temp.push($(this)); } }); return temp;}
function getTerminalTitle(container, options) { var terminalId = container.TerminalId; for (var i = 0; i < getTerminalOffline().length; i++) { if (getTerminalOffline()[i].TerminalId === terminalId) { return getTerminalOffline()[i].TerminalName; } } return 'N/A (' + getTerminalOffline().length + ")";}function getServiceTitle(container, options) { var serviceId = container.ServiceId; for (var i = 0; i < getServiceOffline().length; i++) { if (getServiceOffline()[i].Code === serviceId) { return getServiceOffline()[i].NameEnglish; } } return 'N/A (' + getServiceOffline().length + ")";}function refreshGrid() { gridMain.dataSource.read(); gridMain.refresh();}$(function () { $.post("/Ajax/GetTerminalsCache") .done(function (response) { if (response.length > 0) { setTerminalOffline(response); $.post("/Ajax/GetAllServicesCache") .done(function (response) { if (response.length > 0) { setServiceOffline(response); refreshGrid(); } }) .fail(function (err) { console.log(err); }); } }) .fail(function (err) { console.log(err); });});The OnChange events:
function terminalDropDown_OnChange() { var companyId = $("#CompanyId").val(); var terminalId = $("input[id^='terminalsDropDownList']").data("kendoDropDownList").value(); var serviceDdl = $("input[id^='servicesDropDownList']").data("kendoDropDownList"); serviceDdl.value(""); $.post("/Ajax/GetTerminalServices", { companyId: companyId, terminalId: terminalId }) .done(function (response) { if (response.length > 0) { serviceDdl.setDataSource(response); } }) .fail(function (err) { console.log(err); });}
function serviceDropDown_OnChange(a) { var serviceDropDown = $("input[id^='servicesDropDownList']").data("kendoDropDownList"); var extract = serviceDropDown.dataItem(serviceDropDown.select()); // I have the fee at extract.Fee}The above code for terminals updates the services and I'm able to extract Fee from the dataItem of service. Now I want to update the grid current row which can be the first row in case of new record or current edit row. Once I have the the row, I need to access it's dataItem and update the ServiceFee column.
Issues/questions:
- (How to) Even after setting the ServiceFee column of the grid to be not editable, it still is editable
- (How to) After going through a lot of questions, all ways to access the current/selected/editing row of grid have failed for me. I'm not able to get the row I want and calling dataItem on what I get results in undefined
- Once #2 is solved, is it simple enough to set the grid's current row's dataItem's ServiceFee to Fee?