Giter Site home page Giter Site logo

fajarprayoga / react-laravel-datatable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chamamme/react-laravel-datatable

0.0 0.0 0.0 858 KB

A simple yet flexible react data-table library built to support laravel's pagination out of the box.

JavaScript 93.67% CSS 6.33%

react-laravel-datatable's Introduction

react-laravel-datatable

A simple yet flexible react datatable library built to support laravel's pagination out of the box.

Heads Up: This library is still fragile and infant so do yourself a favor an handle it with care ๐Ÿค“๐Ÿค—. Do not forget to log your issues so I can follow up on them ๐Ÿ‘จ๐Ÿฟโ€๐Ÿซ.

Installation

npm i react-laravel-datatable

Usage

  1. First import package.
import Datatable  from  'react-laravel-datatable'; 
  1. Define expected column details. Details must be an array of objects with key(id,label);
    const columns =[
        {
            id: "user_id", //This is the key from your data source. (Required)
            label : "ID", //Label for column title. (Required)
        },
        {
            id: "user_name", 
            label : "Name", 
        },
        {
            id: "user_email", 
            label : "Email", 
        }
    ]
  • Define your data source. Please note that your data source endpoint should be an http GET method which returns laravel pagination object as a response.
    const dataSource = "htts://test.com/users" ;

    /*
    A typical laravel pagination object will return the following.;
     {
         "data":{
             "current_page":1,
             "data":[
                 {
                     "id":33,
                     "name":"Andrew Chamamme",
                     "email":"[email protected]",
                     "created_at":"2019-05-29 10:42:24"
                 }
                 ],
            "first_page_url":"http://localhost:8000/api?page=1",
            "from":1,
            "last_page":2,
            "last_page_url":"http://localhost:8000/api?page=2",
            "next_page_url":"http://localhost:8000/api?page=2",
            "path":"http://localhost:8000/api","per_page":15,
            "prev_page_url":null,
            "to":15,
            "total":28
            }
         } 
      
     */

Though the package is tailored to laravel's pagination, you can still use with other frameworks or vanilla code provided your data source endpoint returns a response just as it is above ๐ŸŒš.

  1. Finally intialize the Datatable component.
    <Datatable url={dataSource} columns={columns} />

Advanced Options

The package gives you the flexibility to define your own callback functions on each cell as well as define you own action components or buttons.

Headers

The package accepts a headers prop. This can be used in situations where there is a need to modify request headers. Eg. setting Authorization token,Content type, etc.

const myToken = "ATOKENSTRINGHERE";

 const myHeaders =  {
      "Authorization" : `Bearer ${myToken}`,
      "Content-Type" : 'application/json',
  }


  <Datatable ... headers={myHeaders} />
  

Filter & Sort

The component already sends a couple of parameters in the query string when making request to the api endpoint. You can leverage on them to filter and sort results from the server side. A typical query string from this component will be ?term=&page=1&column=&order=asc&per_page=5 .

Param Description Example
term The search term entered in the search field hello world
page Current page number 1
column Column for sorting user_name
order Sorting order asc / desc
per_page Number of records per page 5

Action Buttons/Components

Action buttons or UI components can be added by indicating an ` actions ` prop in the `Datatable`. This prop  takes a function and your functnion can contain any valid react code but in this case its preferred to use it for button actions ๐Ÿ‘จ๐Ÿฟโ€๐Ÿซ . At the point of when this function is being called, the current row object in injected into it.
    const  actions = (rowItem)=>{
            //Below is just an example. You can decide to do whatever you want here.๐Ÿค“
           return ( <a  href={`/user/${rowItem.id}`}> Views </a>)
    }

    <Datatable ... actions={actions} />

Onclick Event on cells

Maybe you want to add an onClick event to records in a specific column (cell). You can easily do that by indicating an onClick property in the columns defination.
    const columns =[
        {
            id: "user_id",
            label : "ID", 
            onClick: (rowItem) => { 
                    //This is just an example. You can decide to do whatever you want here.๐Ÿค“
                    console.log(`User ${rowItem.id} has been clicked `); 
                    }
        }
        ...
    ];

CONTRIBUTIONS

All contributions and pull requests are welcome. Incase of any issue or suggestions please dont hesitate to log it on github.

TODOS

  • Add styling โœ…
  • Search field โœ…
  • Bulk action
  • Editable cell

react-laravel-datatable's People

Contributors

chamamme avatar

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.