如何在HTML表中获取自动序列号列

时间:2013-09-25 07:14:37

标签: javascript jquery html css jsp

我需要在表格的一栏中自动获取序列号。

以下是我的示例代码:

<%@ 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>

5 个答案:

答案 0 :(得分:24)

纯CSS解决方案

Working Fiddle

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 */
}

答案 1 :(得分:5)

将第一列留空并调用javascript方法添加序列号。一个例子如下所示

var addSerialNumber = function () {
    $('table tr').each(function(index) {
        $(this).find('td:nth-child(1)').html(index+1);
    });
};

addSerialNumber();

http://jsfiddle.net/ChaitanyaMunipalle/DgUG2/

答案 2 :(得分:3)

    <%! 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;

答案 3 :(得分:0)

如果您的htmp页面中有标题....请使用以下代码

var addSerialNumber = function () {
    var i = 0
    $('table tr').each(function(index) {
        $(this).find('td:nth-child(1)').html(index-1+1);
    });
};

addSerialNumber();

答案 4 :(得分:0)

使用以下代码: - 阅读评论以便更好地理解

<% 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>