Giter Club home page Giter Club logo

react-filterable-table's Introduction

Note

I'm no longer actively working on this, but please feel free to fork it.

react-filterable-table

Extendable table with filtering, sorting, paging, and more. Hold down shift to sort on multiple fields.

Demo

NPM

Install

npm install react-filterable-table

Basic usage:

const FilterableTable = require('react-filterable-table');

// Data for the table to display; can be anything
const data = [
	{ name: "Steve", age: 27, job: "Sandwich Eater" },
	{ name: "Gary", age: 35, job: "Falafeler" },
	{ name: "Greg", age: 24, job: "Jelly Bean Juggler" },
	{ name: "Jeb", age: 39, job: "Burrito Racer" },
	{ name: "Jeff", age: 48, job: "Hot Dog Wrangler" }
];

// Fields to show in the table, and what object properties in the data they bind to
const fields = [
	{ name: 'name', displayName: "Name", inputFilterable: true, sortable: true },
	{ name: 'age', displayName: "Age", inputFilterable: true, exactFilterable: true, sortable: true },
	{ name: 'job', displayName: "Occupation", inputFilterable: true, exactFilterable: true, sortable: true }
];

<FilterableTable
	namespace="People"
	initialSort="name"
	data={data}
	fields={fields}
	noRecordsMessage="There are no people to display"
	noFilteredRecordsMessage="No people match your filters!"
/>

Props

There are a lot, but most are just for customization. The minimum you need to get running are data and fields
  • data - array - Static data to bind to
  • fields - array - Array of fields (see below) used for building the table. These fields have their own list of props detailed below
  • dataEndpoint - string - If not using a static dataset, this can be used to fetch data with AJAX
  • onDataReceived - fn - This is called (passing the array of data) before the data is rendered. Any necessary data transformations (date parsing, etc) can be done here
  • className - string - Class name to apply to the component's root <div> element
  • tableClassName - string - Class name to apply to the component's <table> element
  • trClassName - string or fn - Class name to apply to the <tr> elements. If a function is passed, it's called with the record and index as parameters: function (record, index)
  • footerTrClassName - string - Class name of the footer's <tr> element
  • iconSortAsc - object - Element to use for the asc sort icon next to a field name
  • iconSort - object - Element to use for the default sort icon next to a field name. If not provided, the default uses icons from FontAwesome.
  • iconSortedDesc - object - Element to use for the desc sort icon next to a field name. If not provided, the default uses icons from FontAwesome.
  • iconSortedDesc - object - Element to use for the desc sort icon next to a field name. If not provided, the default uses icons from FontAwesome.
  • initialSort - string - The field name on which to sort on initially
  • initialSortDir - bool - The sort direction to use initially - true is ascending, false is descending. Default: true
  • initialFieldFilters - array - Array of fieldFilter objects to start with. fieldFilter object: { fieldname{string}, value{any}, exact{bool} }
  • initialExactFilters - array - Array of exactFilter objects to start with. exactFilter object: { fieldname{string}, value{any}, name{string} }
  • noRecordsMessage - string - Message to show when there are no records
  • noFilteredRecordsMessage - string - Message to show when the user has applied filters which result in no records to show
  • serverErrorMessage - string or object - Message to show when an error is encountered from the dataEndpoint (if used). Can be a string or a React component
  • loadingMessage - string or object - Message to show when the component is loading data
  • recordCountName - string - Verbiage to use at the top where it says "X results". For example, "1 giraffe"
  • recordCountNamePlural - string - Verbiage to use when there are more than 1 results (or 0). For example, "3 giraffes"
  • headerVisible - bool - Whether or not to show the header
  • pagersVisible - bool - Whether or not to show the pagers
  • topPagerVisible - bool - Whether or not to show the top pager
  • bottomPagerVisible - bool - Whether or not to show the bottom pager
  • pageSize - int - Page size (default: 10)
  • pageSizes - Array - Array of numbers to use for the page size dropdown. Default is [10, 20, 30, 50]. Set to null to hide the page size dropdown
  • autofocusFilter - bool - Set to true to focus the filter text box when the component loads
  • pagerTitles - object - Specify the titles for the pager buttons. E.g., pagerTitles={{ first: '<|', last: '>|' }}
  • pagerTopClassName - string - Specify the className for the top pager
  • pagerBottomClassName - string - Specify the className for the bottom pager
  • namespace - string - The app saves settings (currently only page size) to localStorage. Namespace prevents overriding settings from other pages/apps where this is used. Use the same namespace across implementations that should share the settings. Default: 'react-filterable-table'
  • onFilterRemoved - fn - Callback for when a filter is removed by clicking the 'x' next to it (or when called manually, e.g., this.refs.FilterableTable.removeExactFilter). Function is called with the filter object that was removed, as well as the event that fired it

field Props

  • name - string - Name of the property on the data object
  • displayName - string - Field name as it will appear in the table header. If ommitted, name is used
  • sortFieldName - string - Field to use when sorting if you want to sort using a different value from what's displayed. For example, A+, A, B, C would normally sort as A, A+, B, C. You could have a separate field that maps those values to an integer, then use that field for sorting
  • inputFilterable - bool - Whether or not this field should be filtered when the user types in the Filter text box at the top
  • exactFilterable - bool - Whether or not the user can click the field's value to filter on it exactly
  • sortable - bool - Whether or not the user can sort on this field
  • visible - bool - Whether or not the field is visible
  • thClassName - string - Class name of the <th> element
  • tdClassName - string or fn - Class name of the <td> element. If a function is passed, it's called with the same parameters as render (see below)
  • footerTdClassName - string - Class name of the footer's <td> element
  • emptyDisplay - string - Text to show when the field is empty, for example "---" or "Not Set"
  • render - fn - Function called to render the field. Function is passed a props object which contains: props.value - the value of the field from the data object, and props.field - this field object (Demo using field render functions)
  • footerValue - fn or string - Value for the footer cell. Can be a render function (for totaling, etc) or a static value. Tip: render functions receive both filteredRecords (your data, filtered if any filters are applied) and records (non-filtered data) objects in the props parameter. You can use these to show totals for filtered or unfiltered data

Example using a render function

const renderAge = (props) => {
	/*
	 * This props object looks like this:
	 * {
	 *   value:  (value of the field in the data. In this case, it's the person's age.),
	 *   record: (the data record for the whole row, in this case it'd be: { name: "Steve", age: 27, job: "Sandwich Eater" }),
	 *   field:  (the same field object that this render function was passed into. We'll have access to any props on it, including that 'someRandomProp' one we put on there. Those can be functions, too, so we can add custom onClick handlers to our return value)
	 * }
	 */

	// If they are over 60, use the "blind" icon, otherwise use a motorcycle
	const iconClassName = "fa fa-" + (props.value > 60 ? "blind" : "motorcycle");
	const personName = props.record.name;

	return (
		<span title={personName + "'s Age"}>
			{props.value} <span className={iconClassName}></span>
		</span>
	);
};


const data = [
	...
	{ name: "Steve", age: 27, job: "Sandwich Eater" },
	...
];

const fields = [
	...
	{ name: 'age', displayName: "Age", inputFilterable: true, exactFilterable: true, sortable: true, someRandomProp: "Tacos!", render: renderAge },
	...
]

The render function gets a few other props as well which may be useful. For example:

  • records - Your data array
  • recordIndex - This record's index
  • filteredRecords - The current array of records that the table is showing (if there are any filters applied, this will be the filtered items)
  • addExactFilter - function to add an exact filter on something. Use it in an onClick to filter on whatever you want.
    • Usage: addExactFilter(value, fieldname, name = fieldname)
  • Various other internal props which may or may not be useful. Check them out using console.log(props) in a render function to see what else is available. These internal props could potentially change with updates, so use at your own risk.

Building

To build the main library: gulp build

To build the example: gulp example

react-filterable-table's People

Contributors

ianwitherow avatar ianwitherow-state avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-filterable-table's Issues

Table with buttons/url

Is there some way to use buttons with onclick events in the table rows? Or have a URL in the table row content?

Is it possible to make a row clickable?

Hi,
Great plugin.
What i want to know is, is it possible to make a row clickable? (not a cell clickable)
I know we can use render in field to make it return something. Can the same done for a row?

Inline styles

Why not make the styles inline so I we don't have to worry about another external file?

More customization

  • Allow more customization of header/footer layouts
  • Take out Bootstrap classes - don't presume upon styling

How to sort by date

How to Sort By Date

Do you have any suggestion about sorting by date, currently it is sorting by string.

Thanks,

removeExactFilter

there is currently no way to programmatically remove a filter... can this be added?

Access is denied error in Incognito Mode

react-dom.production.min.js:282 Uncaught DOMException: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.
    at new t (https://76ea9c66fca04290.cdn2.monday.app/static/js/main.e776acc7.js:2:257781)
    at mi (https://76ea9c66fca04290.cdn2.monday.app/static/js/main.e776acc7.js:2:178399)
    at Tl (https://76ea9c66fca04290.cdn2.monday.app/static/js/main.e776acc7.js:2:210496)
    at xs (https://76ea9c66fca04290.cdn2.monday.app/static/js/main.e776acc7.js:2:247763)
    at yu (https://76ea9c66fca04290.cdn2.monday.app/static/js/main.e776acc7.js:2:235694)
    at vu (https://76ea9c66fca04290.cdn2.monday.app/static/js/main.e776acc7.js:2:235622)
    at mu (https://76ea9c66fca04290.cdn2.monday.app/static/js/main.e776acc7.js:2:235485)
    at ou (https://76ea9c66fca04290.cdn2.monday.app/static/js/main.e776acc7.js:2:232269)
    at ru (https://76ea9c66fca04290.cdn2.monday.app/static/js/main.e776acc7.js:2:230820)
    at k (https://76ea9c66fca04290.cdn2.monday.app/static/js/main.e776acc7.js:2:304735)

The app is used inside an iframe and this error is only thrown up in Incognito Mode

Please provide an example of sortFieldName

There's no example and the description is vague. I don't want my prices to sort like this:
1
10
11
12
2
3
4

I'd actually expect a function to get a sort value, similar to render(), but that's not what you have. How does sortFieldName work? Where's a working example?

Allow passing in exactFilters and filters in props

Hello,

Thanks for creating a really neat table component.

I would like to be able to have some custom buttons to allow my users to apply a couple of filters as well as a sort order to the table. I.e. all records with "active" status, sorted by date.

Could it be enabled so that I can pass in initialExactFilters and initialFilter as well as the existing initialSort prop?

Thanks.

Pagination titles

Hello,
Is there any option to set titles of pagination ? I mean "Last", "First"

CSS issue

For some unknown reasons, the CSS is not included in my project. Could you please confirm the exact steps to take to use your module with create-react-app.

sorting with rendered radio inputs doesn't display correctly

This is an awesome component. But I'm having an issue when trying to use the render function to display radio inputs.

I have fields including three radio buttons that all look something like this:

    {
      name: "yes",
      displayName: "Yes",
      sortable: true,
      render: ({ record, value }) => {
        return (
          <input
            type="radio"
            name={`submission-approved-${record.uid}`}
            value="yes"
            checked={value}
            onChange={e => handleChange(e, record.uid)}
          />
        );
      }
    }

On the first render, everything is perfect. It's only after trying to sort a column that the inputs start to ignore their checked attribute. clicking a radio triggers a rerender and all inputs show correctly again. I'm stumped...

Screen Shot 2019-11-27 at 1 47 54 PM

Link to element

Hello Is it possible to add a link (anchor tag) to names for example to I can navigate to another page. Thanks

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.