This is a migrated thread and some comments may be shown as answers.

Custom Command Which invokes an action that returns row data to be shown

21 Answers 1915 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tolga Erdogus
Top achievements
Rank 1
Tolga Erdogus asked on 25 Jan 2013, 03:28 PM
Hi,

in one of my usecases, I have a custom command.  When clicked it does some server side calculations and returns an updated data model for the row.  This is kind of like a row edit except the editing is not done by the user directly in the row but via some business logic on the server and the updated row is returned to the grid.

The grid as to overlay this data in to the row.

Is there a way to do this with a regular click action, but then grab the returned row viewmodel and through javascript update the row?

I see the client side click event, but it seems then I would have to wire the ajax call in there.  It would be nice to razor script the action that's invoked when the command is clicked, then intercept the returned data on the client side and amend the row data.

Could you maybe post a couple of lines of code for the best practice to do this?

Thanks

21 Answers, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 29 Jan 2013, 09:53 AM
Hi Togla,

 
Basically you can achieve the desired behavior using for example Custom command button which on click event sends the current record ID to the server and when the updated data is received updates the current record using the Set method. Please check the example below:

Define custom command:

columns.Command(c => {
    c.Custom("RefreshRecord").Text("Refresh").Click("onClick");
});

onClick function:
function onClick(e) {
    grid = $("#Grid").data("kendoGrid");
    dataItem = grid.dataItem($(e.srcElement).closest("tr"));
 
    $.ajax({
        url: "/Home/ReturnNewData",
        //send current record ID to the server
        data: { id: dataItem.id },
        success: function (data) {
            //update the current dataItem with the received data from the server
            //example data: {"OrderID":4,"OrderDate":"\/Date(1343941200000)\/","OrderDescription":"NewDescription","EmployeeId":4}
            for (var property in data) {
                dataItem.set(property, data[property]);
            }
        }
    })
}

 
Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Tolga Erdogus
Top achievements
Rank 1
answered on 29 Jan 2013, 02:46 PM
Would I be able set the command button to be disabled while updating the properties?
0
Vladimir Iliev
Telerik team
answered on 31 Jan 2013, 09:39 AM
Hi Tolga,

 
You can disable all custom command buttons or only the current row custom command before making the ajax request and enable them again on success or error event - please check the updated function below:

function onClick(e) {
    grid = $("#Grid").data("kendoGrid");
    dataItem = grid.dataItem($(e.srcElement).closest("tr"));
 
    //disable the command buttons
    $("#Grid .k-grid-content")
        .find(".k-grid-RefreshRecord")
        .addClass("k-state-disabled")
        .bind("click", function (e) {
            e.preventDefault();
            e.stopPropagation();
        })
   
    $.ajax({
        url: "/Home/ReturnNewData",
        //send current record ID to the server
        data: { id: dataItem.id },
        error: function () {
 
            //enable the command buttons
            $("#Grid .k-grid-content").find(".k-grid-RefreshRecord").removeClass("k-state-disabled").off("click");
        },
        success: function (data) {
            //update the current dataItem with the received data from the server
            //example data: {"OrderID":4,"OrderDate":"\/Date(1343941200000)\/","OrderDescription":"NewDescription","EmployeeId":4}
            for (var property in data) {
                dataItem.set(property, data[property]);
            }
 
            //enable the command buttons
            $("#Grid .k-grid-content").find(".k-grid-RefreshRecord").removeClass("k-state-disabled").off("click");
        }
    })
}
Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Amit
Top achievements
Rank 2
answered on 19 Feb 2013, 02:19 PM
Hi Vladimir Iliev

i have a custom command on grid, calling below function:

<script type="text/javascript">
    function ShowSubgranteeEdit(e) {
        e.preventDefault();
        grid = $("#Grid").data("kendoGrid");
        dataItem = grid.dataItem($(e.srcElement).closest("tr"));

       // var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        $.ajax({
            url: "/SubGrantee/Edit",
            data: { id: dataItem.id },
            success: function (response) {
                $('#EditSubGrantee').html(response);
            }
        });
    }
</script>

The function is called successfully,however the view in the URL does not change, i want it to move to "/SubGrantee/Edit".

what wrong i am doing...????

Thanks in Advance

Amit
0
Vladimir Iliev
Telerik team
answered on 21 Feb 2013, 10:49 AM
Hi Amit,

 
The custom command that you posted uses jQuery Ajax method which requests data from remote page without changing the current URL. If you need to redirect the client to another page you can use for example the Location replace method.

Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Amit
Top achievements
Rank 2
answered on 28 Feb 2013, 12:26 PM
Thanks Vladimir,

That Helped...:)

I have another requirement:

below is the code i am using to bind Kendo grid...however i want to rebind my Grid to  "ReadPresidentList" after destroy method is called...How can u i achieve this...??

Thanks in Advance..
Amit
<%: Html.Kendo().Grid<BCCWebForm.Models.BCC>()
    .Name("PezGrid")
    .Columns(columns =>
    {
        columns.Bound(p => p.Role).Width(250);
        columns.Bound(p => p.FullName).Width(250);
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(160);
    })
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .DataSource(dataSource => dataSource
         
        .Ajax()
         .Read(read => read.Action("ReadPresidentList", "BCC", new { ID = ViewBag.orgID }))
          
        .Model(model =>
        {
            model.Id(p => p.ID);
            model.Field(p => p.Role).Editable(false);
        })
         
             .Update(update => update.Action("UpdatePrez", "BCC"))
             .Destroy(update => update.Action("DelPrez", "BCC"))     
    )
     
%>
 
0
Vladimir Iliev
Telerik team
answered on 28 Feb 2013, 01:19 PM
Hi Amit,

 
Basically you can use the RequestEnd event to check if the current operation is "destroy" to use the dataSource read method to refresh the grid data:

Define RequestEnd event handler:

.DataSource(dataSource => dataSource       
    .Ajax()     
    .Events(e => e.RequestEnd("onRequestEnd"))

JavaScript:
function onRequestEnd(e) {
    if (e.type == "destroy") {
        e.sender.read();
    }
}
Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Amit
Top achievements
Rank 2
answered on 28 Feb 2013, 01:30 PM
WOW..that works like charm..:)) Thanks...:)

also what JavaScript i need to use, in case i am trying to call read method from other view....if i have custom edit and create command...
i guess 
e.sender.read();
will not work in that case...??
<%: Html.Kendo().Grid<BCCWebForm.Models.SelectSubGranteeListResult>()
    .Name("Grid")
    .ToolBar(toolbar =>
    {
        toolbar.Custom().Text("Add New").Url("").HtmlAttributes(new { onclick = "PopulateAddressForm()" });
    })
    .Columns(columns =>
    {
        columns.Bound(p => p.Name).Width(150).Title("Subgrantee Name");
        columns.Bound(p => p.Address).Width(350).Title("Subgrantee Address");
        columns.Command(command => { command.Custom("Edit").Click("ShowSubgranteeEdit"); command.Destroy(); }).Width(160);
    })
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .Pageable()
    .Sortable()
    .Scrollable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("ReadSubgranteeList", "SubGrantee"))
        .Model(model => model.Id(p => p.ID))
        .Destroy(update => update.Action("Delete", "SubGrantee"))
 
    )
%>

Thanks,
Amit
0
Vladimir Iliev
Telerik team
answered on 01 Mar 2013, 06:36 AM
Hi Amit,

 
I'm not sure if I understand you correctly but you can call the read method outside the requestEnd event in the following way:

//find the grid (from cure) - change Grid with your grid name
var grid = $("#Grid").data("kendoGrid");
//use the grid dataSource read method
grid.dataSource.read();

Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Amit
Top achievements
Rank 2
answered on 01 Mar 2013, 10:31 AM
Thanks Vladimir,
but the following line will only work, if the kendo grid is on the same page...
var grid = $("#Grid").data("kendoGrid");
in my case i am trying to call read method of grid from pop-up window...??

i need something like this....

var grid = window.opener. $("#Grid").data("kendoGrid");

which could allow me to refer kendo grid of opener window and call its read method..??

Thanks in Advance
Amit
0
Amit
Top achievements
Rank 2
answered on 01 Mar 2013, 06:26 PM
ok, i got this to work....:))

var grid = window.opener.$("#Grid").data("kendoGrid");
grid.dataSource.read();
Thanks,
Amit
0
Amit
Top achievements
Rank 2
answered on 02 Mar 2013, 01:31 PM
Hi Vladimir Iliev,

i am stuck at a point where i want, the Text of my custom command button to change according to some condition, in Kendo Grid...

For Example:
the text of the button should b "Edit subgrantee Statement" where Amount is >0 else it should display "Add subgrantee Statement"


Thanks in Advance..
Amit
0
Vladimir Iliev
Telerik team
answered on 05 Mar 2013, 06:02 AM
Hi Amit,

Your last question is not related to the original topic of this support conversation, so please submit a new support ticket for it with more details about what exactly you are trying to achieve.

Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Umut
Top achievements
Rank 1
answered on 09 Mar 2015, 08:38 PM
Hi Vladimir, 
This solution is not working if i grouped my grid. The data updated but not able to refresh the row. If there is no Grouping, it is Ok. 

Regards
0
Vladimir Iliev
Telerik team
answered on 10 Mar 2015, 08:28 AM
Hello Umut,

When the Grid is grouped and the "ServerOperation" option is set to true, you should also call the "refresh" method of the Grid in order to refresh the target row:

for (var property in data) {
    dataItem.set(property, data[property]);
}
 
grid.refresh();

Regards,
Vladimir Iliev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Umut
Top achievements
Rank 1
answered on 10 Mar 2015, 09:19 AM
$.ajax({
           url: "/Wlb/wlbexplorer/umkitos",
           type: "POST",
           data: {
               ItemID: dataItem.Id
           },
           success: function (data) {
               for (var property in data) {
                   dataItem.set(property, data[property]);
               }
               grid.refresh();
           }
       });
Sorry but it is not working. 
0
Vladimir Iliev
Telerik team
answered on 10 Mar 2015, 11:07 AM
Hi Umut,

I tried to reproduce the described behavior but to no avail - the provided setup is working as expected on our side. Could you please open a new support ticket / forum post and provide runable example where the issue is reproduced? This would help us pinpoint the exact reason for this behavior.

Regards,
Vladimir Iliev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Sajitha
Top achievements
Rank 1
Veteran
answered on 03 Mar 2021, 09:18 PM

Dear All,

I have a different requirement.

In my Kendo grid the data is loading from the Database. I have a check box on top of the grid. When the user clicks on the checkbox(show closed) , it should load all the closed items in the same grid, with some additional columns and some columns from the initial grid is to be removed also. I have 2 custom command buttons also in the grid called APPROVE and DENY. I need to hide them also on the checkbox click.

 

Please guide me.

 

Thanks ,

Sajitha

 

0
Eyup
Telerik team
answered on 05 Mar 2021, 01:21 PM

Hi Sajitha,

 

This requirement is possible and the best way to achieve it would be to create 2 different grids on the same page.

Then, using a CheckBox or RadioButton you can toggle their visibility accordingly:
https://api.jquery.com/hide/

 

Regards,
Eyup
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Sajitha
Top achievements
Rank 1
Veteran
answered on 17 Mar 2021, 04:54 PM

Hi Eyup,

I have implemented to the same grid, using the same Stored procedure. And in the databound method, I am showing and hiding the columns as well.

 

Thanks for  your guidance.

 

Regards,

Sajitha

0
Eyup
Telerik team
answered on 19 Mar 2021, 03:36 AM

Hello Sajitha,

 

I am glad that you've managed to resolve the issue.

Feel free to turn to us if new questions arise.

 

Regards,
Eyup
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Tolga Erdogus
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Tolga Erdogus
Top achievements
Rank 1
Amit
Top achievements
Rank 2
Umut
Top achievements
Rank 1
Sajitha
Top achievements
Rank 1
Veteran
Eyup
Telerik team
Share this question
or