[英]Get row id of dynamic table in html
Below function is for creating dynamic table with data and performing clicking operations. 以下功能用于创建带有数据的动态表并执行单击操作。 When user is clicked a row we need to get that particular row id.
当用户被单击时,我们需要获取该特定的行ID。 But am always getting only last row id.
但是我总是只获取最后一行的ID。 I tried so many ways.
我尝试了很多方法。 please simplify it:
请简化一下:
function loader() {
//alert("alert is here");
//calling java function
var jsonData = window.webConnector.load();
//toaster.print(jsonData);
//parsing json data
var data=JSON.parse(jsonData);
//document.getElementById("test").innerHTML = data.pos[0].store_name;
var totalBillAmount=0.0;
var posid;
var row=[];
//creating dynamic table
for(var i=0;i<data.pos.length;i++){
posid=data.pos[i].posid;
var radioHtml = '<input type="radio" name="' + posid + '" id="radio" />';
var table = document.getElementById("mytbody");
row[i] = table.insertRow(i);
var cell1 = row[i].insertCell(0);
var cell2 = row[i].insertCell(1);
var cell3 = row[i].insertCell(2);
var cell4 = row[i].insertCell(3);
//cell1.innerHTML = radioHtml;
cell1.innerHTML = radioHtml+" "+data.pos[i].date_time;
cell2.innerHTML = data.pos[i].store_name;
cell3.innerHTML = data.pos[i].billamount;
cell4.innerHTML = posid;
totalBillAmount=totalBillAmount+parseFloat(data.pos[i].billamount);
//performing operations when row is cliked
row[i].onclick=function(){
alert(posid);//it gives only last row posid
//var rowIndex=this.parentNode.rowIndex;
var rowIndex=$(this).parent().find(row);
var row1=rowIndex.eq(i).text();
radioHtml.setChecked=true;
alert(this.parentElement.rowIndex);
//var rowindex=$(this).closest(row).index();
//above line gives 1 and -1
var rowindex=table.parentNode.rowIndex;
alert(rowindex) //it gives undefined
}
}
On-clicks are futures and you'll have to retain transient scope values by using the right closure setup (IIFE / bind etc.). 点击是期货,您必须使用正确的闭包设置(IIFE / bind等)来保留临时范围值。
row[i].onclick = (function (index) { //capture more aguments as passed
return function () {
alert(index);
//more code
}
}(i)); //pass in more arguments as needed
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.