最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamically change datatables column width via API - Stack Overflow

programmeradmin2浏览0评论

I know we can set the columns width on a datatable via the columns.width option. But now I want to change that option on the xhr event.

Based on the ajax response I get from the server I want to hide/show a column and adjust the other columns width accordingly (they are all fixed size). Is there any way to achieve that via API?

I looked around all docs but seems it's not possible to achieve.

I know we can set the columns width on a datatable via the columns.width option. But now I want to change that option on the xhr event.

Based on the ajax response I get from the server I want to hide/show a column and adjust the other columns width accordingly (they are all fixed size). Is there any way to achieve that via API?

I looked around all docs but seems it's not possible to achieve.

Share Improve this question asked Jun 8, 2017 at 2:10 AlfaTeKAlfaTeK 7,78514 gold badges53 silver badges92 bronze badges 3
  • Are you loading Datatable after ajax response or It's already loaded and you want to hide/show a column and change the width size of others after ajax response ? – Basanta Matia Commented Jun 8, 2017 at 6:05
  • I'm doing that on xhr event of datatables itself. basically I want to do my changes after all my data retrieval from the server. – AlfaTeK Commented Jun 8, 2017 at 12:14
  • I think it's pretty easy. U can check by if else condition. If foo then show/hide bar and change the width of other columns. – Basanta Matia Commented Jun 8, 2017 at 12:17
Add a ment  | 

1 Answer 1

Reset to default 3

You cannot change settings once the dataTable is initalised. But if you are using column.width and you have turned autoWidth off, then the width of each column is easy to change. Here is an example :

var table = $('#example').DataTable({
   autoWidth: false,
   ajax: {
     url: 'https://api.myjson./bins/avxod'
   },
   columns: [
     { data: 'name', width: 50 },
     { data: 'position', width: 50 },
     { data: 'salary', width: 50 },
     { data: 'office',  width: 50}
  ]    
})  

$('#example').on('xhr.dt', function() {
  $('#example thead th:eq(1)').width('400');
})

demo -> http://jsfiddle/cc7x6h64/

It works because column.width is injected into each <th> as style="width: xxx;"

发布评论

评论列表(0)

  1. 暂无评论