简体   繁体   English

如何使用jQuery UI使datatables.js的列可调整大小

[英]How to make columns of datatables.js resizable with jQuery UI

I found a solution to resize table's columns in stackoverflow 我找到了一种在stackoverflow中调整表的列大小的解决方案

jQuery UI Resize with table and colspans 带有表和列的jQuery UI调整大小

I'm trying to apply it to a datatable that is populated using server side processing, but datatable doesn't allow change the column width. 我正在尝试将其应用于使用服务器端处理填充的数据表,但是该数据表不允许更改列宽。

I have tried this http://jsfiddle.net/BlackSRC/JvELx/2/ 我已经尝试过这个http://jsfiddle.net/BlackSRC/JvELx/2/

I have this simple datatable 我有这个简单的数据表

<table id="datatable-1" class="table table-hover" style="width: 100%">
</table>

My datatable initialization is: 我的数据表初始化是:

 $(table).DataTable({ 'lengthMenu': [[10, 25, 50, 100], [10, 25, 50, 100]], 'ordering': false, 'processing': true, 'serverSide': true, 'dom': 'Blfrtip', 'ajax': { 'url': 'ajax.php', 'type': 'GET' }, 'columns':[ {'data': 'id', 'title': 'Id'}, {'data': 'A', 'title': 'A'}, {'data': 'B', 'title': 'C'}, {'data': 'C', 'title': 'D'}, {'data': 'D', 'title': 'E'} ] }); 

Anyone know how can resize column using datatables.js? 有人知道如何使用datatables.js调整列的大小吗?

Try to set autoWidth option to false on your datatable 尝试在数据表autoWidth选项设置为false

$(table).DataTable({
    'lengthMenu': [[10, 25, 50, 100], [10, 25, 50, 100]],
    'ordering': false,
    'processing': true,
    'serverSide': true,
    'autoWidth': false,
    'dom': 'Blfrtip',
    'ajax': {
        'url': 'ajax.php',
        'type': 'GET'
    },
    'columns':[
        {'data': 'id', 'title': 'Id'},
        {'data': 'A', 'title': 'A'},
        {'data': 'B', 'title': 'C'},
        {'data': 'C', 'title': 'D'},
        {'data': 'D', 'title': 'E'}
    ]
});

then add css 然后添加CSS

table {
    width: 100%;
}

I don't see an issue. 我没看到问题。 You do want to run .DataTable() first, then run .resizable() 您确实想先运行.DataTable() ,然后再运行.resizable()

Example: http://jsfiddle.net/Twisty/ah67jopf/3/ 示例: http//jsfiddle.net/Twisty/ah67jopf/3/

JavaScript 的JavaScript

$(function() {
  $(".data").DataTable({
    'lengthMenu': [
      [10, 25, 50, 100],
      [10, 25, 50, 100]
    ],
    'ordering': false,
    'processing': true,
    'serverSide': true,
    'dom': 'Blfrtip',
    'ajax': {
      'url': 'ajax.php',
      'type': 'GET'
    },
    'columns': [{
      'data': 'id',
      'title': 'Id'
    }, {
      'data': 'A',
      'title': 'A'
    }, {
      'data': 'B',
      'title': 'C'
    }, {
      'data': 'C',
      'title': 'D'
    }, {
      'data': 'D',
      'title': 'E'
    }]
  });
  $('table th').resizable({
    handles: 'e',
    minWidth: 18,
    stop: function(e, ui) {
      $(this).width(ui.size.width);
    }
  });
});

Updated 更新

In the Stop callback, you can set the new size of the th element. 在Stop回调中,可以设置th元素的新大小。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM