[英]I want to get row Id from Table
In my application I have loaded some information's to the table.在我的应用程序中,我已将一些信息加载到表中。
Now I want to get the item Id of that row when user clicks.现在我想在用户点击时获取该行的项目 ID。
Here Item.Id is the one I want to get it but here I didn't assigned to any table row.这里的 Item.Id 是我想要得到的,但在这里我没有分配给任何表格行。 (I thought it doesn't need to).
(我认为它不需要)。
So far I have created the JavaScript also, But I'm stuck within getting id when click event.到目前为止,我也创建了 JavaScript,但是当点击事件时我被困在获取 id 中。
Can you help me here.你能帮我吗? This is how I done so far
这就是我到目前为止所做的
Table桌子
<div class="table-full-width table-responsive">
<table class="table" id="tblParts">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().PartNo)
</th>
<th>
@Html.DisplayNameFor(model => model.First().PartDescription)
</th>
<th>
@Html.DisplayNameFor(model => model.First().PartModel)
</th>
<th>
@Html.DisplayNameFor(model => model.First().AvaQty)
</th>
<th>
@Html.DisplayNameFor(model => model.First().ReOrderQty)
</th>
<th>
@Html.DisplayNameFor(model => model.First().PartCatogary)
</th>
<th>
@Html.DisplayNameFor(model => model.First().LastUpdated)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.PartNo)
</td>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.PartDescription)
</td>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.PartModel)
</td>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.AvaQty)
</td>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.ReOrderQty)
</td>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.PartCatogary)
</td>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.LastUpdated)
</td>
</tr>
}
</table>
This is the script.这是脚本。 It triggers when clicks, but I want to get the item.id when user click the certain row.
它在单击时触发,但我想在用户单击特定行时获取 item.id。
<script type="text/javascript">
$("#tblParts tr").click(function (event) {
alert("Row Clicked");
});
</script>
To get the id From target you have to do this on the code of click and if it has an id it will grab it要从目标获取 id,您必须在点击代码上执行此操作,如果它有 id,它将抓取它
function(event){ alert(event.target.id) }
One of the solutions is:解决方案之一是:
<script type="text/javascript">
window.onload = function () {
addRowClickEventHandlers();
}
function addRowClickEventHandlers() {
var table = document.getElementById("tblParts");
var rows = table.getElementsByTagName("tr");
for (i = 0; i < rows.length; i++) {
var currentRow = table.rows[i];
var createClickHandler = function (row) {
return function () {
var cell = row.getElementsByTagName("td")[0];
alert("PartNo: " + cell.innerText);
};
};
currentRow.onclick = createClickHandler(currentRow);
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.