Giter VIP home page Giter VIP logo

colresize's Introduction

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:

  • Chrome 59.0.3071.115
  • FF 54.0.1
  • IE 8, 9 (without scrollY + fixed header feature)
  • IE 10+ (for scrollY + fixed header feature)

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="../js/dataTables.colResize.js"></script>
<link rel="stylesheet" href="../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:

  • minColumnWidth: number [default=10] must be equal too or greater than the sum of the cell's padding (left + right)
  • resizeTable: boolean [default=false] set to true if you want the entire table to be shifted when a column is resized. Default behaviour is that when a column is resized the neighbouring column expands/shrinks accordingly. If set to true, when a column is resized, the neighbouring column is left untouched and the table shifts accordingly.
  • stateSave: boolean [default=false] set to true if you wish to save the column width states using the DT default state saving mechanism
  • scrollY: number [default=false] much like the DT scrollY. Sets a fixed header and tbody height with scroll functionality. Cannot be used with DT scrollY option.

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:

  • redraw(): forces a complete redraw of the draggable bars etc by destroying the current DOM elements and recreating them
  • visible(columnIndex, visibilityBoolean, redrawCalculationsBoolean): when using the ColResize extension, the standard column visibility function should be avoided because the events are too late for ColResize to use. Use this visible function instead. Original docs for more info.

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:

  • column-resized.dt arguments: (jQuery Event Object, columnIndex [integer], newColumnWidth [integer]) - fired after a drag event on mouse up.

Documentation / support

colresize's People

Contributors

smasala avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

colresize's Issues

Memory leak when using with Vue

I'm running into an issue with ColResize when using the plugin within a single page Vue application.

It appears that the plugin is holding something from the table in memory, and each time a DataTable within the application is visited, the memory size grows by the table size, but it is not released when the table is navigated away from.

The table is being properly destroyed, but something within ColResize is holding onto memory references within the table, and causing a memory leak.

I've tried adding additional calls in the destroy() function within the plugin to try to remove more listeners and null more objects, but I'm not having any success.

Wanted to see if anyone had any issues like this in the past or any guidance about how ColResize allocates memory.

Change relative column size (percent)

Hi
Can you maybe give me some pointer what to change in case I just want to change/resize the percentage distribution of the table columns?

Background is that I do not want vertical scrollbars and always fit everything into the cells (with ellipsis or line breaks depending on the cell), however it would be great if the user could change the size distribution.

The table is already setup with percent-wise column widths.

stateSave: true only saves header widths, but not column width.

Google Chrome 71

Issue appears with stateSave: true (both DT and colResize) and with colresize`s scrollY: true:

$('#content-table').DataTable( {
    colResize: {
        scrollY: 500,
        minColumnWidth: 50,
        stateSave: true
    },
    autoWidth: false,
    scrollX: false,
    stateSave: true
} );

This causes inconsistency after page reloads: only the header width got saved, but not column width.
Disabling scrollY under colResize fix this behaviour but the table is no longer scrollable.

Does not work stably in excess columns

Hi,

I have about 90 columns in a table. Since it is made according to the width of the area, 90 columns come together and are not read In addition, sizing is not possible on the width stably.

Bar and column not quite insync

While playing with the demo I noticed that if you grab a bar and drag it, it doesn't quite stay insync with the columns. To see, pick up the bar between the first and second columns and then drag it all the way to the left, then as far as possible to the right (blocked by the next bar - nice touch!). The sorting icon can now be seen to the right of the drag bar. If you use the inspect you can see a bit more easily the cells not lining up with the bars.

I suspect its a sub-pixel rendering issue. On each mouse move there is just a small offset which is gradually adding up. One option might be to use getBoundingClientRect() which will get sizes with sub-pixel precision, while another option might be to redraw the bars to match the columns when mouseup is triggered (although not quite as nice UX).

One way or another - this is superb and easily the best column resizing I've seen for DataTables!

Add ability to save column sizes

When using the stateSave Datatables functionality would like the column sizes saved so when the user expands or shrinks the column size it will remember those settings the next time the table is initialized.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.