Skip to content Skip to sidebar Skip to footer

Resizing Tables In Vanilla Javascript Without Affecting Positioning

I'm trying to resize a th element without affecting the position of the next th in the row, for example I want that the width of the th would affect the width of the next th accord

Solution 1:

Hey guys I figured it out. here's the code.

resizeable() {
let startOffset;

let thElements = this.container.querySelectorAll("th");

for(let i = 0; i < thElements.length; i++) {
  let currentElement = thElements[i];
  currentElement.style.position = 'relative';

  let grip = document.createElement('div');
  grip.innerHTML = "&nbsp;";
  grip.style.top = 0;
  grip.style.right = 0;
  grip.style.bottom = 0;
  grip.style.width = '5px';
  grip.style.position = 'absolute';
  grip.style.cursor = 'col-resize';

  currentElement.appendChild(grip);

  EventManager.on(grip, 'mousedown', (e)=> {
    let nextItem = thElements[i + 1];
    let currentElement = thElements[i];
    let originalNextItemWidth = nextItem.getBoundingClientRect().width;
    let originalCurrentItemWidth = currentElement.getBoundingClientRect().width;

    startOffset = currentElement.offsetWidth - e.pageX;


    EventManager.on(document.body, 'mousemove', (e)=> {

      // Subtract that difference from the next items widthif (currentElement) {
        currentElement.style.width = startOffset + e.pageX + 'px';

        let offset = originalCurrentItemWidth - currentElement.getBoundingClientRect().width;
        nextItem.style.width = (originalNextItemWidth + offset) + 'px';

      }
    });
  });

  EventManager.on(document.body, 'mouseup', (e)=> {
    EventManager.off(document.body, 'mousemove');
    currentElement = null;
  });
}

}

Post a Comment for "Resizing Tables In Vanilla Javascript Without Affecting Positioning"