Git Product home page Git Product logo

reactjs-simple-table's Introduction

React Simple Table

A simple and reusable table for React (Demo)

Server side table for React (Demo)

Installation

The package can be installed via NPM:

npm i micheg/reactjs-simple-table

Note

Fork of SinaMAlizadeh/reactjs-simple-table but with but with the possibility of having a customized rendering element. useful for links, strong, etc. ex:

function App() {
  const [list, setList] = useState([]);

  useEffect(() => {
     fetch(`https://jsonplaceholder.typicode.com/users`)
       .then((response) => response.json())
       .then((json) => setList(json));
  }, []);

  const myRender = (item, col) =>
  {
      if(col.field === 'email') return (
        <a href={`mailto:${item[col.field]}`}>{item[col.field]}</a>
      ); else if(col.field === 'name') return (
        <strong>{item[col.field]}</strong>
      ); else return (
        <span>{item[col.field]}</span>
      );
  };


  return (
    <div className="App">
      <SimpleTableComponent columns={columns} list={list} cellComponent={myRender}/>
    </div>
  );
}
export default App;

Usage

Import SimpleTableComponent for usage :

import SimpleTableComponent from "reactjs-simple-table";

Define your column with filed variable and header name :

const columns = [
  {
    field: "title",
    headerName: "Title",
  },
  {
    field: "number",
    headerName: "Amount",
  },
];

Get your list from Api or define your list :

const list = [
  { title: "Michael", number: 1 },
  { title: "Lindsay", number: 10 },
  { title: "Tobias", number: 6 },
  { title: "Byron", number: 3 },
  { title: "George", number: 1 },
  { title: "Rachel", number: 10 },
  { title: "Lawson", number: 6 },
  { title: "Ferguson", number: 3 },
  { title: "Funke", number: 1 },
];

The most basic use of the simple table can be described with:

<SimpleTableComponent columns={columns} list={list} />
import SimpleTableComponent from "reactjs-simple-table";

const columns = [
  {
    field: "title",
    headerName: "Title",
  },
  {
    field: "number",
    headerName: "Amount",
  },
];

function App() {
  const list = [
    { title: "Michael", number: 1 },
    { title: "Lindsay", number: 10 },
    { title: "Tobias", number: 6 },
    { title: "Byron", number: 3 },
    { title: "George", number: 1 },
    { title: "Rachel", number: 10 },
    { title: "Lawson", number: 6 },
    { title: "Ferguson", number: 3 },
    { title: "Funke", number: 1 },
  ];
  return (
    <div className="App">
      <SimpleTableComponent columns={columns} list={list} />
    </div>
  );
}

export default App;

Server Side Table

Import SimpleTableComponent for usage :

import { ServerSimpleTableComponent } from "reactjs-simple-table";

For server side , define function for get table pagation , sorting and number of items in per page parameters then update table by new data. each time you change any table feature like pagation,... onGetData will call and you can use table parameters :

import React, { useState } from "react";
import "./styles.css";
import { ServerSimpleTableComponent } from "reactjs-simple-table";

const columns = [
  {
    field: "id",
    headerName: "id",
  },
  {
    field: "name",
    headerName: "name",
  },
  {
    field: "username",
    headerName: "username",
  },
  {
    field: "email",
    headerName: "email",
  },
];

export default function App() {
  const [list, setList] = useState([]);

  //function fo get table parameters
  const tableData = (item) => {
    //fetch data by filter parameters from table
    //item :
    // {
    // page : 1 ,
    // numberPerPage : 10 ,
    // order :  "ascending" or "descending",
    //orderby : "title"  *name of the field ,
    //}
    fetch(
      `https://yourServerUrl/users?page=${item.page}&pageSize=${item.numberPerPage}`
    ).then((response) => setList(json));
  };

  return (
    <div className="App">
      <ServerSimpleTableComponent
        columns={columns}
        list={list}
        onGetData={tableData}
        total={10}
        serverSideFiltering={false}
      />
    </div>
  );
}

User guide

Prop name Description Default value Example values
total Total of list , for using sever side table is required list.length() 100
numberPerPage number of row in each page 10 5 , 10 , 20 , 50 , 100
isRtl for support rtl language false true or false
numberPageOfText for change 'of' text in pagation 'of' 'from' or 'از'
tableClassName you can use your own class for table style , in this case you can add boostrap or other css file in your app and use table class name and change table style 'myTable' 'table table-bordered'
serverSideFiltering you can handle server side sorting by receive 'order' and 'orderby' parameters from changes of table features , if you want sorting one page data you can set serverSideFiltering to 'true' and sorting data without server call false true or false

Using Bootsrap

For using boostrap table class you can add boostrap css to Index.js or App.js , then add tableClassName property in SimpleTableComponent and use your table class :

npm i bootstrap

import SimpleTableComponent from "reactjs-simple-table";
import "bootstrap/dist/css/bootstrap.min.css";

const columns = [
  {
    field: "title",
    headerName: "Title",
  },
  {
    field: "number",
    headerName: "Amount",
  },
];

function App() {
  const list = [
    { title: "Michael", number: 1 },
    { title: "Lindsay", number: 10 },
    { title: "Tobias", number: 6 },
    { title: "Byron", number: 3 },
    { title: "George", number: 1 },
    { title: "Rachel", number: 10 },
    { title: "Lawson", number: 6 },
    { title: "Ferguson", number: 3 },
    { title: "Funke", number: 1 },
  ];
  return (
    <div className="App">
      <SimpleTableComponent
        columns={columns}
        list={list}
        tableClassName={"table table-bordered"}
      />
    </div>
  );
}

export default App;

You can use table-responsive :

<div class="table-responsive">
  <SimpleTableComponent
    columns={columns}
    list={list}
    tableClassName={"table table-bordered"}
  >
    ...
  </SimpleTableComponent>
</div>

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.