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

[Solved] Register Click Event to Grid Item

1 Answer 176 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Christian
Top achievements
Rank 1
Christian asked on 20 Oct 2014, 01:55 PM
Hi there,

I would like to have a traffic light icon for every row of my grid. When the user clicks on the light an ajax request is triggered and the icon is updated.
However my problem is to register the event.

This is what I have:
    @(Html.Kendo().Grid<CompanyViewModel>()
          .Name("Companies")
          .DataSource(source => source
              .Ajax()
              .Read(read => read.Action("_Index", "Company"))
              .Events(events => events.Error("onError"))
          )
          .Columns(c => {
              c.Bound(i => i.Lock)
                  .ClientTemplate("<span class='glyphicon glyphicon-lock lockable'></span>");
              c.Bound(i => i.Name);
          }
    )
 
<script type="text/javascript">
 
    $(function () {
        $('.lockable').click(function (arg) {
            alert('Hello World');
            var grid = $('#Companies').data('kendoGrid');
            var currentCompany = grid.dataItem($(this).closest("tr"));
            // ajax call comes here with ID of currentCompany
        });
   });

I guess my jQuery Expression is called before the Grid is initialized.
But how is it done correctly?

Thanks for any help on that!

Chris

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimiter Madjarov
Telerik team
answered on 21 Oct 2014, 09:03 AM
Hello Christian,


You should use jQuery's on() method to attach the click handler for dynamically appended elements too.
E.g.
$("#Companies").on("click", ".lockable", function(e){
   ...
});

Regards,
Dimiter Madjarov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
Christian
Top achievements
Rank 1
Answers by
Dimiter Madjarov
Telerik team
Share this question
or