Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Automatic Serial number column in the HTML Table

I need to get serial number automatically in one of my column in the table.

Here is my sample code:

<%@ 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>
like image 291
Yellow Flash Avatar asked Sep 25 '13 07:09

Yellow Flash


People also ask

How do I display the serial number in HTML?

jQuery Method to add automating serial number for each roweach(function(){ $('th:first-child, thead td:first-child', this). each(function(){ var tag = $(this). prop('tagName'); $(this). before('<'+tag+'>#</'+tag+'>'); }); $('td:first-child', this).

How do I make a column table in HTML?

HTML Tables are sets of data that are presented in rows and columns. To make an HTML table use the <table> element. You can use <tr> to create rows, <td> to create columns, and <th> to create table headers.


2 Answers

Pure CSS Solution

see that Working Fiddle

HTML: (a simple table with a blank td that will hold the counter)

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

if you want to target specific table, just give it a class, and target those trs specifically.

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 */
}
like image 126
avrahamcool Avatar answered Sep 17 '22 22:09

avrahamcool


Leave the first column as blank and call a javascript method to add serial numbers. An example is shown below

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

addSerialNumber();

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

like image 43
Chaitanya Munipalle Avatar answered Sep 20 '22 22:09

Chaitanya Munipalle