单击“唯一ID”时,我试图填充或显示子表行。但是,我得到的只是“未定义”。
我知道我需要一个循环或需要通过子表的索引的内容(例如subTableData [i] .Description),但是然后单击功能中断了。
有人可以指导我吗?
HTML:
<table id="parentTable">
<thead>
<tr>
<th>Unique ID</th>
<th>Name</th>
<th>Email</th>
<th>Price</th>
<th>Hours</th>
</tr>
</thead>
<tbody id="parentTableBody">
</tbody>
</table>
<div id="subTableContainer" style="display: none;">
<table>
<thead>
<tr>
<th>Description</th>
<th>Arrival</th>
</tr>
</thead>
<tbody id = subTableBody>
<tr>
</tr>
</tbody>
</table>
</div>
jQuery的:
$(document).ready(function(){
var arr1 = generateItem();
if(arr1){
var arr2 = [].concat(arr1);
var tr;
$.each(arr2, function(i,e) {
tr = $('<tr>');
tr.append("<td>" + "<a href='#'>" + (e.UniqueId || "") + "</a>" + "</td>");
tr.append("<td>" + (e.Name || "") + "</td>");
tr.append("<td>" + (e.Email || "") + "</td>");
tr.append("<td>" + (e.Price || "") + "</td>");
tr.append("<td>" + (e.Hours || "") + "</td>");
$('#parentTableBody').append(tr);
/* tr = $('<tr>');
tr.append("<td>" + (e.Description || "") + "</td>");
tr.append("<td>" + (e.Arrival || "") + "</td>");
$('#subTableBody').append(tr); */
});
}
});
function generateItem(){
var item = [
{UniqueId: "0",Name: "Alex", Email: "[email protected]", Price: "$20.00", Hours: "1", Description: "N/A", Arrival: "Noon"},
{UniqueId: "1", Name: "Jay", Email: "[email protected]", Price: "$12.00", Hours: "0.2", Description: "Small", Arrival: "Morning"},
{UniqueId: "2" ,Name: "Dylan", Email: "[email protected]", Price: "$32.00", Hours: "2.2", Description: "Heavy", Arrival: "Night"}];
return item;
}
$(function(){
$('#parentTable tbody a').on("click", function(event) {
var idName = $(event.target).text(),
$baseRow = $(event.target).closest("tr");
if ($baseRow.next("tr").find("table").length == 0) {
var subTableData = generateItem(),
subTableHTML = $("#subTableContainer table").clone(true),
descriptionHTML = "<td>" + subTableData.Description + "</td>",
arrivalHTML = "<td>" + subTableData.Arrival + "</td>";
$baseRow.after("<tr><td colspan='4'></td></tr>");
$baseRow.next("tr").find("td").append(subTableHTML);
$baseRow.next("tr").find("#subTableBody tr").append(descriptionHTML).append(arrivalHTML);
} else $baseRow.next("tr").remove();
});
});
我至少在单击功能起作用的地方将其还原了。谢谢
jsFiddle
最佳答案
您走在正确的轨道上;
您需要找到单击项的索引。
var targetIdx = subTableData.findIndex(x => x.UniqueId == idName);
我还对
subTable
结构进行了更改以修复它,并确保页面上没有多个id
,并对添加技术进行了一些小的更改。$(document).ready(function() {
var arr1 = generateItem();
if (arr1) {
var arr2 = [].concat(arr1);
var tr;
$.each(arr2, function(i, e) {
tr = $('<tr>');
tr.append("<td>" + "<a href='#'>" + (e.UniqueId || "") + "</a>" + "</td>");
tr.append("<td>" + (e.Name || "") + "</td>");
tr.append("<td>" + (e.Email || "") + "</td>");
tr.append("<td>" + (e.Price || "") + "</td>");
tr.append("<td>" + (e.Hours || "") + "</td>");
$('#parentTableBody').append(tr);
/* tr = $('<tr>');
tr.append("<td>" + (e.Description || "") + "</td>");
tr.append("<td>" + (e.Arrival || "") + "</td>");
$('#subTableBody').append(tr); */
});
}
});
function generateItem() {
var item = [{
UniqueId: "0",
Name: "Alex",
Email: "[email protected]",
Price: "$20.00",
Hours: "1",
Description: "N/A",
Arrival: "Noon"
},
{
UniqueId: "1",
Name: "Jay",
Email: "[email protected]",
Price: "$12.00",
Hours: "0.2",
Description: "Small",
Arrival: "Morning"
},
{
UniqueId: "2",
Name: "Dylan",
Email: "[email protected]",
Price: "$32.00",
Hours: "2.2",
Description: "Heavy",
Arrival: "Night"
}
];
return item;
}
$(function() {
$('#parentTable tbody a').on("click", function(event) {
var idName = $(event.target).text(),
$baseRow = $(event.target).closest("tr");
if ($baseRow.next("tr").find("table").length == 0) {
var subTableData = generateItem();
var targetIdx = subTableData.findIndex(x => x.UniqueId == idName); //get the index
var subTableHTML = $("#subTableContainer table").clone(true);
var descriptionHTML = $("<td>").text(subTableData[targetIdx].Description); //modified to shorten
var arrivalHTML = $("<td>").text(subTableData[targetIdx].Arrival); //modified to shorten
$baseRow.after("<tr><td colspan='4'></td></tr>");
$baseRow.next("tr").find("td")
.append(subTableHTML).find("tbody>tr")
.append(descriptionHTML).append(arrivalHTML);
} else $baseRow.next("tr").remove();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="parentTable">
<thead>
<tr>
<th>Unique ID</th>
<th>Name</th>
<th>Email</th>
<th>Price</th>
<th>Hours</th>
</tr>
</thead>
<tbody id="parentTableBody">
</tbody>
</table>
<div id="subTableContainer" style="display: none;">
<table>
<thead>
<tr>
<th>Description</th>
<th>Arrival</th>
</tr>
</thead>
<tbody>
<tr>
</tr>
</tbody>
</table>
</div>
编辑[0]:
根据您留下的评论,您希望您的
object array
是可编辑的。此编辑对generateItem
进行了少量修改,并允许对其进行修改,并保留这些更改。$(document).ready(function() {
var arr1 = generateItem();
if (arr1) {
var arr2 = [].concat(arr1);
var tr;
$.each(arr2, function(i, e) {
tr = $('<tr>');
tr.append("<td>" + "<a href='#'>" + (e.UniqueId || "") + "</a>" + "</td>");
tr.append("<td>" + (e.Name || "") + "</td>");
tr.append("<td>" + (e.Email || "") + "</td>");
tr.append("<td>" + (e.Price || "") + "</td>");
tr.append("<td>" + (e.Hours || "") + "</td>");
$('#parentTableBody').append(tr);
/* tr = $('<tr>');
tr.append("<td>" + (e.Description || "") + "</td>");
tr.append("<td>" + (e.Arrival || "") + "</td>");
$('#subTableBody').append(tr); */
});
}
});
var mydata = null;
function generateItem() {
if (mydata == null) {
mydata = [{
UniqueId: "0",
Name: "Alex",
Email: "[email protected]",
Price: "$20.00",
Hours: "1",
Description: "N/A",
Arrival: "Noon"
},
{
UniqueId: "1",
Name: "Jay",
Email: "[email protected]",
Price: "$12.00",
Hours: "0.2",
Description: "Small",
Arrival: "Morning"
},
{
UniqueId: "2",
Name: "Dylan",
Email: "[email protected]",
Price: "$32.00",
Hours: "2.2",
Description: "Heavy",
Arrival: "Night"
}
];
}
return mydata;
}
$(function() {
$('#parentTable tbody a').on("click", function(event) {
var idName = $(event.target).text(),
$baseRow = $(event.target).closest("tr");
if ($baseRow.next("tr").find("table").length == 0) {
var subTableData = generateItem();
var targetIdx = subTableData.findIndex(x => x.UniqueId == idName); //get the index
var subTableHTML = $("#subTableContainer table").clone(true);
var descriptionHTML = $("<td>").text(subTableData[targetIdx].Description); //modified to shorten
var arrivalHTML = $("<td>").text(subTableData[targetIdx].Arrival); //modified to shorten
$baseRow.after("<tr><td colspan='4'></td></tr>");
$baseRow.next("tr").find("td")
.append(subTableHTML).find("tbody>tr")
.append(descriptionHTML).append(arrivalHTML);
} else $baseRow.next("tr").remove();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="parentTable">
<thead>
<tr>
<th>Unique ID</th>
<th>Name</th>
<th>Email</th>
<th>Price</th>
<th>Hours</th>
</tr>
</thead>
<tbody id="parentTableBody">
</tbody>
</table>
<div id="subTableContainer" style="display: none;">
<table>
<thead>
<tr>
<th>Description</th>
<th>Arrival</th>
</tr>
</thead>
<tbody>
<tr>
</tr>
</tbody>
</table>
</div>
关于javascript - jQuery-在父行下方填充子表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50414187/