I am struggling with implementing custom command buttons in a Kendo grid that makes an AJAX call using JavaScript to call a web API post action with dynamic parameters (start, stop, restart) behind button clicks.
- datasource
dataSource = new kendo.data.DataSource({
transport: {
read:
{
url: crudServiceBaseUrl + "WindowsService",
dataType: "json",
},
destroy:
{
url: crudServiceBaseUrl + "WindowsService/Delete/?deletedby=" + clientid,
type: "DELETE"
},
create:
{
url: crudServiceBaseUrl + "WindowsService/Post",
type: "POST"
//complete: function (e) {
// $("#grid").data("kendoGrid").dataSource.read();
//}
},
update:
{
url: crudServiceBaseUrl + "WindowsService/Put/",
type: "PUT",
parameterMap: function (options, operation) {
if (operation !== "read" && options.models) {
return {
models: kendo.stringify(options.models)
};
}
}
},
},
schema:
{
model:
{
id: "WindowsServiceId",
fields: {
WindowsServiceId: { editable: true, nullable: false, type: "int" },
ServiceName: { editable: true, nullable: true, type: "string" },
ServiceStatus: { editable: true, nullable: false, type: "string" },
}
}
}
});
- kendo grid
$("#grid").kendoGrid({
dataSource: dataSource,
editable: "popup",
toolbar: ["create"],
columns: [
{
field:"ServiceName",
title: "Service",
width: '200px',
},
{
field: "ServiceStatus",
title: "Status",
width: '140px',
},
{
field: "CreatedDate",
title: "Date",
width: '140px',
},
{
command: [
{
name: "start",
text: "Start",
click: function (e) {
$.ajax({
method: "POST",
url: crudServiceBaseUrl + "WindowsService/Start?windowsserviceid=3c661827-01cf-e511-bcd8-3859f9fd735e"+"&clientid="+clientid
}).done(function (msg) {
alert("Service Started successfully");
}).fail(function () {
alert("service failed");
});
}
},
{
name: "stop",
text: "Stop",
click: function (e) {
$.ajax({
method: "POST",
url: crudServiceBaseUrl + "WindowsService/Stop?windowsserviceid=3c661827-01cf-e511-bcd8-3859f9fd735e"+"&clientid="+clientid
}).done(function (msg) {
alert("Service Stop successfully");
}).fail(function () {
alert("service failed");
});
}
},
{
name: "restart",
text: "Restart",
click: function (e) {
$.ajax({
method: "POST",
url: crudServiceBaseUrl + "WindowsService/ReStart?windowsserviceid=3c661827-01cf-e511-bcd8-3859f9fd735e"+"&clientid="+clientid
}).done(function (msg) {
alert("Service ReStarted successfully");
}).fail(function () {
alert("service failed");
});
}
},
{
name: "history",
text: "History",
click: function (e) {
alert("History");
}
}
]
}
],
//height: "500px",
pageable: {
refresh: true,
pageSizes: true,
buttonCount: 5
},
}).data("kendoGrid")
;
- html
<div id="grid"> </div>
I am facing challenges when passing the dynamic windowsserviceid which is a unique ID. Currently, I am using a static ID and it works fine. Please provide guidance on how to use a dynamic windowsserviceid in the AJAX function call. Your assistance and time are greatly appreciated. Thank you in advance.