Giter Site home page Giter Site logo

Set width in percentage about table HOT 26 CLOSED

BenMGilman avatar BenMGilman commented on August 18, 2024 18
Set width in percentage

from table.

Comments (26)

abhandaru avatar abhandaru commented on August 18, 2024 57

I think this is an important feature.
Feel free to narrow down the scope of the behavior.
For instance, it is not unreasonable to require that all widths be percentages when using fractions.
Admittedly this is a tradeoff, but hard-coding the width is definitely not ideal.

from table.

colomar avatar colomar commented on August 18, 2024 29

Given react-table is using flexbox, wouldn't it make sense to allow setting the flex-grow and flex-shrink properties of each column instead of percentage widths? That should give you similar results if I understand flexbox correctly.

from table.

evilPaprika avatar evilPaprika commented on August 18, 2024 14

How to do that properly in 2024?

from table.

neocybereth avatar neocybereth commented on August 18, 2024 10

a solution to this could be something as simple as:

This would give your particular column a screen width of 20% ; not very dynamic though.
width: Math.round(window.innerWidth * 0.2)

from table.

yangfei4913438 avatar yangfei4913438 commented on August 18, 2024 8

@sean-magin

a solution to this could be something as simple as:

This would give your particular column a screen width of 20% ; not very dynamic though. width: Math.round(window.innerWidth * 0.2)

This is very useful, we can add a global event listener and just regenerate the data when the window size changes.

 const getColumns = () => {
    return [
      {
        Header: 'Name',
        accessor: 'name',
        id: 'name',
        width: Math.round((window.innerWidth - 55) * 0.2),
        disableFilters: true,
      },
      {
        Header: 'Age',
        accessor: 'age',
        id: 'age',
        width: Math.round((window.innerWidth - 55) * 0.2),
        disableFilters: true,
      },
      {
        Header: 'Visits',
        accessor: 'visits',
        id: 'visits',
        width: Math.round((window.innerWidth - 55) * 0.2),
        disableFilters: true,
      },
      {
        Header: 'Last Visit',
        accessor: 'last_visit',
        id: 'last_visit',
        width: Math.round((window.innerWidth - 55) * 0.2),
        disableFilters: true,
      },
      {
        Header: 'Status',
        accessor: 'status',
        id: 'status',
        width: Math.round((window.innerWidth - 55) * 0.2),
        disableFilters: false,
      },
    ];
  };

  const [columns, setColumns] = useState(() => getColumns());

  useEffect(() => {
    const resizeCols = () => {
      setColumns(() => getColumns());
    };
    window.addEventListener('resize', resizeCols);
    return () => {
      window.removeEventListener('resize', resizeCols);
    };
  }, []);

from table.

yniknafs avatar yniknafs commented on August 18, 2024 6

I'm really surprised this does not exist. Seems like a really useful / standard feature for a table. Hard-coding widths gives basically 0 flexibility for window resizing.

from table.

samueldjones24 avatar samueldjones24 commented on August 18, 2024 5

@colomar We managed to use flex-grow in this case and it does the trick:

CSS:

.react-table-column-flex-grow-1 {
  flex-grow: 1 !important;
  width: unset !important;
  flex-basis: 5px !important;
  max-width: none !important;
}

JS

getTdProps={( state, rowInfo, column, instance) => 
({ className: `react-table-column-flex-grow-${column.flexGrow}` })}

Also, don't forget to set a flexGrow property in the relevant column(s):

Header: "Reference"
accessor: "refId"
flexGrow: 1
width: 100 // you could also play with the width value 

from table.

therohitjha avatar therohitjha commented on August 18, 2024 4

a solution to this could be something as simple as:

This would give your particular column a screen width of 20% ; not very dynamic though.
width: Math.round(window.innerWidth * 0.2)

you can do like this too,just pass variable value as decimal value like 20 which means 20%.check the example with image.

width: Math.round(window.innerWidth * (value/100))

Capture

from table.

saiemsaeed avatar saiemsaeed commented on August 18, 2024 2

@tannerlinsley any update for using percentage values on minWidth, maxWidth and width?

from table.

tannerlinsley avatar tannerlinsley commented on August 18, 2024 1

from table.

oshiniG avatar oshiniG commented on August 18, 2024 1

any update for using percentage values related to width?

from table.

tannerlinsley avatar tannerlinsley commented on August 18, 2024

This is a great question. I'm trying to think how that would work with flexbox right now. Would you expect the percentage to be relative to the width of the table viewport? I'll have to experiment with this :)

from table.

BenMGilman avatar BenMGilman commented on August 18, 2024

Yes, exactly! What I really need is to set relative scaling. So, if I have 4 columns, and I want the 1st column to be twice the size of the others, the flex properties would be 40, 20, 20, 20, respectively, rather than all 25.

from table.

BenMGilman avatar BenMGilman commented on August 18, 2024

(sorry didn't mean to actually close this)

from table.

tannerlinsley avatar tannerlinsley commented on August 18, 2024

Still working on this. I did just add support for all pixel widths though. minWidth, width, and maxWidth

from table.

tannerlinsley avatar tannerlinsley commented on August 18, 2024

the more I'm getting into this, I'm not sure if it's going to work exactly how I was thinking. Especially when mixing percentage widths with min/max widths and static widths. I'm going to keep working on it, but just wanted to report back on the difficulty ;)

from table.

BenMGilman avatar BenMGilman commented on August 18, 2024

Thanks for the help! We could overwrite inline styles with css and !important but would prefer if we didn't have to go through all that

from table.

tannerlinsley avatar tannerlinsley commented on August 18, 2024

Absolutely. I'm with ya. I'll give it a fresh look this week and see if I can't achieve it another way ;)

from table.

tannerlinsley avatar tannerlinsley commented on August 18, 2024

Closing this one for now. Multiple attempts have been made and nothing seems to get the job done correctly. I'll keep trying as the code matures.

from table.

chin2km avatar chin2km commented on August 18, 2024

Any update on this yet?

from table.

annie-l avatar annie-l commented on August 18, 2024

or could you do something like colspan to support flexibility?

from table.

yniknafs avatar yniknafs commented on August 18, 2024

@Joaquinox Thanks for this. looks great.

to clarify - in your code above. flexGrow: 1 turns it on? Where are you actually setting the percentage?

from table.

samueldjones24 avatar samueldjones24 commented on August 18, 2024

@yniknafs Apologies, this is a workaround to setting a percentage. We are simply setting the flexGrow property as per docs.

from table.

adamscybot avatar adamscybot commented on August 18, 2024

I have a very hacky plugin hook to use alongside flex layout. When I say hacky, I mean hacky -- use with caution. It even works with resizable columns. Here it is: gist.github.com/adamscybot/65df3ce2b155c20c8b867786ef612e72

from table.

damianobarbati avatar damianobarbati commented on August 18, 2024

@evilPaprika how did you solve this?

from table.

DreierF avatar DreierF commented on August 18, 2024

I also needed columns to have a given fraction of the overall table and came up with the following solution:

https://gist.github.com/DreierF/0511d58d36c3ac2aaa0aa68807efb912

It can be plugged in as a feature into Tanstack Table v8 and allows to specify column sizes either as pixel values or fractions of the remaining table space. Would also be easy to extend this to percentage, but IMHO fraction is easier to deal with as you don't need to ensure that the values add up to 100%. Maybe this is a helpful starting point for others facing a similar challenge.

from table.

Related Issues (20)

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.