在函数中检索 Bootstrap table 中的所有计数器值

Retrieve in function all counter values from a Bootstrap table

这里我有一个 table 的示例,它显示每页 10 行的 25 条记录的列表。 当我应用过滤器“印度”时,如何在函数中获取更新的计数器指示值?

我有显示值,但没有条目数和总条目数。

这是一张用于直观解释的图片。

这是 HTML 代码和函数,只有我才能获得第一个计数器。

 <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta name="description" content="Bootstrap.">
        <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <link rel="stylesheet" href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css">
        </style>
        <script type="text/javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
        <script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    </head>
    
    <body style="margin:20px auto">
        <div class="container">
            <div class="row header" style="text-align:center;color:green">
                <h3>Bootstrap</h3>
            </div>
            <table id="myTable" class="table table-striped">
                <thead>
                    <tr>
                        <th>ENO</th>
                        <th>EMPName</th>
                        <th>Country</th>
                        <th>Salary</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>001</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
                    <tr>
                        <td>002</td>
                        <td>Charles</td>
                        <td>United Kingdom</td>
                        <td>28000</td>
                    </tr>
                    <tr>
                        <td>003</td>
                        <td>Sravani</td>
                        <td>Australia</td>
                        <td>7000</td>
                    </tr>
                    <tr>
                        <td>004</td>
                        <td>Amar</td>
                        <td>India</td>
                        <td>18000</td>
                    </tr>
                    <tr>
                        <td>005</td>
                        <td>Lakshmi</td>
                        <td>India</td>
                        <td>12000</td>
                    </tr>
                    <tr>
                        <td>006</td>
                        <td>James</td>
                        <td>Canada</td>
                        <td>50000</td>
                    </tr>
    
                    <tr>
                        <td>007</td>
                        <td>Ronald</td>
                        <td>US</td>
                        <td>75000</td>
                    </tr>
                    <tr>
                        <td>008</td>
                        <td>Mike</td>
                        <td>Belgium</td>
                        <td>100000</td>
                    </tr>
                    <tr>
                        <td>009</td>
                        <td>Andrew</td>
                        <td>Argentina</td>
                        <td>45000</td>
                    </tr>
    
                    <tr>
                        <td>010</td>
                        <td>Stephen</td>
                        <td>Austria</td>
                        <td>30000</td>
                    </tr>
                    <tr>
                        <td>011</td>
                        <td>Sara</td>
                        <td>China</td>
                        <td>750000</td>
                    </tr>
                    <tr>
                        <td>012</td>
                        <td>JonRoot</td>
                        <td>Argentina</td>
                        <td>65000</td>
                    </tr>
                    <tr>
                        <td>013</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
                    <tr>
                        <td>014</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
                    <tr>
                        <td>015</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
                    <tr>
                        <td>016</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
                    <tr>
                        <td>017</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
                    <tr>
                        <td>018</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
                    <tr>
                        <td>019</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
                    <tr>
                        <td>020</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
                    <tr>
                        <td>021</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
                    <tr>
                        <td>022</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
                    <tr>
                        <td>023</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
                    <tr>
                        <td>024</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
                    <tr>
                        <td>025</td>
                        <td>Anusha</td>
                        <td>India</td>
                        <td>10000</td>
                    </tr>
    
                </tbody>
            </table>
        </div>
    </body>
    <script>
        $(document).ready(function() {
            $('#myTable').dataTable();
        });
    </script>
    
    <input type="button" id="btnGetallCount" value="Count Rows" />
    <script type="text/javascript">
        $(function() {
            $("#btnGetallCount").click(function() {
                var showing = $('#myTable tbody tr:not(.footable-filtered)').length;
                var entries = "?";
                var totalentries = "?";
                var message = "Showing: " + showing;
                message += "\nEntries: " + entries;
                message += "\nTotal Entries: " + totalentries;
                alert(message);
            });
        });
    </script>
    </html>

因为您正在使用 Datatables.net,所以您可以使用 Javascript 中的数据表 API 来获取此信息:

var myTable = $('#myTable').dataTable();

var fromRecord = ((myTable.page() * myTable.page.len()) + 1);
var toRecord = ((myTable.page() + 1) * myTable.page.len());

var entries = "?";
var totalentries = myTable.rows().count();
var message = "Showing: " + fromRecord + ' to ' + toRecord;
message += "\nEntries: " + entries;
message += "\nTotal Entries: " + totalentries;
alert(message);

有关详细信息,请查看 Datatables API reference

您可以使用 page.info() API of DataTable

$(document).ready(function() {
    var table = $('#myTable').DataTable({})    
    
    $("#btnGetallCount").click(function() {
    console.log(table.page.info());
    
        var page_info = table.page.info();
        var showing = page_info['end'];
        var entries = page_info['recordsDisplay'];
        var totalentries = page_info['recordsTotal'];
        var message = "Showing: " + showing;
        message += "\nEntries: " + entries;
        message += "\nTotal Entries: " + totalentries;
        alert(message);
   });
});
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Final Output</title>
    <meta name="description" content="Bootstrap.">
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <link rel="stylesheet" href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css">
    
    <script type="text/javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

</head>

<body style="margin:20px auto">
    <div class="container">
        <div class="row header" style="text-align:center;color:green">
            <h3>Bootstrap</h3>
        </div>
        <table id="myTable" class="table table-striped">
            <thead>
                <tr>
                    <th>ENO</th>
                    <th>EMPName</th>
                    <th>Country</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>001</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>002</td>
                    <td>Charles</td>
                    <td>United Kingdom</td>
                    <td>28000</td>
                </tr>
                <tr>
                    <td>003</td>
                    <td>Sravani</td>
                    <td>Australia</td>
                    <td>7000</td>
                </tr>
                <tr>
                    <td>004</td>
                    <td>Amar</td>
                    <td>India</td>
                    <td>18000</td>
                </tr>
                <tr>
                    <td>005</td>
                    <td>Lakshmi</td>
                    <td>India</td>
                    <td>12000</td>
                </tr>
                <tr>
                    <td>006</td>
                    <td>James</td>
                    <td>Canada</td>
                    <td>50000</td>
                </tr>

                <tr>
                    <td>007</td>
                    <td>Ronald</td>
                    <td>US</td>
                    <td>75000</td>
                </tr>
                <tr>
                    <td>008</td>
                    <td>Mike</td>
                    <td>Belgium</td>
                    <td>100000</td>
                </tr>
                <tr>
                    <td>009</td>
                    <td>Andrew</td>
                    <td>Argentina</td>
                    <td>45000</td>
                </tr>

                <tr>
                    <td>010</td>
                    <td>Stephen</td>
                    <td>Austria</td>
                    <td>30000</td>
                </tr>
                <tr>
                    <td>011</td>
                    <td>Sara</td>
                    <td>China</td>
                    <td>750000</td>
                </tr>
                <tr>
                    <td>012</td>
                    <td>JonRoot</td>
                    <td>Argentina</td>
                    <td>65000</td>
                </tr>
                <tr>
                    <td>013</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>014</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>015</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>016</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>017</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>018</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>019</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>020</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>021</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>022</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>023</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>024</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>025</td>
                    <td>Anusha</td>
                    <td>India</td>
                    <td>10000</td>
                </tr>

            </tbody>
        </table>
    </div>
</body>


<input type="button" id="btnGetallCount" value="Count Rows" />
</html>