【发布时间】:2015-05-11 11:13:43
【问题描述】:
问题背景:
我在视图中有一个从数据库动态填充的表。视图中的过滤器允许用户选择需要在表格上显示的数据。数据的重新添加是通过删除当前表行,然后在其位置添加新数据来实现的。
问题:
在每个表格行上,我都可以单击在模态弹出窗口中呈现数据的行,每次用户更改过滤器列表时都会重新查询数据库,这需要删除表格数据然后重新填充. 新添加的行被选中时不会触发行点击事件。
代码:
表格在视图中填充如下:
@foreach (var component in @Model.ComponentDetailsList)
{
<script>
var codebase = '@component.Codebase';
var component = '@component.ComponentName';
var tfsLocation = '@component.ComponentDevTfsLocation';
var tfsQaLocation = '@component.ComponentQaTfsLocation';
AddRows(codebase, component, tfsLocation, tfsQaLocation);
</script>
}
jQuery方法AddRows:
function AddRows(codebase, component, tfsLocation, tfsQaLocation) {
$html = '<tr><td id="codebaseCell"><b>' + codebase + '</b></td><td id="componentCell">' + component + '</td><td class="hidden-xs hidden-sm" id="tfsDevCell">' + tfsLocation + '</td><td class="hidden-xs hidden-sm" id="tfsQaCell">' + tfsQaLocation + '</td><td><button type="button" class="btn btn-danger pull-left" id="deleteQueuedComponent">Delete</button></td></tr>';
$("#componentTable tbody").append($html);
};
The Row On-Click 事件:
$(document).ready(function () {
$('#componentTable tr').click(function () {
var tfsCodebase = $(this).find('#codebaseCell').text();
alert(tfsCodebase);
var tfsComponent = $(this).find('#componentCell').text();
$.ajax({
url: "/Release/GetComponentFiles",
data: { codeBase: tfsCodebase, component: tfsComponent },
success: function (component) {
var count = 0;
var componentName = component.componentName;
var componentCodeBase = component.componentCodebase;
for (var item in component.fileList) {
var devItem = component.componentDevTfsLocation + '/' + component.fileList[item];
var qaItem = component.componentQaTfsLocation + '/' + component.fileList[item];
count++;
AddRowsToModal(componentName, componentCodeBase, component.fileList[item], devItem, qaItem, count);
}
document.getElementById("modalInfo").innerText = 'Queued Files For: ' + component.componentName;
$("#componentModal").modal('show');
}
});
});
});
单击过滤器项时调用的函数。 ClearTableAndAddRows 函数删除表内容并重新添加它,但如上所述行单击事件处理程序将不再工作:
$(document).ready(function () {
$("#codebaseNameList li").click(function () {
var codebase = $(this).text();
$.ajax({
url: "/Release/QueryComponentsByCodebase",
data: { codebase: codebase },
success: function (component) {
for (var item in component.codebaseList)
{
ClearTableAndAddRows(component.codebaseList[item].Codebase, component.codebaseList[item].ComponentName, component.codebaseList[item].ComponentDevTfsLocation, component.codebaseList[item].ComponentQaTfsLocation);
AddRowsToComponentList(component.codebaseList[item].ComponentName);
}
}
});
});
});
JQuery 方法ClearTableAndAddRows 用于删除,然后将新行重新添加到现有表体中:
function ClearTableAndAddRows(codebase, component, tfsLocation, tfsQaLocation) {
$("#queuedFilesTable").empty();
$htmlFilteredRow = '<tr><td id="codebaseCell"><b>' + codebase + '</b></td><td id="componentCell">' + component + '</td><td class="hidden-xs hidden-sm" id="tfsDevCell">' + tfsLocation + '</td><td class="hidden-xs hidden-sm" id="tfsQaCell">' + tfsQaLocation + '</td><td><button type="button" class="btn btn-danger pull-left" id="deleteQueuedComponent">Delete</button></td></tr>';
$("#componentTable tbody").append($htmlFilteredRow);
};
【问题讨论】:
-
$('#componentTable').on('click','tr', function () {
标签: javascript jquery html dom