Unlocking the potential of resizable bootstrap table columns in React

Currently utilizing a bootstrap table

<div class="table-responsive">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>#</th>
        <th>Table heading</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>Table cell</td>
      </tr>
    </tbody>
  </table>
</div>

Exploring options to implement resizable columns using React framework

Answer №1

Here is the code snippet for creating resizable columns in a table using jQuery:

$(document).ready(function() {
//$("td,th")
$("td:first-child,td:nth-child(2),td:nth-child(3)")
  .css({
    /* required to allow resizer embedding */
    position: "relative"
  })
  /* check .resizer CSS */
  .prepend("<div class='resizer'></div>")
  .resizable({
    resizeHeight: false,
    // we use the column as handle and filter
    // by the contained .resizer element
    handleSelector: "",
    onDragStart: function(e, $el, opt) {
      // only drag resizer
      if (!$(e.target).hasClass("resizer"))
        return false;
      return true;
    }
  });
  });
html,
body {
  height: 100%;
  font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
  padding: 0;
  margin: 0;
}

.page-container {
  margin: 20px;
}

table {
  border-collapse: collapse;
  width: 800px;
}

td,
th {
  padding: 8px;
  border: 1px solid silver;
}

th {
  color: white;
  background: #535353;
}

pre {
  margin: 20px;
  padding: 10px;
  background: #eee;
  border: 1px solid silver;
  border-radius: 4px;
}

.resizer {
  position: absolute;
  top: 0;
  right: -8px;
  bottom: 0;
  left: auto;
  width: 16px;    
  cursor: col-resize;       
}
<div class="page-container">

  <h1>
            jquery-resizable - Table Column Resizing
        </h1>
  <hr />
  <p>
    This example makes the first two columns of the table resizable.
  </p>

  <hr />

  <table>
    <thead>
      <th>
        col 1
      </th>
      <th>
        col 2
      </th>
      <th>
        col 3
      </th>
      <th>
        col 4
      </th>
    </thead>

    <tbody>
      <tr>
        <td>
          Column 1
        </td>
        <td>

          Column 2
        </td>
        <td>

          Column 3
        </td>
        <td>
          Column 4
        </td>
      </tr>
      <tr>
        <td>
          Column 1
        </td>
        <td>
          Column 2
        </td>
        <td>
          Column 3
        </td>
        <td>
          Column 4
        </td>
      </tr>
    </tbody>
  </table>
  <hr />
  <p>
    To create resizable columns requires a bit of extra effort as tables don't have an easy way to create a drag handle that can be moved around.
  </p>
  <p>
    To make columns resizable, we can add an element to the right of the column to provide the handle so we have a visual indicator for dragging. The table cell to resize is made <code>position:relative</code> and the injected element is <code>position: absolute</code>    and pushed to the right of the cell to provide the drag handle. Dragging then simply resizes the cell.
  </p>
  <p>
    In practice this means you need some CSS (or jquery styling) to force the injected styling and the logic to inject the element.
  </p>

</div>

Answer №2

Adjustable Column Component for react-bootstrap

You can check out the code snippet on CodeSandbox for Resizable Columns

This component utilizes the 'column-resizer' npm package

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

ASP: The submenu items remain visible

I must express my gratitude for the wealth of knowledge this site has provided me with. It has been an invaluable resource for me this year, even though I have scoured every corner and still come up empty-handed. Allow me to simplify the situation at hand ...

The JSP AJAX response comes back empty

I am encountering an issue where I am using JQuery Ajax to call a REST API in JSP, but it keeps returning null no matter how I try. Strangely enough, the same code works when used in HTML. I have been searching online for a solution but haven't found ...

What is the best method for concealing a specific column in a table with antd?

Here is an example of code: { title: 'Site', dataIndex: 'site' } I am trying to figure out how to hide the site column in my Ant Design React UI. Unfortunately, I couldn't find a show/hide function in the column API documentation ...

The optimal approach for AngularJS services and promises

I have a unique setup in my AngularJS application where I utilize services to make API calls using $http and handle promises in my controllers. Here's an example of my current approach: app.service('Blog', function($http, $q) { var deferr ...

Incorporating Fresh Routes on the Fly: A Guide

Can a new route be added dynamically to the router at runtime after it has been initialized? For instance, if a user enters information in an input field, can I then create a new route based on that input? ...

Bootstrap Button Problem: Difficulty arranging buttons in a neat way, unable to position them next to each other

I'm currently working on a real estate website project and have designed a Photoshop template which is uploaded on Behance. Check it out here. Right now, I'm creating the static version of the real estate store template and facing an issue with ...

Can you make two columns in CSS that are left floated and maintain their original order?

While the title may not provide much clarity, I am struggling to put into words exactly what I am trying to achieve. I have created a layout in Photoshop which I have shared below to help illustrate my goal. Essentially, I have a blog that displays my sto ...

How can I align a single button to the left side while positioning the rest of the elements to the right in Bootstrap 4?

I am attempting to align the B1 button on the left side and have the remaining elements positioned on the right side. Below is my code snippet using Bootstrap 4.1: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/b ...

Special Bootstrap's hamburger menu

My website has a vertical navigation bar with a breakpoint set for medium-sized screens to display a hamburger menu. When the hamburger menu appears, I want to hide the text following the icons to save space and only show the icons. Clicking on the hamburg ...

Implementing GRIDFS for mp3 storage in a meteor application

Being a novice in the world of meteor, I am currently working on defining an mp3 collection and then uploading music to it from the admin page. The packages that are installed include: cfs:standard-packages cfs:gridfs cfs:filesystem 1) I have successful ...

Present location of current page

I utilize angularJS to create a web page with the utilization of ngView for a single page and multiview. Is there a way to display the current page's location, such as Home/abc/dcv? On this website https://docs.angularjs.org/guide/, the page location ...

Exploring a Collection in Meteor: Working with an Object

This issue has been plaguing me for some time now, and it's starting to really get on my nerves. To put it simply: How can we iterate over an object within a collection in a template? Each entry in the collection typically looks like this: { "_i ...

Is a page reload triggered by clicking a webelement?

My Latest Project Lately, I've been working on developing a utility method to enhance my Selenium automation testing. This method aims to locate and wait for web elements efficiently, supporting various locators and incorporating timeouts for better ...

What could be causing the excessive blank space below the image in my React application? What is generating this enigmatic div element?

I've been working on creating a login Component in React, and I'm facing an issue when trying to add the brand image. There is a significant amount of white space appearing below the image. After some investigation, I found that there is a myste ...

What is the best way to insert a thousand separator into a TextField?

I attempted to implement a thousand separator using the react-number-format package, but unfortunately, I was unable to get it working. My development environment utilizes react 18 and here is the code snippet I used: function NumberFormatCustom(props) { ...

Leveraging global attributes beyond Vue components - Vue 3

I have created custom service instances in main.ts app.config.globalProperties.$service1 = new Service1(); app.config.globalProperties.$service2 = new Service2(); While I can use these instances inside Vue components, I also want to be able to utilize the ...

Hide all the div elements on the web page and only display one when a button is clicked

I have successfully set up a few buttons that can show and hide divs on click. However, I am wondering if it is possible to hide all other divs when one is showing, and also have "divone" show up on load. Buttons: <button class="btn btn-outline-primar ...

The ClearInterval() function does not take effect instantly

I've implemented a Carousel with an auto-toggle feature using the setInterval() function to switch between tabs every 4 seconds. However, I now need to stop this automatic toggling when a specific tab is clicked. You can find the HTML and jQuery for ...

Get Angular events in the proper order

I am currently facing challenges with event handling in AngularJs. In my service, I send out events using the following line of code : $rootScope.$emit("FNH."+this.status, flowNodeHelper); When receiving the event in service "S1," I handle it as follows ...

What is the proper method for verifying the status following the submission of a request on the

My goal is to validate states after making requests to the server using axios. The server is set up so that if a form is submitted with an empty input, an error will be returned. However, I am experiencing issues in checking the states within the finally b ...