Giter Site home page Giter Site logo

react-sortable-table's Introduction

Sortable Table in React.js

npm version

Feature

  • Simple API
  • Customizable style
  • Customizable sorting functions

This component is depends on Font Awesome
Please activate Font Awesome. Get started with Font Awesome
LICENSE of Font Awesome

Example

https://rudolph-miller.github.io/react-sortable-table

  • ID: default sorting
    • rendered as <a> tag.
  • NAME: custom sorting function that sort names by the family name
  • CLASS: unsortable

Install

npm install react-sortable-table

Usage

window.React = require('react');
import { render } from 'react-dom';
import React, { Component, PropTypes } from 'react';
import SortableTable from 'react-sortable-table';

function getFamilyName(name) {
  return name.split(' ').slice(-1)[0]
}

const FamilyNameSorter = {
desc: (data, key) => {
    var result = data.sort(function (_a, _b) {
      const a = getFamilyName(_a[key]);
      const b = getFamilyName(_b[key]);
      if ( a <= b ) {
        return 1;
      } else if ( a > b) {
        return -1;
      }
    });
    return result;
  },

  asc: (data, key) => {
    return data.sort(function (_a, _b) {
      const a = getFamilyName(_a[key]);
      const b = getFamilyName(_b[key]);
      if ( a >= b ) {
        return 1;
      } else if ( a < b) {
        return -1;
      }
    })
  }
};


class App extends Component {
  constructor() {
    super()
    this.state = {
      data: [
        { id: 3, name: 'Satoshi Yamamoto', class: 'B' },
        { id: 1, name: 'Taro Tanak', class: 'A' },
        { id: 2, name: 'Ken Asada', class: 'A' },
        { id: 4, name: 'Masaru Tokunaga', class: 'C' }
      ]
    };
  }

  render() {
    const columns = [
      {
        header: 'ID',
        key: 'id',
        defaultSorting: 'ASC',
        headerStyle: { fontSize: '15px', backgroundColor: '#FFDAB9', width: '100px' },
        dataStyle: { fontSize: '15px', backgroundColor: '#FFDAB9'},
        dataProps: { className: 'align-right' },
        render: (id) => { return <a href={'user/'+id}>{id}</a>; }
      },
      {
        header: 'NAME',
        key: 'name',
        headerStyle: { fontSize: '15px' },
        headerProps: { className: 'align-left' },
        descSortFunction: FamilyNameSorter.desc,
        ascSortFunction: FamilyNameSorter.asc
      },
      {
        header: 'CLASS',
        key: 'class',
        headerStyle: { fontSize: '15px' },
        sortable: false
      }
    ];

    const style = {
      backgroundColor: '#eee'
    };

    const iconStyle = {
      color: '#aaa',
      paddingLeft: '5px',
      paddingRight: '5px'
    };

    return (
      <SortableTable
        data={this.state.data}
        columns={columns}
        style={style}
        iconStyle={iconStyle} />
    );
  }
}

render(<App />, document.getElementById('app'));

PropTypes

  • data: React.PropTypes.array.isRequired
  • columns: React.PropTypes.array.isRequired

Copyright

Copyright (c) 2015 Rudolph-Miller ([email protected])

#License

Licensed under the MIT License.

react-sortable-table's People

Contributors

np avatar oebilgen avatar rudolph-miller 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

Watchers

 avatar  avatar

react-sortable-table's Issues

Custom class for th/td

While there is a way to customize the style using dataStyle and headerStyle there is no way to customize the class.

I'm proposing to either add dataClass and headerClass, or something more general such as dataProps and headerProps.

Unable to install

This is the output I receive:

Abrams-MacBook-Pro:SomeRepo abejfehr$ npm install react-sortable-table
npm WARN deprecated [email protected]: react-tools is deprecated. For more information, visit https://fb.me/react-tools-deprecated
npm WARN prefer global [email protected] should be installed with -g

> [email protected] install /some/dir/node_modules/react-sortable-table
> node_modules/react-tools/bin/jsx --extension jsx --no-cache-dir src/ js/

sh: node_modules/react-tools/bin/jsx: No such file or directory
- acorn-globals node_modules/formsy-react/node_modules/acorn-globals
- escodegen node_modules/formsy-react/node_modules/escodegen
- fsevents node_modules/formsy-react/node_modules/fsevents
- node-pre-gyp node_modules/formsy-react/node_modules/fsevents/node_modules/node-pre-gyp
- tar-pack node_modules/formsy-react/node_modules/fsevents/node_modules/tar-pack
- jsdom node_modules/formsy-react/node_modules/jsdom
- jstransform node_modules/formsy-react/node_modules/jstransform
- regexpu node_modules/formsy-react/node_modules/regexpu
- webpack node_modules/formsy-react/node_modules/webpack
/some/dir
├── [email protected] 
├── [email protected] 
├── [email protected]  extraneous
└── [email protected] 

npm WARN [email protected] requires a peer of vue-hot-reload-api@^1.2.0 but none was installed.
npm WARN [email protected] requires a peer of vueify-insert-css@^1.0.0 but none was installed.
npm ERR! Darwin 15.2.0
npm ERR! argv "/usr/local/Cellar/node/5.3.0/bin/node" "/usr/local/bin/npm" "install" "react-sortable-table"
npm ERR! node v5.3.0
npm ERR! npm  v3.5.3
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn

npm ERR! [email protected] install: `node_modules/react-tools/bin/jsx --extension jsx --no-cache-dir src/ js/`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the [email protected] install script 'node_modules/react-tools/bin/jsx --extension jsx --no-cache-dir src/ js/'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the react-sortable-table package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node_modules/react-tools/bin/jsx --extension jsx --no-cache-dir src/ js/
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs react-sortable-table
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls react-sortable-table
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /some/dir/npm-debug.log

Customizable output

My thead always stays on top, while tbody have a maximum height and can be scrolled.
This is accomplished using extra containers, so my average structure looks like:

<div class="table">
  <div class="table-head">
    <table="table-head-inner">
      <tr>
        <th></th>
        <td></td>
        <td></td>
      </tr>
    </table>
  </div>
  <div class="table-body">
    <table="table-body-inner">
      <tr>
        <th></th>
        <td></td>
        <td></td>
      </tr>
    </table>
  </div>
</div>

Please make your plugin's default HTML customizable to the alike output.

Multiple keys in the column object

Is there a way to implement support of multiple keys?

the scenario I am after is to render function to take multiple parameters, e,g Id1 and Id2 and not just the key

Thanks
Ana

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.