DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Creating Resizable Columns in Angular 2

Creating Resizable Columns in Angular 2

In this quick tutorial, we go over how to make column size customizable using the Angular 2 framework, the jQuery library, and a little HTML and CSS.

Rohit Kawade user avatar by
Rohit Kawade
·
Sep. 10, 18 · Tutorial
Like (2)
Save
Tweet
Share
20.60K Views

Join the DZone community and get the full member experience.

Join For Free

I am working on HTML table and trying to implement some features using Angular 2 and jQuery.

I prefer primeng components but found a similar solution for resizing the columns for my custom grid.

Initial Table:

First, we need to create header columns. I added a <span> tag in <th> and placed it near the right border.

HTML:

<th>
    <span class="ui-column-resizer"></span>
    <span class="gridHeader">{{ name }}</span>
</th>

CSS:

thead tr th {
  position: relative;
}
span.ui-column-resizer {
  display: block;
  position: absolute;
  top: 0;
  right: 0;
  margin: 0;
  width: 8px;
  height: 100%;
  padding: 0;
  cursor: col-resize;
  border: 1px solid transparent;
}

I highlighted the <span> in the following image.

Adding functionality to grab the table border:

<th>
    <span 
       class = "ui-column-resizer"
       (mousedown) = "onMouseDown($event)">
    </span>
    <span class = "gridHeader">{{ name }}</span>
</th>

Add a (mousedown) event to <span> to grab table border (resizer span element)

private onMouseDown(event){
  this.start = event.target;
  this.pressed = true;
  this.startX = event.x;
  this.startWidth = $(this.start).parent().width();
  this.initResizableColumns();
}

Assign ‘true’ to this.start to start the column resize.

Here we will save the information in variables, such as which column we need to resize and the current width and x-position for the header column. Then call the initResizableColumns() function.

We will use the Angular 2 Renderer to track the mouse move and mouse up events.

import {Component,Renderer} from '@angular/core';

Add the renderer in the constructor

constructor(public renderer: Renderer) {}

Use the initResizableColumns() function :-

private initResizableColumns() {
     this.renderer.listenGlobal('body', 'mousemove', (event) => {
        if(this.pressed) {
           let width = this.startWidth + (event.x - this.startX);
           $(this.start).parent().css({'min-width': width, 'max-   width': width});
           let index = $(this.start).parent().index() + 1;
           $('.glowTableBody tr td:nth-child(' + index + ')').css({'min-width': width, 'max-width': width});
        }
     });
     this.renderer.listenGlobal('body', 'mouseup', (event) => {
     if(this.pressed) {
         this.pressed = false;
     }
   });
}

Bind the mousemove event to track mouse movements and calculate the current width.

let width = this.startWidth + (event.x - this.startX);

Assign the calculated width to <th> i.e. $(this.start).parent()

Now we resized the header column but we need to resize the body column as well.

For that, get the index of the resized header column.

let index = $(this.start).parent().index() + 1;

Then apply the header column width to body column by using nth-child(index).

$('.tableBody tr td:nth-child(' + index + ')').css({'min-width': width, 'max-width': width});

Then bind the moveup event to complete the column resize by making this.pressed ‘false’.

this.renderer.listenGlobal('body', 'mouseup', (event) => {
   if(this.pressed) {
       this.pressed = false;
   }
});

Resized columns:

Database AngularJS

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?
  • Master Spring Boot 3 With GraalVM Native Image
  • Rust vs Go: Which Is Better?
  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: