【问题标题】:How can I display the responsive datatable's expand/collapse icon in the right side of the first column's value?如何在第一列值的右侧显示响应式数据表展开/折叠图标?
【发布时间】:2018-03-09 10:51:23
【问题描述】:

我正在使用响应式数据表 (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>

【问题讨论】:

  • 请提供一个与您遇到的问题重复的简单代码示例。
  • 你的代码在哪里?

标签: javascript css datatable datatables


【解决方案1】:

有一个css选择器

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

定义元素的位置。

在你的 css 中覆盖它:

table.dataTable.dtr-inline.collapsed>tbody>tr>td:first-child:before, 
table.dataTable.dtr-inline.collapsed>tbody>tr>th:first-child:before  {
left: auto;
right: 4px;

}

【讨论】:

    【解决方案2】:

    将空白&lt;th&gt;&lt;/th&gt; 移动到最后一列,并将目标值也更新到最后一列。

     $(document).ready( function () {
      var table = $('#example').DataTable({
        responsive: {
                details: {
                    type: 'column'
                }
            },
            columnDefs: [ {
                className: 'control',
                orderable: false,
                targets:   5
            } ],
            order: [ 1, 'asc' ]
      });
    } );
    <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>`

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 2011-02-09
      • 1970-01-01
      • 2015-03-08
      • 2018-01-24
      • 1970-01-01
      • 2015-12-09
      相关资源
      最近更新 更多