digidem / react-dimensions

[Looking for maintainers]

Home Page:http://lab.digital-democracy.org/react-dimensions/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how do you actually use this with fix data table?

craigcosmo opened this issue · comments

given this code, how would you make the table responsive?

import React from 'react'
import {Table, Column, Cell} from 'fixed-data-table';
export default class Data extends React.Component {

    render() {
        return (
            <div style={{width:'100%'}}>
                <Table
                    rowHeight={50}
                    rowsCount={10}
                    width={600}
                    height={500}
                    headerHeight={30}>
                    <Column
                    header={<Cell>First Name</Cell>}
                    cell={<Cell>mama</Cell>}
                    fixed={true}
                    width={100}
                    />
                    <Column
                    header={<Cell>Sentence! (flexGrow greediness=2)</Cell>}
                    cell={<Cell>chacha</Cell>}
                    flexGrow={2}
                    width={200}
                    />
                    <Column
                    header={<Cell>Company (flexGrow greediness=1)</Cell>}
                    cell={<Cell>nana</Cell>}
                    flexGrow={1}
                    width={200}
                    />
                    <Column
                    width={100}
                    header={<Cell>Last Name</Cell>}
                    cell={<Cell>sasa</Cell>}
                    />
                </Table>
            </div>
        )
    }
}

Since I was invited to in #1 , I'd suggest you use react-dimensions just as the example, making sure that the container of your table has a defined height/width before the component is mounted. If you use "%" height/width, make sure the parent has a defined size, a.s.o.

Try something like this (I haven't tested this code). This component would need to be within a <div> that is already styled to have a width and height, e.g. 100% width and height. Check your layout without rendering this component first: Use the Chrome inspector to ensure that your CSS is creating a div that is the size you want, then this will fill that div with the table.

import React from 'react'
import {Table, Column, Cell} from 'fixed-data-table';
import Dimensions from 'react-dimensions';

class Data extends React.Component {
  render() {
    return (
      <Table
        rowHeight={50}
        rowsCount={10}
        width={this.props.containerWidth}
        height={this.props.containerHeight}
        headerHeight={30}>
        <Column
          header={<Cell>First Name</Cell>}
          cell={<Cell>mama</Cell>}
          fixed={true}
          width={100}
          />
        <Column
          header={<Cell>Sentence! (flexGrow greediness=2)</Cell>}
          cell={<Cell>chacha</Cell>}
          flexGrow={2}
          width={200}
          />
        <Column
          header={<Cell>Company (flexGrow greediness=1)</Cell>}
          cell={<Cell>nana</Cell>}
          flexGrow={1}
          width={200}
          />
        <Column
          width={100}
          header={<Cell>Last Name</Cell>}
          cell={<Cell>sasa</Cell>}
          />
      </Table>
    )
  }
}

export default Dimensions()(Data)

So here is all i got

file data.js

import React from 'react'
import {Table, Column, Cell} from 'fixed-data-table';
import Dimensions from 'react-dimensions';

class Data extends React.Component {
  render() {
    return (
      <Table
        rowHeight={50}
        rowsCount={10}
        width={this.props.containerWidth}
        height={this.props.containerHeight}
        headerHeight={30}>
        <Column
          header={<Cell>First Name</Cell>}
          cell={<Cell>mama</Cell>}
          fixed={true}
          width={100}
          />
        <Column
          header={<Cell>Sentence! (flexGrow greediness=2)</Cell>}
          cell={<Cell>chacha</Cell>}
          flexGrow={2}
          width={200}
          />
        <Column
          header={<Cell>Company (flexGrow greediness=1)</Cell>}
          cell={<Cell>nana</Cell>}
          flexGrow={1}
          width={200}
          />
        <Column
          width={100}
          header={<Cell>Last Name</Cell>}
          cell={<Cell>sasa</Cell>}
          />
      </Table>
    )
  }
}

export default Dimensions()(Data)

file home.js

import React from 'react'
import Data from 'Data'
export default class Home extends React.Component {
    render() {
        return (
            <div class="page">
                <div class="header"></div>
                <div class="content">
                    <Data />
                </div>
            </div>
        )
    }
}

Error: Wrapper div has no height or width, try overriding style with containerStyle option

This is most likely because your CSS is wrong. react-dimensions passes the width and height of the container div to the props. If your container div has no height or width, you get 0. Read up on setting height: 100% in CSS and post anything useful here - lots of people get this problem and it would be good to have a reference. Quick check: remove the <Data /> component completely, set a background color on .content and take a look. If the div has no height, then no height can be passed down with react-dimentions.

    render() {
        return (
            <div class="page">
                <div class="header"></div>
                <div class="content" style={{backgroundColor: 'blue'}}>
                </div>
            </div>
        )
    }

I try, and here's the result

screen shot 2016-07-19 at 1 17 09 am

css

.page{
display:flex;
flex-direction: column;
}
.content{
flex:1
}

I think that with flex designs you need to specify Dimensions(containerStyle={flex: 1})(Data). Maybe also elementResize: true ?

@jdelafon that will give error: Uncaught ReferenceError: containerStyle is not defined

Dimensions({containerStyle: {flex: 1}})(Data)

Sorry about the mess above. I just want clarify what finally works. This code actually work. The table extends to width and height of the wrapper (.content). And the table resizes when window resize. However, react-dimension still gives warning Wrapper div has no height or width, try overriding style with containerStyle option

file data.js

import React from 'react'
import {Table, Column, Cell} from 'fixed-data-table';
import Dimensions from 'react-dimensions';

class Data extends React.Component {
  render() {
    return (
      <Table
        rowHeight={50}
        rowsCount={10}
        width={this.props.containerWidth}
        height={this.props.containerHeight}
        headerHeight={30}>
        <Column
          header={<Cell>First Name</Cell>}
          cell={<Cell>mama</Cell>}
          fixed={true}
          width={100}
          />
        <Column
          header={<Cell>Sentence! (flexGrow greediness=2)</Cell>}
          cell={<Cell>chacha</Cell>}
          flexGrow={2}
          width={200}
          />
        <Column
          header={<Cell>Company (flexGrow greediness=1)</Cell>}
          cell={<Cell>nana</Cell>}
          flexGrow={1}
          width={200}
          />
        <Column
          width={100}
          header={<Cell>Last Name</Cell>}
          cell={<Cell>sasa</Cell>}
          />
      </Table>
    )
  }
}

export default Dimensions()(Data)

file home.js

import React from 'react'
import Data from 'Data'
export default class Home extends React.Component {
    render() {
        return (
            <div class="page">
                <div class="header"></div>
                <div class="content">
                    <Data />
                </div>
            </div>
        )
    }
}

css

.page{
display:flex;
flex-direction: column;
}
.content{
flex:1
}