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

Unable to get data from selected row

4 Answers 292 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alexandre
Top achievements
Rank 1
Alexandre asked on 03 Jan 2012, 05:23 PM
Greetings, I am not sure what's happening here, but I am unable to obtain data from a selected row in my code:
var selectedActivity;
 
var activityModel = kendo.data.Model.define({
  id: "_id",
  fields: {
    _id : {type:"string"},
    date: {type:"string"},
    activity : {type:"string"},
    hours : {type:"number"}
  }
});
 
dataSource = new kendo.data.DataSource({
  transport: {
    read: {
      url: '/activity',
        contentType: 'application/json; charset=utf-8',
        type: 'GET',
        dataType: 'json'
    },
    destroy: {
      url: '/activity',
      type: 'DELETE',
      dataType: 'json'
    },
    update: {
      url: '/activity',
      type: 'PUT',
      dataType: 'json'
    },
  },
  schema:{
    model: activityModel
  }
});
 
$("#grid").kendoGrid({
  dataSource: dataSource,
  sortable: true,
  selectable: true,
  rowTemplate: kendo.template($("#rowTemplate").html()),
  altRowTemplate: kendo.template($("#altRowTemplate").html()),
  columns: [
    {field:"_id", title:"id"},
    {field:"date", title:"date"},
    {field:"activity", title:"activity"},
    {field:"hours", title:"hours"}
  ],
  change: function(){
    var selectedId = this.select().data("id");
    selectedActivity = this.dataSource.get(selectedId);
    console.log("selected id:"+selectedId)
    console.log("selected activity is:"+JSON.stringify(selectedActivity));
  }
});

When my change function is triggered, my selected id is undefined. My guess is that I have a misunderstanding somewhere in my data modelling or something of that nature.

Any hints would be appreciated, thank you very much!

4 Answers, 1 is accepted

Sort by
0
Joel
Top achievements
Rank 1
answered on 03 Jan 2012, 08:47 PM
This is how I'm doing it right now

var $selectedItem = $(this.select()[0]); // Picks first selected item
var selectedId = $selectedItem.data("id");

My row template looks like this
<tr id="row-#= data.Id #" data-id="#= data.Id #">
  ...
</tr>

 
What would be really beneficial if the KendoUI team would expose single row data via the change event args. This would be similar to the detailInit event and so one could directly access it via the e.data property. This obviously would get a little trickier when multiple rows are selected.
0
Joel
Top achievements
Rank 1
answered on 04 Jan 2012, 10:18 PM
-deleted-
0
Alexandre
Top achievements
Rank 1
answered on 13 Jan 2012, 05:08 PM
Greetings, it turns out that since I was using rowTemplates, there were no data-id that were generated for me automagically, so I had to specify them like you did. Thank you for your help as well, especially since I did not know that the selected items were a list, doh!
0
Joel
Top achievements
Rank 1
answered on 14 Jan 2012, 03:17 AM
Alexandre, glad I could help.

I just wanted to let you know this is the code I finalized on with a single row selection model for my grid implementation: http://jsfiddle.net/rusev/ZpCAy/3/  (You still have to maintain the data-id on the rowTemplate as you have done now)
Tags
Grid
Asked by
Alexandre
Top achievements
Rank 1
Answers by
Joel
Top achievements
Rank 1
Alexandre
Top achievements
Rank 1
Share this question
or