0

I am working the react-data-grid library to create an filterable datatable in react. All of their examples use the depreciated React.createClass method, and I am trying to refactor to the ES6 Class Components.

Specifically, I am trying to refactor the Filterable Grid example:

My refactored code looks like this:

import React from 'react'
import ReactDataGrid from 'react-data-grid'
const { Toolbar, Data: { Selectors } } = require('react-data-grid-addons')

class FilterableTable extends React.Component {
  constructor(props) {
    super(props);

    this._columns = [
      {
        key: 'id',
        name: 'ID',
        width: 80
      },
      {
        key: 'task',
        name: 'Title',
        editable: true
      },
      {
        key: 'priority',
        name: 'Priority',
        editable: true
      },
      {
        key: 'issueType',
        name: 'Issue Type',
        editable: true
      },
      {
        key: 'complete',
        name: '% Complete',
        editable: true
      },
      {
        key: 'startDate',
        name: 'Start Date',
        editable: true
      },
      {
        key: 'completeDate',
        name: 'Expected Complete',
        editable: true
      }
    ];

    this.state = { rows: this.createRows(1001), filters: {} };
    console.log(this.state);
  }


  getRandomDate = (start, end) => {
    return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())).toLocaleDateString();
  }

  createRows = () => {
    let rows = [];
    for (let i = 1; i < 1000; i++) {
      rows.push({
        id: i,
        task: 'Task ' + i,
        complete: Math.min(100, Math.round(Math.random() * 110)),
        priority: ['Critical', 'High', 'Medium', 'Low'][Math.floor((Math.random() * 3) + 1)],
        issueType: ['Bug', 'Improvement', 'Epic', 'Story'][Math.floor((Math.random() * 3) + 1)],
        startDate: this.getRandomDate(new Date(2015, 3, 1), new Date()),
        completeDate: this.getRandomDate(new Date(), new Date(2016, 0, 1))
      });
    }

    return rows;
  }

  getRows = () => {
    return Selectors.getRows(this.state);
  }

  getSize = () => {
    return this.getRows().length;
  }

  rowGetter = ( rowIdx ) => {
    let rows = this.getRows();
    return rows[rowIdx];
  }

  handleFilterChange = ({ filter }) => {
    let newFilters = Object.assign({}, this.state.filters);
    if (filter.filterTerm) {
      newFilters[filter.column.key] = filter;
    } else {
      delete newFilters[filter.column.key];
    }
    this.setState({ filters: newFilters });
  }

  onClearFilters = () => {
    // all filters removed
    this.setState({filters: {} });
  }

  render() {
    return (
      <ReactDataGrid
        columns={this._columns}
        rowGetter={this.rowGetter}
        enableCellSelect={true}
        rowsCount={this.getSize()}
        minHeight={800}
        toolbar={<Toolbar enableFilter={true}/>}
        onAddFilter={this.handleFilterChange}
        onClearFilters={this.onClearFilters} />);
  }

}

export default FilterableTable

Issue:

An issue arises when I click the filter button - a new header row is rendered (via the Toolbar component), but there is no input field. This screenshot shows the two examples side by side - my ES6 version on top and the createClass version on the bottom: screenshot comparison

I am not sure what is causing this, but have a feeling it might be due to the way I am importing Toolbar ? Any help or a point in the right direction would be greatly appreciated ! (As well as any other suggestions re refactoring this component.)

1 Answer 1

1

To enable filtering for a given column, you need to set filterable=true for that column. So, add filterable:true to each object in this._columns. For more info, check http://adazzle.github.io/react-data-grid/examples.html#/filterable-grid.

this._columns = [
  {
    key: 'id',
    name: 'ID',
    width: 80
  },
  {
    key: 'task',
    name: 'Title',
    editable: true,
    filterable:true
  },
  {
    key: 'priority',
    name: 'Priority',
    editable: true,
    filterable:true
  },
  {
    key: 'issueType',
    name: 'Issue Type',
    editable: true,
    filterable:true
  },
  {
    key: 'complete',
    name: '% Complete',
    editable: true,
    filterable:true
  },
  {
    key: 'startDate',
    name: 'Start Date',
    editable: true,
    filterable:true
  },
  {
    key: 'completeDate',
    name: 'Expected Complete',
    editable: true,
    filterable:true
  }
];
Sign up to request clarification or add additional context in comments.

3 Comments

Yikes. Can't believe I missed that in my example, but appreciate the help
I'm glad I was able to help
Indeed. I am glad as well. Sometimes the answers as so obvious... that you can't even see them... lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.