gpt4 book ai didi

javascript - 无法在列内添加动态表

转载 作者:行者123 更新时间:2023-11-28 16:49:22 25 4
gpt4 key购买 nike

我需要创建一个具有以下结构的动态表:

# | area         | sub area    | category        | delete 
inp. text inp. text table with rows button

所以我创建了以下标记:

 <div id="areas-container">
<div class="row">
<input type="button" class="btn btn-primary" value="add area" id="add-area">
<table class="table table-bordered table-hover" id="areas-table">
<thead>
<tr>
<th class="text-center">
#
</th>
<th class="text-center">
area
</th>
<th class="text-center">
sub area
</th>
<th class="text-center">
category
</th>
<th class="text-center">
delete
</th>
</tr>
</thead>
<tbody>
<tr id='area-0' data-id="0" class="d-none">
<td data-name="button">
<button type="button" class="btn btn-success row-addsub">sub area</button>
</td>
<td data-name="area" data-id="0">
<input type="text" name='area0' data-type="area" placeholder="name" class="form-control" />
</td>
<td data-name="sub-area" data-id="0">
<input type="text" name='sub_area0' data-type="sub-area" placeholder="name" class="form-control" />
</td>
<td data-name="category" data-id="0">
<table class="table" name="categories-table">
<tbody>
<tr>
<td>test/
<td>
</tr>
</tbody>
</table>
</td>
<td data-name="del text-center">
<button type="button" class="btn btn-danger row-remove">
<i class="material-icons">delete</i>
</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>

现在我编写了以下代码来将动态行添加到表中:

$(document).ready(function() {

$('#add-area').click(function() {
let areaId = 0;
let subAreaId = 0;
let categoryId = 0;

$.each($("#areas-table tr"), function() {

// Take reference from the row
if (parseInt($(this).data("id")) > areaId) {
areaId = parseInt($(this).data("id"));
}

// Get the reference from the column
let currSubAreaId = parseInt($(this).find('td').eq(2).data('id'));
if (currSubAreaId > subAreaId) {
subAreaId = currSubAreaId;
}

let currCatAreaId = parseInt($(this).find('td').eq(3).data('id'));
if (currCatAreaId > categoryId) {
subAreaId = categoryId;
}
});
areaId++;
subAreaId++;
categoryId++;

let tr = $("<tr></tr>", {
id: "area-" + areaId,
"data-id": areaId
});

// Clone the column
$.each($("#areas-table tbody tr:nth(0) td"), function() {
let curTd = $(this);
let children = curTd.children();

// If a name was provided, a column will be added
if ($(this).data("name") != undefined) {
let colName = $(curTd).data("name");
colId = areaId; // default reference area

switch (colName) {
case 'sub-area':
colId = subAreaId;
break;
case 'category':
colId = categoryId;
break;
}

let td = $("<td></td>", {
"data-name": colName,
"data-id": colId
});

let c = $(curTd).find($(children[0]).prop('tagName')).clone().val("");
c.attr("name", $(curTd).data("name") + areaId);
c.appendTo($(td));
td.appendTo($(tr));
} else {
let td = $("<td></td>", {
'text': $('#areas-table tr').length
}).appendTo($(tr));
}
});

// add new row
$(tr).appendTo($('#areas-table'));
})

});

问题是包含 categories-table 的列向表中添加了一个额外的列,其 id 为 3,我怀疑问题与 有关>.clone 方法,我用它来克隆行。

$(document).ready(function() {

$('#add-area').click(function() {
let areaId = 0;
let subAreaId = 0;
let categoryId = 0;

$.each($("#areas-table tr"), function() {

// Take reference from the row
if (parseInt($(this).data("id")) > areaId) {
areaId = parseInt($(this).data("id"));
}

// Get the reference from the column
let currSubAreaId = parseInt($(this).find('td').eq(2).data('id'));
if (currSubAreaId > subAreaId) {
subAreaId = currSubAreaId;
}

let currCatAreaId = parseInt($(this).find('td').eq(3).data('id'));
if (currCatAreaId > categoryId) {
subAreaId = categoryId;
}
});
areaId++;
subAreaId++;
categoryId++;

let tr = $("<tr></tr>", {
id: "area-" + areaId,
"data-id": areaId
});

// Clone the column
$.each($("#areas-table tbody tr:nth(0) td"), function() {
let curTd = $(this);
let children = curTd.children();

// If a name was provided, a column will be added
if ($(this).data("name") != undefined) {
let colName = $(curTd).data("name");
colId = areaId; // default reference area

switch (colName) {
case 'sub-area':
colId = subAreaId;
break;
case 'category':
colId = categoryId;
break;
}

let td = $("<td></td>", {
"data-name": colName,
"data-id": colId
});

let c = $(curTd).find($(children[0]).prop('tagName')).clone().val("");
c.attr("name", $(curTd).data("name") + areaId);
c.appendTo($(td));
td.appendTo($(tr));
} else {
let td = $("<td></td>", {
'text': $('#areas-table tr').length
}).appendTo($(tr));
}
});

// add new row
$(tr).appendTo($('#areas-table'));
})

});
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="areas-container">
<div class="row">
<input type="button" class="btn btn-primary" value="add area" id="add-area">
<table class="table table-bordered table-hover" id="areas-table">
<thead>
<tr>
<th class="text-center">
#
</th>
<th class="text-center">
area
</th>
<th class="text-center">
sub area
</th>
<th class="text-center">
category
</th>
<th class="text-center">
delete
</th>
</tr>
</thead>
<tbody>
<tr id='area-0' data-id="0" class="d-none">
<td data-name="button">
<button type="button" class="btn btn-success row-addsub">sub area</button>
</td>
<td data-name="area" data-id="0">
<input type="text" name='area0' data-type="area" placeholder="name" class="form-control" />
</td>
<td data-name="sub-area" data-id="0">
<input type="text" name='sub_area0' data-type="sub-area" placeholder="name" class="form-control" />
</td>
<td data-name="category" data-id="0">
<table class="table" name="categories-table">
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</td>
<td data-name="del text-center">
<button type="button" class="btn btn-danger row-remove">
<i class="material-icons">delete</i>
</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>

最佳答案

将选择器 $.each($("#areas-table tbody tr:nth(0) td"), ... 更改为 $.each($("# Areas-table tbody tr:nth(0)>td"), ... 这样您就只能选择父 tr 元素的直接后代,而不是嵌套的表格单元格。

编辑

I get the same problem, did you actually tried your solution?

...这就是它的工作原理吗?没有额外的列?!

$(document).ready(function() {

$('#add-area').click(function() {
let areaId = 0;
let subAreaId = 0;
let categoryId = 0;

$.each($("#areas-table tr"), function() {

// Take reference from the row
if (parseInt($(this).data("id")) > areaId) {
areaId = parseInt($(this).data("id"));
}

// Get the reference from the column
let currSubAreaId = parseInt($(this).find('td').eq(2).data('id'));
if (currSubAreaId > subAreaId) {
subAreaId = currSubAreaId;
}

let currCatAreaId = parseInt($(this).find('td').eq(3).data('id'));
if (currCatAreaId > categoryId) {
subAreaId = categoryId;
}
});
areaId++;
subAreaId++;
categoryId++;

let tr = $("<tr></tr>", {
id: "area-" + areaId,
"data-id": areaId
});

// Clone the column
$.each($("#areas-table tbody tr:nth(0)>td"), function() {
let curTd = $(this);
let children = curTd.children();

// If a name was provided, a column will be added
if ($(this).data("name") != undefined) {
let colName = $(curTd).data("name");
colId = areaId; // default reference area

switch (colName) {
case 'sub-area':
colId = subAreaId;
break;
case 'category':
colId = categoryId;
break;
}

let td = $("<td></td>", {
"data-name": colName,
"data-id": colId
});

let c = $(curTd).find($(children[0]).prop('tagName')).clone().val("");
c.attr("name", $(curTd).data("name") + areaId);
c.appendTo($(td));
td.appendTo($(tr));
} else {
let td = $("<td></td>", {
'text': $('#areas-table tr').length
}).appendTo($(tr));
}
});

// add new row
$(tr).appendTo($('#areas-table'));
})

});
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="areas-container">
<div class="row">
<input type="button" class="btn btn-primary" value="add area" id="add-area">
<table class="table table-bordered table-hover" id="areas-table">
<thead>
<tr>
<th class="text-center">
#
</th>
<th class="text-center">
area
</th>
<th class="text-center">
sub area
</th>
<th class="text-center">
category
</th>
<th class="text-center">
delete
</th>
</tr>
</thead>
<tbody>
<tr id='area-0' data-id="0" class="d-none">
<td data-name="button">
<button type="button" class="btn btn-success row-addsub">sub area</button>
</td>
<td data-name="area" data-id="0">
<input type="text" name='area0' data-type="area" placeholder="name" class="form-control" />
</td>
<td data-name="sub-area" data-id="0">
<input type="text" name='sub_area0' data-type="sub-area" placeholder="name" class="form-control" />
</td>
<td data-name="category" data-id="0">
<table class="table" name="categories-table">
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</td>
<td data-name="del text-center">
<button type="button" class="btn btn-danger row-remove">
<i class="material-icons">delete</i>
</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>

关于javascript - 无法在列内添加动态表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60116069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com