Reputation: 261
I have to form a JSON object like
var SelectedRows= {
"item1":{"id":"1","name":"jhon","phone":"6699"},
"item2":{"id":"2","name":"Aron","phone":"7799"},
"item2":{"id":"3","name":"Iyan","phone":"8899"},
}
On check box click event, I need to add the grid row values to the JSON list. I am trying with this code:
var SelectedRows={};
$(this).delegate(":checkbox", "click", function() {
var j=0;
var item=[];
SelectedRows[item]={}; *//I guess this line creating problem*
$(this).closest("tr").find("td:visible").each(function(){
var key=headerRow[j]["i"];
if(j == 0)
{
}
else
{
SelectedRows[item][key]=$(this).text();
}
j=++j;
});
After multiple checkbox click events happening,SelectedRows
contains only last click event data.
How get all the checkboxes click events data?
Upvotes: 5
Views: 36754
Reputation: 616
You can create an json array like this
var SelectedRows= [
item1:{id:"1",name:"jhon",phone:"6699"},
item2:{id:"2",name:"Aron",phone:"7799"},
item2:{id:"3",name:"Iyan",phone:"8899"}
]
Upvotes: 2
Reputation: 261
I done like this. it is displaying exactly above format.
var SelectedRows=[];
var rowCount=1;
Used extra count variable to from item1, item2, item3 etc..
$(this).delegate(":checkbox", "click", function() {
var j=0;
var item={};
$(this).closest("tr").find("td:visible").each(function(){
var key=headerRow[j]["i"];
if(j == 0){ }
else {
item[key]=$(this).text();
}
j=++j;
});
SelectedRows["item"+rowCount]=item;
rowCount=++rowCount;
});
Upvotes: 0
Reputation: 11254
replace var item[];
by var item = 'item'+(j+1);
and result should like..
var SelectedRows= {
"item1":{"id":"1","name":"jhon","phone":"6699"},
"item2":{"id":"2","name":"Aron","phone":"7799"},
"item2":{"id":"3","name":"Iyan","phone":"8899"},
}
Upvotes: 1