如何在第一列值的右侧显示响应数据表的展开/折叠图标?

时间:2018-03-09 10:51:23

标签: javascript css datatable datatables

我正在使用响应式数据表(https://www.datatables.net/extensions/responsive/examples/display-control/classes.html)。但展开/折叠图标出现在第一列值的左侧。

如何将其更改为显示在第一列值的右侧。

还想更改图标样式以使用Font Awesome的图标

JS:

$(document).ready( function () {
  var table = $('#example').DataTable({
    responsive: {
            details: {
                type: 'column'
            }
        },
        columnDefs: [ {
            className: 'control',
            orderable: false,
            targets:   0
        } ],
        order: [ 1, 'asc' ]
  });
} );

HTML代码:

<!DOCTYPE html>
<html>       
  <body>
    <div class="container">
      <table id="example" class="display responsive" width="100%">
        <thead>
          <tr>
            <th></th>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
          </tr>
        </thead>

        <tfoot>
          <tr>
            <th></th>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
          </tr>
        </tfoot>

        <tbody>
          <tr>
            <td></td>
            <td>Tiger Nixon</td>
            <td>System Architect</td>
            <td>Edinburgh</td>
            <td>61</td>
            <td>2011/04/25</td>
            <td>$3,120</td>
          </tr>
          </tbody>
      </table>
    </div>
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

有一个CSS选择器

const examples = [
    'X6JV2YUW8T',
    'JN1H86LEKA',
    '111JEJE134',
    'A111111111',
    'ABCDEFGHIJ',
    '0123456789',
];

const regex = /^(?=(.*\d){3})(?=(.*[a-zA-Z]))[a-zA-Z0-9]{10,}$/;

examples.forEach(example => {
    console.log(`${example} => ${regex.test(example)}`)
});

定义元素的位置。

在你的css中覆盖它:

table.dataTable.dtr-inline.collapsed>tbody>tr>td:first-child:before, 
table.dataTable.dtr-inline.collapsed>tbody>tr>th:first-child:before 

}

答案 1 :(得分:1)

将空白<th></th>移至最后一列,并将目标值更新为最后一列。

&#13;
&#13;
 $(document).ready( function () {
  var table = $('#example').DataTable({
    responsive: {
            details: {
                type: 'column'
            }
        },
        columnDefs: [ {
            className: 'control',
            orderable: false,
            targets:   5
        } ],
        order: [ 1, 'asc' ]
  });
} );
&#13;
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables.net/responsive/2.2.1/css/responsive.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.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/responsive/2.2.1/js/dataTables.responsive.min.js"></script>



<!DOCTYPE html>
<html>       
  <body>
    <div class="container">
      <table id="example" class="display responsive" width="100%">
        <thead>
          <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
            <th></th>
          </tr>
        </thead>

        <tfoot>
          <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
            <th></th>
          </tr>
        </tfoot>

        <tbody>
          <tr>
            <td>Tiger Nixon</td>
            <td>System Architect</td>
            <td>Edinburgh</td>
            <td>61</td>
            <td>2011/04/25</td>
            <td>$3,120</td>
            <td></td>
          </tr>
          </tbody>
      </table>
    </div>
  </body>
</html>`
&#13;
&#13;
&#13;