GithubHelp home page GithubHelp logo

dhobi / datatables.colresize Goto Github PK

View Code? Open in Web Editor NEW
42.0 5.0 25.0 29 KB

Column resizing plug-in for DataTables

License: MIT License

CSS 0.96% JavaScript 60.87% HTML 38.17%
datatables datatables-plugin datatables-column-resize javascript jquery

datatables.colresize's Introduction

jQuery dataTables ColResize

Plugin to resize columns by mouse drag or touch.

Demo:

https://dhobi.github.io/datatables.colResize/

Installation:

As npm package:

npm i datatables.net-colresize-unofficial

Manually:

Javascript file to include: jquery.dataTables.colResize.js

Stylesheet to include: jquery.dataTables.colResize.css

With CDN links:

https://cdn.jsdelivr.net/npm/datatables.net-colresize-unofficial@latest/jquery.dataTables.colResize.css
https://cdn.jsdelivr.net/npm/datatables.net-colresize-unofficial@latest/jquery.dataTables.colResize.js

Usage:

The plugin will try to initialize itself on preInit.dt event.

var options = { ...see below... };
// Either:
var table = $('#example').DataTable({
    colResize: options
});

// Or:
var table = $('#example').DataTable();
new $.fn.dataTable.ColResize(table, options);

// Available methods:
table.colResize.enable();  // enable plugin (i.e. when options was isEnabled: false)
table.colResize.disable(); // remove all events
table.colResize.reset();   // reset column.sWidth values
table.colResize.save();    // save the current state (defaults to localstorage)
table.colResize.restore(); // restore the state from storage (defaults to localstorage)

Options:

colResize = {
    isEnabled: true,
    saveState: false,
    hoverClass: 'dt-colresizable-hover',
    hasBoundCheck: true,
    minBoundClass: 'dt-colresizable-bound-min',
    maxBoundClass: 'dt-colresizable-bound-max',
    isResizable: function (column) {
        return true;
    },
    onResizeStart: function (column, columns) {
    },
    onResize: function (column) {
    },
    onResizeEnd: function (column, columns) {
    },
    getMinWidthOf: function ($thNode) {
    },
    stateSaveCallback: function (settings, data) {
    },
    stateLoadCallback: function (settings) {
    }
}

isEnabled

default: true

Specify whether resize functionality is enabled on dataTable init

saveState

default false

Specify whether state should automatically save when changed and restore on page load

hoverClass

default: 'dt-colresizable-hover'

Class which will be toggled on resizable & hovered <th> element (right border +/- 10px)

hasBoundCheck

default: true

Specify whether min-width / max-width styles are checked to keep resizing of column in bounds

minBoundClass

default: 'dt-colresizable-bound-min'

Class which will be toggled for 500ms once the min-width of the column has been reached

maxBoundClass

default: 'dt-colresizable-bound-max'

Class which will be toggled for 500ms once the max-width of the column has been reached

isResizable

default: column.isResizable / true

Called once during plugin/datatable init. Return false will not attach the drag events to the column.

onResizeStart

Callback on drag start / touchstart event. Parameter is the dataTable column which has been resized and all other columns in the table.

onResize

Callback on dragging / touchmove event. Parameter is the dataTable column which is currently being resized.

onResizeEnd

Callback on drag end / touchend event. Parameter is the dataTable column which has been resized and all other columns in the table.

getMinWidthOf

default: null

If defined (not null) will be used to calculate the minimal width of the given jQuery th - node.

If null (default) the plugin tries to detect the minimal width by

  1. Looking at the CSS min-width property
  2. Guessing the width by checking the space the text label uses
  3. Minimum 30px width

stateSaveCallback

default: uses localStorage

Callback when state needs to save. Parameter is an array of column sizes.

example: [437,412,416,258,397,357]

stateLoadCallback

default: uses localStorage

Callback when state needs to be restored. You will need to return an array of column sizes.

example: [437,412,416,258,397,357]

datatables.colresize's People

Contributors

brambruning avatar dhobi avatar hchiam avatar javaman1138 avatar jqnatividad avatar jstark518 avatar pafernandez-oesia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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

datatables.colresize's Issues

Support for RTL languages?

I would like to know if this plugin works properly for RTL languages like Arabic, Hebrew etc. Any help/information regarding this would be greatly appreciated. Thanks in advance.

Error occurs when refreshing page if hasBoundCheck = true.

This can be reproed on the current version of the demo page.
I'm using Chrome 114 on Windows 10 Home.

If you go to the demo page, resize a column, and refresh the page you get a JS error that results in the grid failing to complete it's load.
image

jquery.dataTables.colResize.js:478 Uncaught TypeError: Cannot read properties of null (reading 'addClass')
    at ColResize._fnShowMaxBoundReached (jquery.dataTables.colResize.js:478:39)
    at ColResize._fnBoundCheck (jquery.dataTables.colResize.js:457:22)
    at ColResize._fnApplyWidth (jquery.dataTables.colResize.js:337:61)
    at jquery.dataTables.colResize.js:164:22
    at Array.forEach (<anonymous>)
    at ColResize.fnRestoreState (jquery.dataTables.colResize.js:151:18)
    at ColResize._fnConstruct (jquery.dataTables.colResize.js:302:22)
    at new ColResize (jquery.dataTables.colResize.js:103:18)
    at HTMLDocument.<anonymous> (jquery.dataTables.colResize.js:613:17)
    at HTMLDocument.dispatch (jquery-2.2.4.min.js:3:7537)

If you reload the page, the error persists until you clear the local storage.
Here's the generated contents of my local storage for referece.

image

The issue appears to have something to do with calculating/inferring the max widths of an element.

I downloaded the repo and was able to reproduce the issue locally as well. However, if I change index.html so that the hasBoundCheck option is false instead of true, the issue goes away.

I think for my purposes I don't need the hasBoundCheck functionality, but if this could be fixed that would be awesome. Let me know if there's any other information you need.

Not working in dataTables v2

Just migrated to datatables v2.0.1 and colResize not working.
It fails at the version check and fails after fixing and passing version check.
There are no error messages in the console after the version check passes.

Header width not matching column width

I am having an issue where, when resizing to min width, the header is a different width from the column. Any help on how to fix this? You can see this behavior in your demo.

Upgrading to dataTables.Net 1.12.1 causes resize cursor to not show

I upgraded to dataTables.net version 1.11.5 to 1.12.1 and the resize cursor will not longer show. I can resize the columns, but is is difficult to see where to drag from without the cursor. I changed the library reference back and forth in my code and it seems the upgraded is definitely the problem.

Width fetched from API is not applied when hasBoundCheck is set to true

When resizing columns, the width is reflected properly. But on re-rendering the table, the width array is returned in the stateLoadCallback properly but the proper width is not set. Kindly help.

FYI, the plugin loads width properly on re-render when hasBoundCheck is set to false though.

scroll option with column resize

Hi @dhobi, We have check your script but when we add a scrollbar in the grid, it stops working. it only allow to resize the header only. This is what we have done in javascript:

$(document).ready( function () {
var table = $('#example').DataTable({ "scrollX": true});

Do you have any suggestion on that?

CDN links for convenience

I've used the datatables.colResize plugin in production and in CodePen demos a few times already. It'd be nice if there were CDN links in the readme.md to make it easier to find the correct URLs.

For example, with explicit version numbers:
https://cdn.jsdelivr.net/npm/[email protected]/jquery.dataTables.colResize.css
https://cdn.jsdelivr.net/npm/[email protected]/jquery.dataTables.colResize.js

Or explicitly fetching the latest, to reduce maintenance of the readme:
https://cdn.jsdelivr.net/npm/datatables.net-colresize-unofficial@latest/jquery.dataTables.colResize.css
https://cdn.jsdelivr.net/npm/datatables.net-colresize-unofficial@latest/jquery.dataTables.colResize.js

Got error if there are some invisible columns

I got an issue when some columns in my table are invisible which can be set by
columnDefs: [ { targets: [0, 1], visible: false }]
or
columns: [{ title: 'Customer Name', data: 'Name', visible: false }]

I'm using colResize with saveState = true. It's ok for the first time but since the next time I got the error like this
Uncaught TypeError: Cannot read properties of null (reading 'match')
from the line
column.sWidth.match(/(\d+)/i)
as sWidth was undefined which could be caused by calculating the width of an invisible DOM.

Actually, we can check null / undefined then skip & continue with visible columns because we do not need to resize invisible columns.

Third column not working and undefined index

I have 2 issues. First, when you're on the demo page, the third column cannot be resized. I've looked, and there's nothing that mentions that it's intentionally designed this way.
issuebug

I'm also experiencing issues when applying it to a table in my project. It's a table created via JavaScript, and after it's created, I apply the following function:

$('#table').DataTable({
  ordering: true,
  searching: true,
  info: false,
  paging: false,
  colResize: {
    isEnabled: true,
    saveState: false,
    hoverClass: 'dt-colresizable-hover',
    hasBoundCheck: true,
    minBoundClass: 'dt-colresizable-bound-min',
    maxBoundClass: 'dt-colresizable-bound-max',
  },
});

Every time I try to make a column larger, I get the following error in the console:
image

How to save/restore the width via Ajax?

Hello!
I try to save and restore the width of each column. This is done by Ajax with a call to a Backend.
Saving seams to work for me, but I am not able to set the width of a column.
Maybe somebody can guide me?
Thanks

Cannot read properties of undefined (reading 'fnEnable')

dataTable.colResize.enable();

jquery.dataTables.colResize.js:621 Uncaught TypeError: Cannot read properties of undefined (reading 'fnEnable')
    at B.<anonymous> (jquery.dataTables.colResize.js:621:28)
    at B.iterator (datatables.min.js:34:52119)
    at B.<anonymous> (jquery.dataTables.colResize.js:620:21)
    at Object.enable (datatables.min.js:34:53602)
    at <anonymous>:1:21

New colResize Option Is Awesome! But Requires Care When Using With colReorder

I find the new colResize plugin by Daniel Hobi awesome!

Allan mentions it in What's New. Although the free demo has expired, the plugin definitely works.

Minor detail, but when using in conjunction with colReorder, the user needs to resize only enough to not drag target column too far into next column, else the colReorder kicks in.

Here's link to my simple demo.

I tried mitigating with colReorder.realtime() set to false, but to no avail.

Might be better for end users to suppress colReorder while colResize is engaged. I will try to do this with jQuery .on action, but in mean time, heads-up.

Will send separate note to Daniel as well.

But again, very welcomed addition to dt!

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.