0

I found a solution to resize table's columns in stackoverflow

jQuery UI Resize with table and colspans

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/

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?

1
  • show us what you have tried so far. Commented Oct 26, 2017 at 21:31

2 Answers 2

1

Try to set autoWidth option to false on your datatable

$(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

table {
    width: 100%;
}
Sign up to request clarification or add additional context in comments.

Comments

1

I don't see an issue. You do want to run .DataTable() first, then run .resizable()

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

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.

Hope that helps.

9 Comments

This example work perfectly, is like my code, but mine is not working. I can see in the inspector that th width change, but in browser doesn't show the resize.
@omixam do you see any errors in console? How are libraries being loaded, what order, in the <head>?
Any error in console.I disabled all libraries the order is the following: css: datatables and than jquery-ui, for JS: jquery, jquery-ui, and datatable.
I would advise a different order: Jquery UI CSS, Datatable CSS, jQuery, jQuery UI, DataTable. Should work as you described. I assume you meant no errors appear in console?
I used the order that you suggest me, but the result is the same. There isn't errors in the console.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.