npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

custom-column-resizer

v1.0.2

Published

Javascript to resize table columns

Downloads

11

Readme

ColumnResizer

CustomColumnResizer is a fork ColumnResizer. The plugin is rewritten as vanilla ES6 javascript.

Install

You can install the package via yarn (or npm):

npm install custom-column-resizer
yarn add custom-column-resizer

Features

  • Supports mouse and touch events
  • Persistent layout on refresh
  • No external resources
  • Lightweight and small footprint
  • Customizable column anchors
  • Server side rendering

Usage

Column-resizer can be used directly as a script in a page:

<head>
  <script src="js/column-resizer.js"></script>
  <script type="text/javascript">
     window.onload = function() {
        let resizable = CustomColumnResizer.default;
        
          new resizable(document.querySelector("#somethingUnique"),{
              liveDrag:true,
              draggingClass:"rangeDrag",
              gripInnerHtml:"<div class='rangeGrip'></div>",
              minWidth:8
          });
     };
  </script>
</head>
<body>	 
  <table id="somethingUnique" width="100%" >
    <tr> <th> header </th> <th> header </th> </tr>
    <tr> <td> cell </td> <td> cell </td> </tr>							
    <tr> <td> cell </td> <td> cell </td> </tr>							
  </table>	
</body>

It can also be used as an ES6 module as in this React example:

import ColumnResizer from 'column-resizer';
import React, { Component } from 'react';

class MyTable extends Component {
    
    /**  Other implementation ignored ... **/
    
    componentDidMount() {
        if (this.props.resizable) {
            this.enableResize();
        }
    }

    componentWillUnmount() {
        if (this.props.resizable) {
            this.disableResize();
        }
    }

    componentDidUpdate() {
        if (this.props.resizable) {
            this.enableResize();
        }
    }

    componentWillUpdate() {
        if (this.props.resizable) {
            this.disableResize();
        }
    }

    /*
     * In this example, one table controls the resizing of the
     * another table so both tables' columns resize synchronously.
     */
    enableResize() {
        const remoteTable = ReactDOM.findDOMNode(this)
            .querySelector(`#${this.remoteTableId}`);
        const options = this.props.resizerOptions;
        options.remoteTable = remoteTable;
        if (!this.resizer) {
            this.resizer = new CustomColumnResizer(
                ReactDOM.findDOMNode(this)
                    .querySelector(`#${this.tableId}`), options);
        } else {
            this.resizer.reset(options);
        }
    }

    disableResize() {
        if (this.resizer) {
            /* This will return the current options object.
             *
             * The options, which include the column widths,
             * can be used to re-create the table with the 
             * same column widths as last used.
             */
            this.resizer.reset({ disable: true });
        }
    }
}

Options

  • resizeMode: [type: string] [default: 'fit'] [values: 'fit', 'flex', 'overflow']

    It is used to set how the resize method works. Those are the possible values:

    • 'fit': this is default resizing model, in which resizing a column does not alter table width, which means that when a column is expanded the next one shrinks.
    • 'flex': table can change its width and each column can shrink or expand independently if there is enough space in the parent container. If there is not enough space, columns will share its width as they are resized. Table will never get bigger than its parent.
    • 'overflow': allows resize of columns with overflow of parent container.

  • disable: [type: boolean] [default: false]

When set to true it aims to remove all previously added enhancements such as events and additional DOM elements assigned by this plugin to a single or collection of tables. It is required to disable a previously resized table prior its removal from the document object tree using JavaScript, and also before any DOM manipulations to an already resized table such as adding columns, rows, etc.


  • disabledColumns: [type: array of int] [default: []]

An array of column indexes to be excluded, so it will not be possible to drag them manually.


  • liveDrag: [type: boolean] [default: false]

When set to true the table layout is updated while dragging column anchors. liveDrag enabled is more CPU consuming so it is not recommended for slow computers, specially when dealing with huge or extremely complicated tables.


  • partialRefresh: [type: boolean] [default: false]

This attribute should be set to true if the table is inside of an updatePanel or any other kind of partial page refresh using ajax. Table's ID should be same before and after the partial partial refresh.


  • innerGripHtml: [type: string] [default: empty string]

Its purpose is to allow column anchor customization by defining the HTML to be used in the column grips to provide some visual feedback. It can be used in a wide range of ways to obtain very different outputs, and its flexibility can be increased by combining it with the draggingClass attribute.


  • draggingClass: [type: string] [default: internal css class]

This attribute is used as the css class assigned to column anchors while being dragged. It can be used for visual feedback purposes.


  • minWidth: [type: number] [default: 15]

This value specifies the minimum width (measured in pixels) that is allowed for the columns.


  • headerOnly: [type: boolean] [default: false]

This attribute can be used to prevent vertical expansion of the column anchors to fit the table height. If it is set to true, column handler's size will be bounded to the first row's vertical size.


  • hoverCursor: [type: string] [default: "e-resize"]

This attribute can be used to customize the cursor that will be displayed when the user is positioned on the column anchors.


  • dragCursor: [type: string] [default: "e-resize"]

Defines the cursor that will be used while the user is resizing a column.


  • flush: [type: boolean] [default: false]

Flush is to remove all previously stored data related to the current table layout from session storage.


  • marginLeft: [type: string / null] [default: null]

If the target table contains an explicit margin-left CSS rule, the same value must be used in this attribute (for example: "auto", "20%", "10px"). The reason why it is needed it is because most browsers (all except of legacy IE) don’t allow direct access to the current CSS rule applied to an element in its original units (such as "%", "em" or "auto" values).


  • marginRight: [type: string / null] [default: null]

It behaves in exactly the same way than the previous attribute but applied to the right margin.


  • remoteTable: [type: Node / null] [default: null]

Table element whose column widths will be set by the current table. Remote table must have the same number of columns.


  • widths: [type: array of int] [default: []]

An array of column widths to set the initial width.


  • serialize: [type:Boolean] [default: true]

Flag to determine if column width data will be saved to session storage.


Events

  • onResize: [type: function] [default: null]

If a callback function is supplied it will be fired when the user has ended dragging a column anchor altering the previous table layout. The callback function can obtain a reference to the updated table through the currentTarget attribute of the event retrieved by parameters


  • onDrag: [type: function] [default: null]

This event is fired while dragging a column anchor if liveDrag is enabled. It can be useful if the table is being used as a multiple range slider. The callback function can obtain a reference to the updated table through the currentTarget attribute of the event retrieved by parameters