gpt4 book ai didi

javascript - 在 td 标记中放置一个 0(零)而不是多个 0

转载 作者:行者123 更新时间:2023-12-03 05:22:22 24 4
gpt4 key购买 nike

这是我的 html,

<table class="table table-striped">
<thead>
<tr>
<th>Sl</th>
<th>Book</th>
<th th:each="inventory: ${inventories}" th:text="${inventory.name}"></th>
<th>Total</th>
</tr>
</thead>

<tbody>
<tr th:each="book, bookCount : ${books}">
<td th:text="${bookCount.index}+1"></td>
<td th:text="${book.name}"></td>
<td th:each="inventory, inventoryCount : ${inventories}">
<span th:each="inventoryDetails, inventoryDetailsCount : ${inventoryDetailsList}">
<span th:if="${inventory eq inventoryDetails.inventory}">
<span th:text="${inventoryDetails.book eq book}? ${inventoryDetails.bookQuantity} : 0"></span>
</span>

</span>
</td>
<td th:text="${book.inventoryQuantity}"></td>
</tr>
</tbody>
</table>

这是当前的输出, enter image description here

案例 1:库存 0 中的图书 3

当前显示: 0 0 0

预期:0

情况 2:库存 0 中的账簿 0库存 0 中的账簿 1

当前显示:0 0 20 或 0 20 0

预计:20

您可以在库存2下看到理想的场景

您能否建议我一个关于如何使用 jquery 获得预期结果的解决方案?

这是渲染的 Html,

<table class="table table-striped">
<thead>
<tr>
<th>Sl</th>
<th>Book</th>
<th>Inventory 0</th>
<th>Inventory 1</th>
<th>Inventory 2</th>
<th>Total</th>
</tr>
</thead>

<tbody>
<tr>
<td>1</td>
<td>Book 5</td>
<td>
<span>
<span>
<span>50</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>


</span>
<span>


</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>0</span>
</span>

</span>
</td>
<td>0</td>
</tr>
<tr>
<td>2</td>
<td>Book 1</td>
<td>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>20</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>


</span>
<span>


</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>0</span>
</span>

</span>
</td>
<td>20</td>
</tr>
<tr>
<td>3</td>
<td>Book 0</td>
<td>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>20</span>
</span>

</span>
<span>


</span>
<span>


</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>60</span>
</span>

</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>40</span>
</span>

</span>
</td>
<td>120</td>
</tr>
<tr>
<td>4</td>
<td>Book 3</td>
<td>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>


</span>
<span>


</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>10</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>0</span>
</span>

</span>
</td>
<td>10</td>
</tr>
</tbody>
</table>

最佳答案

嗯,这是一个解决方案。不太优雅(必须使用很少的每个循环),但它正在工作:

$('.table-striped td').each(function() {
//spantext=$(this).find('span').text().replace(/[^0-9]/g,'');

$(this).find('span').each(function( index ) {

spantext=$(this).text().replace(/[^0-9]/g,'');

if(spantext=='0') {

$(this).remove();
}


});

});

$('.table-striped td').each(function() {
if(!($.trim($(this).text()).length)) {
$(this).html('<span>0</span>')
}
});
table,th, td {
border:1px solid black;
border-collapse:collapse;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-striped">
<thead>
<tr>
<th>Sl</th>
<th>Book</th>
<th>Inventory 0</th>
<th>Inventory 1</th>
<th>Inventory 2</th>
<th>Total</th>
</tr>
</thead>

<tbody>
<tr>
<td>1</td>
<td>Book 5</td>
<td>
<span>
<span>
<span>50</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>


</span>
<span>


</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>0</span>
</span>

</span>
</td>
<td>0</td>
</tr>
<tr>
<td>2</td>
<td>Book 1</td>
<td>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>20</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>


</span>
<span>


</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>0</span>
</span>

</span>
</td>
<td>20</td>
</tr>
<tr>
<td>3</td>
<td>Book 0</td>
<td>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>20</span>
</span>

</span>
<span>


</span>
<span>


</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>60</span>
</span>

</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>40</span>
</span>

</span>
</td>
<td>120</td>
</tr>
<tr>
<td>4</td>
<td>Book 3</td>
<td>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>


</span>
<span>


</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>10</span>
</span>

</span>
<span>
<span>
<span>0</span>
</span>

</span>
<span>


</span>
</td>
<td>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>


</span>
<span>
<span>
<span>0</span>
</span>

</span>
</td>
<td>10</td>
</tr>
</tbody>
</table>

所以,想法是 - 找到(仅)零的跨度,将其删除,重建 td 的 HTML(第二个循环)。如果我找到更优雅的解决方案,我会发布它......

关于javascript - 在 td 标记中放置一个 0(零)而不是多个 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41320189/

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