Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox column with Kendo grid

I wanted to add a checkbox column as first column to below grid. Can some one help me how to add it?

 @(Html.Kendo().Grid(Model)
      .Name("items")
      .Columns(columns =>
      {
          columns.Bound(p => p.itemname).Title("Name");
          columns.Bound(p => p.cost).Title("Cost");
          columns.Bound(p => p.stockinhand).Title("Stock in hand");
          columns.Command(command => command.Destroy()).Width(100);
      })
     .Pageable()
             .DataSource(dataSource => dataSource
                .Server() 
                .Model(model => model.Id(p=>p.Id))
                .Destroy(update => update.Action("EditingInline_Destroy", "Grid"))
            )
)
like image 780
jestges Avatar asked Apr 24 '13 07:04

jestges


People also ask

How do I add a checkbox to a column in Kendo grid?

add this the input element: '# if(Recon){ # checked #} #' , That should do it!

How do I bind a checkbox inside a kendo grid?

What you need to do is: Define a template for displaying a checkbox. If you do not want to click twice the checkbox (the first to enter edit mode and the second to change it's value), you need to define a checkbox but bind a change event that intercepts clicks on it and change the model.

What is selectable in kendo grid?

selectable Boolean|String|Object (default: false) "row" - the user can select a single row. "cell" - the user can select a single cell. "multiple, row" - the user can select multiple rows. "multiple, cell" - the user can select multiple cells.


3 Answers

This is how I did it:

columns.Template(@<text></text>).ClientTemplate("<input type='checkbox' #= IsAdmin ? checked='checked':'' # class='chkbx' />")

and then on javascript:

   $(function () {
    $('#grid').on('click', '.chkbx', function () {
        var checked = $(this).is(':checked');
        var grid = $('#grid').data().kendoGrid;
        var dataItem = grid.dataItem($(this).closest('tr'));
        dataItem.set('IsAdmin', checked);
    })
})
like image 166
AlexFreitas Avatar answered Oct 21 '22 00:10

AlexFreitas


I normally add a boolean column in the model; like following.

@(Html.Kendo().Grid(Model)
  .Name("items")
  .Columns(columns =>
  {
      columns.Bound(p => p.status).ClientTemplate("<input type='checkbox' disabled #= status == true ? checked='checked' : '' # />");
      columns.Bound(p => p.itemname).Title("Name");
      columns.Bound(p => p.cost).Title("Cost");
      columns.Bound(p => p.stockinhand).Title("Stock in hand");
      columns.Command(command => command.Destroy()).Width(100);
  })
 .Pageable()
         .DataSource(dataSource => dataSource
            .Server() 
            .Model(model => model.Id(p=>p.Id))
            .Destroy(update => update.Action("EditingInline_Destroy", "Grid"))
        )
)

And to disable it until you press "Edit" button just add "disabled" in the ClientTemplate. That should do it. Thanks.

like image 38
Mahib Avatar answered Oct 21 '22 00:10

Mahib


Hi you can add Checkbox in Header and Column like below :

columns.Bound(p => p.Status).HeaderTemplate("<input id='selectall' class='chkbx' type='checkbox' onclick='ToggleChkBox(this.checked);' />").ClientTemplate("<input id='checkbox' onclick='grdChkBoxClick(this); ' class='chkbxq' type='checkbox' />").Sortable(false).Filterable(false).Width(30);

And FInd Checkbox click like below :

//Cell click Checkbox select
$('#Grid').on("click", "td", function (e) {
    var selectedTd = $(e.target).closest("td");
        var grdChkBox = selectedTd.parents('tr').find("td:first").next("td").find('input:checkbox');
        grdChkBox.prop('checked', !grdChkBox.prop('checked'));

});

And do Check all checkbox functionality like below :

function ToggleChkBox(flag) {
    $('.chkbxq').each(function () {
        $(this).attr('checked', flag);
    });
}
like image 24
ravisolanki07 Avatar answered Oct 21 '22 01:10

ravisolanki07