有没有一种方法我可以首先在表中显示具有“分支1”的“分支”的记录?有什么想法,请帮忙吗?下面是我的片段。



$(document).ready(function(){
  $("#dtable").DataTable( {
            "pagingType": "full_numbers",
            "dom": 'T<"clear">lfrtip',
            "lengthMenu": [[10, 25, 50, -1],[10, 25, 50, "All"]],
        });
});

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<table id="dtable" class="table table-hover">
  <thead>
    <tr>
      <th>NAME</th>
      <th>BRANCH</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 2</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 2</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 2</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 3</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 3</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 3</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 3</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
  </tbody>
</table>

最佳答案

您需要使用默认的数据表排序,更多信息可以在这里找到:
https://datatables.net/examples/basic_init/table_sorting.html

基本上,您只需要"order": [[ 1, "asc" ]]

演示:


$(document).ready(function(){
  $("#dtable").DataTable( {
            "pagingType": "full_numbers",
            "dom": 'T<"clear">lfrtip',
            "lengthMenu": [[10, 25, 50, -1],[10, 25, 50, "All"]],
      "order": [[ 1, "asc" ]]
        });
});

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<table id="dtable" class="table table-hover">
  <thead>
    <tr>
      <th>NAME</th>
      <th>BRANCH</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 2</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 2</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 2</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 3</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 3</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 3</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 3</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
    <tr>
      <td>NAME 1</td><td>BRANCH 1</td>
    </tr>
  </tbody>
</table>

关于javascript - 按首选条件在表中显示这些记录的第一位或第一位,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35076691/

10-12 05:26