gvd
gvd

Reputation: 1592

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

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?

Upvotes: 0

Views: 7627

Answers (2)

Twisty
Twisty

Reputation: 30893

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.

Upvotes: 1

juntapao
juntapao

Reputation: 427

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%;
}

Upvotes: 1

Related Questions