smasala / ColResize

DataTables extension to make columns resizable
MIT License
22 stars 12 forks source link

ColResize

Doesn't always work under jQuery version 3.2.x: [https://github.com/jquery/jquery/issues/3611]() use 3.1.1 if you can until 3.3.x is available :)

ColResize adds the ability for the end user to click and drag column headers to resize a DataTables table.

Demo

https://smasala.github.io/ColResize/

Compatibility

Tested in:

Installation

NPM and Bower

If you prefer to use a package manager such as NPM or Bower, distribution repositories are available with software built from this repository under the name dt-colresize. Styling packages for Bootstrap, Foundation and other styling libraries are also available by adding a suffix to the package name.

Please see the DataTables NPM and Bower installation pages for further information. The DataTables installation manual also has details on how to use package managers with DataTables.

Basic usage

Import necessary files:

<!DOCTYPE html> <!-- must be set for scrollY + fixed header feature -->
...
<script type="text/javascript" src="https://github.com/smasala/ColResize/raw/master/./js/dataTables.colResize.js"></script>
<link rel="stylesheet" href="https://github.com/smasala/ColResize/blob/master/./css/dataTables.colResize.css" />

ColResize is initialised using the colResize option in the DataTables constructor - a simple boolean true will enable the feature. Further options can be specified using this option as an object - see the documentation for details. autoWidth and scrollX must also be set as false.

Example:

$(document).ready( function () {
    $('#myTable').DataTable( {
        colResize: true,
        autoWidth: false,
        scrollX: false
    } );
} );
/** Customise the resize bar **/
.dt-colresizable-col {
    /** styles go here **/
}

Configuration / options

$(document).ready( function () {
    $('#myTable').DataTable( {
        colResize: {
            scrollY: 200,       //cannot be used with DT scrollY
            minColumnWidth: 50
        },
        autoWidth: false,
        scrollX: false
    } );
} );

Possible Options:

API

This extension also has a few extra api functions which can be called by using the created DataTables instance.

var dtInstance = $(table).DataTable({... colResize: true ...});
dtInstance.colResize.redraw();

Available functions:

events

Events can be caught on the DataTable instance object

var dtInstance = $(table).DataTable({... colResize: true ...});
dtInstance.on("column-resized.dt" function(event, columnIndex, newColumnWidth) {
    // do something  
});

Possible events:

Documentation / support