Reputation: 1075
I've got a custom widget with an onDemandGrid. I make a database call and the data gets returned to the grid in a callback function. Within the callback function I'm setting up a row onclick event, but the problem is that no matter what row I click, I always get data from the last row. Does anyone know how I could fix this to get data from the row that gets clicked in?
Here is the postCreate function that calls the database and sets the callback function:
...
postCreate: function () {
MarkTicketDB.TicketUnMarkedGet(this.dataServiceUrl,
lang.hitch(this, this.TicketUnMarkedSet)); //this works ok
}
Here is the callback function that sets the onclick event:
TicketUnMarkedSet: function (GridData) {
...
grid.on(".dgrid-row:click", function (evt) {
var row = grid.row(evt);
ID = row.data.ID; //this is always the last record in the grid
alert(ID);
});
}
Any help would be much appreciated
Thanks
Upvotes: 0
Views: 1709
Reputation: 438
In order for the click event to associate itself with a row the row requires a unique ID. If your data doesn't return with an ID for each row then the click event will not know how to distinguish between rows and will return the most recent record which in this case is the last record in your table.
Add an id property to each record in your data and that will solve the problem
Here is a jsfiddle which demonstrates this: http://jsfiddle.net/kagant15/o8mq3ks1/
var storeWithID = new Memory({ data: [
{"id" : 1, "Name": "John", "Artist": "Rock", "Album": "Roll", "Genre":"Alternative", "Year": "1995"}
]});
var storeWithNOid = new Memory({ data: [
{"Name": "John", "Artist": "Rock", "Album": "Roll", "Genre":"Alternative", "Year": "1995"},
]});
Upvotes: 1