如何在 HTML 表中获取自动序列号列
How to get Automatic Serial number column in the HTML Table
我需要在表格中的一列中自动获取序列号。
这是我的示例代码:
<%@ include file="/WEB-INF/pages/common/taglibs.jspf"%>
<link rel="stylesheet" href="<c:url value='/styles/tablesort.css'/>" />
<script type="text/javascript"
src="<c:url value='/scripts/jquery.tablesort.js'/>"></script>
<script type="text/javascript">
$(function() {
$("#tabs").tabs();
});
</script>
<style type="text/css">
table tr td{
text-align:center;
}
</style>
<body>
<div id="tabs" style="width: 880px;">
<c:if test="${ model != null}">
<table id="commentsTable" class="tablesorter">
<thead>
<tr>
<th>S.NO<th/>
<th><spring:message code="title" /></th>
<th><spring:message code="CommentsValue" /></th>
<th><spring:message code="By" /></th>
<th><spring:message code="date" /></th>
<th><spring:message code="comments" /></th>
<th><spring:message code="By" /></th>
<th><spring:message code="LateUser" /></th>
<th><spring:message code="LateTimestamp" /></th>
</tr>
</thead>
<tbody>
<c:forEach var="row" items="${model}">
<tr>
<td>Need to get automatic serial numbers value here<td>
<td>HTML</td>
<td style="word-break:break-all;">Mount</td>
<td>1234</td>
<td>2345</td>
<td style="word-break:break-all;">2345</td>
<td>token</td>
<td>right</td>
<td>10982</td>
</tr>
</c:forEach>
</tbody>
</table>
</c:if>
</div>
</body>
纯CSS解决方案
看到工作小提琴
HTML:(一个简单的表格,带有一个空白td
,将容纳计数器)
<table border="1">
<thead>
<tr>
<th>Automatic Serial number</th>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<!--leave it blank-->
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td></td>
<!--leave it blank-->
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td></td>
<!--leave it blank-->
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td></td>
<!--leave it blank-->
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td></td>
<!--leave it blank-->
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td></td>
<!--leave it blank-->
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td></td>
<!--leave it blank-->
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td></td>
<!--leave it blank-->
<td>Column 1</td>
<td>Column 2</td>
</tr>
</tbody>
</table>
.CSS:
body
{
counter-reset: Serial; /* Set the Serial counter to 0 */
}
table
{
border-collapse: separate;
}
tr td:first-child:before
{
counter-increment: Serial; /* Increment the Serial counter */
content: "Serial is: " counter(Serial); /* Display the counter */
}
如果要定位特定表,只需为其提供一个类,并专门针对这些tr
。
.html
<table class="auto-index">
.
.
.
.css
.auto-index td:first-child:before
{
counter-increment: Serial; /* Increment the Serial counter */
content: "Serial is: " counter(Serial); /* Display the counter */
}
将第一列留空,并调用 javascript 方法来添加序列号。示例如下所示
var addSerialNumber = function () {
$('table tr').each(function(index) {
$(this).find('td:nth-child(1)').html(index+1);
});
};
addSerialNumber();
http://jsfiddle.net/ChaitanyaMunipalle/DgUG2/
<%! int i = 1; %>
<tbody>
<c:forEach var="row" items="${model}">
<tr>
<td><%= i; %> <%! i++; %> <td>
<td>HTML</td>
<td style="word-break:break-all;">Mount</td>
<td>1234</td>
<td>2345</td>
<td style="word-break:break-all;">2345</td>
<td>token</td>
<td>right</td>
<td>10982</td>
</tr>
</c:forEach>
</tbody>
试试这个 JSP 代码。
在 Sql 中试试这个
SELECT @a:=@a+1 serial_number,marks,(need fields in you db) FROM
student_marks(your db name),(SELECT @a:= 0) AS a;
如果你的htmp页面中有标题....使用以下代码
var addSerialNumber = function () {
var i = 0
$('table tr').each(function(index) {
$(this).find('td:nth-child(1)').html(index-1+1);
});
};
addSerialNumber();
使用以下代码:- 阅读评论以更好地理解
<% int i = 1; %> // --> It will declare value of i as 1
<c:forEach>
<tr>
<td><%= i %> <% i++; %></td> // --> It will display and increment the value
<td>${product.productDescription}</td>
<td>${products.warehouseQuantity}</td>
<td>${product.productPrice }</td>
<td>Rs ${product.productDiscount }</td>
<td>Rs ${product.productPricePerQuantity }</td>
</tr>
</c:forEach>
我知道
已经给出了正确的答案,但我有同样的问题,但我的表格行将动态附加,所以答案对我没有帮助。
如果有人遇到我需要更新表并需要索引的问题类型,例如电子商务站点中的添加到购物车,那么我们可以使用它。我在表上使用了名为childElementCount的DOM属性,该属性返回元素的子元素数。
function func(){
let index = document.getElementsByTagName("table")[0].childElementCount
$('table').append(`
<tr class="items--row">
<td class="ind">${index}</td>
<td>Chocolate</td>
<td>$23</td>
<td>10</td>
<td>$230</td>
</tr>
`);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr class="head--row">
<th>S.no</th>
<th>Name</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Total Price</th>
</tr>
</table>
<button onclick="func()">Sample</button>
相关文章:
- Regex表示10位数字,其中没有't允许所有零、所有单个no和序列号
- 当行增加时自动递增序列号,并在 jQuery 中删除行时自动重新调整编号顺序
- 查找子项的序列号
- 数据表自动生成的序列号无法正常工作.对于每一页
- 我想在客户端单击中生成作业序列号.如果有任何建议,请提供帮助
- 如何使用javascript或jquery获取自动序列号
- 从序列号生成的 HTML 颜色,用于区分各个设备
- 可以通过JavaScript获取CPU或主板序列号吗?
- 从Flash AS3 Android应用程序调用Java脚本函数以检索设备ID和序列号
- 如何向字符串添加连字符(将其格式化为 16 位序列号)
- 如何使用Javascript自动输入html中行的序列号
- 如何在 HTML 表中获取自动序列号列
- 通过php或javascript检索iPad / iPhone序列号
- 如何使用jquery为表中的序列号列提供连续编号
- 如何制作序列号表格?用“-"在中间分开
- 如何在Javascript表中添加动态序列号
- 每页都有相同的序列号
- 是否可以在jQuery中通过其索引号获取父元素?还是有其他方法
- 获取客户端系统硬盘序列号
- 如何通过web获取客户端硬盘序列号以生成许可证密钥