jquery - 固定列宽

标签 jquery html css datatables

我使用 Jquery 数据表构建了一个数据表,该数据表通过 Ajax 以 JSON 格式从服务器动态加载数据。这是我的代码:

          $.ajax({
              type: 'POST',
              url: "{{URL::to('/')}}/mynote_list",
              headers: {
                  'X-CSRF-TOKEN': '{{ csrf_token() }}'
              },
              dataType: 'json',
              success: function(data) {
                  var dataSet = data.data;
                  mytable = $('#mynote_table').DataTable({
                      destroy: true,
                      paging: false,
                      searching: false,
                      ordering: true,
                      bInfo: false,
                      columnDefs: [
                      { "width": "150px", "targets": [0] },       
                      { "width": "40px", "targets": [1] }
                      ],
                      fixedColumns: true,
                      "data": dataSet,
                      "columns": [
                          {"data": "my_note"},
                          { "data": "Link",
                          "mRender": function (data, type, full) {
                          return '<button type="button" class="fa fa-trash delete-button btn btn-flat"  style="font-size:14px;color:red" id="'+full.id+'" ></button>';
                            }
                          },
                      ],
                      "order": [
                          [1, 'asc']
                      ]
                  });
              }
          });

但结果列宽不是我要设置的。

Particuls| Action 
=========+=============
  AAAAA  |  Delete        
---------+-------------
  BBBBB  |  Delete         
---------+-------------
  CCCCC  |  Delete   
---------|------------

我该如何解决这个问题?

最佳答案

你可以配置

columnDefs: [
                      { width: 20, targets: 0 },       
                     { width: 100, targets: 1 }
                      ],

引用 https://datatables.net/extensions/fixedcolumns/examples/initialisation/size_fixed.html

var dataSet = 
[
{id: 1, my_note: "ABBBBBBBBBB", Link: "B"},
{id: 2, my_note: "A", Link: "B"}];
                  mytable = $('#mynote_table').DataTable({
                      destroy: true,
                      paging: false,
                      searching: false,
                      ordering: true,
                      bInfo: false,
                      columnDefs: [
                      { width: 20, targets: 0 },       
                     { width: 100, targets: 1 }
                      ],
                      fixedColumns: true,
                      "data": dataSet,
                      "columns": [
                          {"data": "my_note"},
                          { "data": "Link",
                          "mRender": function (data, type, full) {
                          return '<button type="button" class="fa fa-trash delete-button btn btn-flat"  style="font-size:14px;color:red" id="'+full.id+'" ></button>';
                            }
                          },
                      ],
                      "order": [
                          [1, 'asc']
                      ]
                  });
table{
  margin: 0 auto;
  width: 100%;
  clear: both;
  border-collapse: collapse;
  table-layout: fixed; // ***********add this
  word-wrap:break-word; // ***********and this
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<table id="mynote_table" class="display" style="width:100%">
        <thead>
            <tr>
                <th>my_note</th>
                <th>Link</th>
               
            </tr>
        </thead>
       
    </table>

关于jquery - 固定列宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56813802/

相关文章:

css html 5 菜单在某些页面上向左移动

jQuery 动画插值问题

css - Wordpress 主题找不到 CSS(本地主机)

jquery - 将 fading "tooltips"添加到网站

java - HTMLDocument 文本中位置或偏移量的含义

javascript - Angularjs 将 json 响应合并为一个

javascript - 如何使用 Javascript 更改事件文本输入的值?

html - 控制在 flex 容器中垂直排列的 flex 元素的宽度

javascript - jQuery:如何从多个元素获取文本并将其打印在多行上?

javascript - 仅捕获不是由 anchor 击引起的 hashchange 事件