user1392853
user1392853

Reputation: 269

Get value of a column in kendo grid with checkbox

I have a grid with checkbox

                        @(Html.Kendo().Grid<Re.Web.ReService>()
                    .Name("ServiceList")
                        .Columns(columns =>
                        {
                            columns.Bound(p => p.PKiServiceID)
                            .ClientTemplate("<input name=\"selectedIds\" type=\"checkbox\" value=\"#=PKiServiceID#\" class=\"chk\"/>")
                            .HeaderTemplate("<input type=\"checkbox\" class=\"selectAll\" />")
                            .Width(30);
                            columns.Bound(p => p.SServiceName).Width(200);
                            columns.Bound(p => p.MServicePrice).Width(80);
                            columns.Bound(p => p.BStatus).Width(70).ClientTemplate("#if(BStatus){# #='Y'# #} else {# #='N'# #}#");
                            columns.Bound(p => p.SDescription);
                        })
                    .Selectable()
                    .Pageable()
                    .DataSource(dataSource => dataSource
                        .Ajax()
                        .Model(model => model.Id(p => p.PKiServiceID))
                        .Read(read => read.Action("ServiceRead", "Home"))
                    )
                    )

and the javascript

    $(document).ready( function () {
    var grid = $("#ServiceList").data("kendoGrid");

    //handle the click of the header checkbox
    grid.table.on("change", ".selectAll", function () {
        var checkbox = $(this);
        if (checkbox.is(':checked')) {
            grid.table.find("tr")
                .find("td:first input")
                .attr("checked", checkbox.is(":checked"));

        }
        else {
            grid.table.find("tr")
                .find("td:first input")
                .attr("checked", checkbox.is(":checked"));
        }
    });
});

I can check/uncheck all row in grid Now I want if I click in the select all checkbox, i get all value of the column MServicePrice so I can calulate the total value. I tried using the $(this).closest('tr') but It's not working

Upvotes: 0

Views: 8503

Answers (1)

Petur Subev
Petur Subev

Reputation: 20193

Inside of the check all handler means all the items in the Grid will be checked, so you are asking how to get the value for all the items basically?

You can use the dataSource and iterate through the view() collection.

e.g.

function checkAll(ele) {        
    var grid = $('#gridName').data().kendoGrid;
    var total = 0;
    $.each(grid.dataSource.view(), function () {           
        total += this.Salary;            
    });
    alert(total);        
}

Upvotes: 2

Related Questions