0.1.3 • Published 4 years ago
laravel-react-datatable v0.1.3
Laravel-react-datatable
work in progress ...
Installation
npm i laravel-react-datatable
usage
import React from "react";
import "./App.css";
import LaraTable from "laravel-react-datatable";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
headers: [
{ title: "#", accessor: "id", index: 1, dataType: "number" },
{
title: "First Name",
accessor: "first_name",
width: "200",
index: 2,
searchable: true,
dataType: "string",
},
{
title: "Last Name",
searchable: true,
accessor: (d) => d.last_name,
width: "300",
index: 3,
dataType: "function",
},
{
title: "username",
// accessor: "username",
accessor: (d) => (
<span>
name <i>{d.username}</i>
<a href="#"> link</a>
</span>
),
width: "300",
index: 5,
searchable: true,
dataType: "function",
},
{
title: "rate",
searchable: true,
accessor: "doctor.average",
width: "300",
index: 6,
},
{
title: "Rating",
accessor: "doctor.avg_rate",
searchable: false,
index: 7,
width: "200",
cell: (row) => {
return (
<div className="rating">
<div
style={{
backgroundColor: "lightskyblue",
textAlign: "center",
height: "1.9em",
width: (row.doctor.avg_rate / 5) * 201 + "px",
margin: "3px 0 4px 0",
}}
>
<a href={`/showchart/${row.id}`}>{row.doctor.avg_rate}</a>
</div>
</div>
);
},
},
],
data: [],
current_page: 1,
last_page_url: null,
next_page_url: null,
per_page: 5,
totalRecords: 5,
from: null,
to: null,
};
}
search = async (pageNumber = 1) => {
let baseApiURL = "http://localhost:8000/api";
try {
let searchURL = `${baseApiURL}/search?page=${pageNumber}`;
const response = await fetch(searchURL, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"X-Requested-With": "XMLHttpRequest",
},
});
const data = await response.json();
if (data.error === false) {
return data.data;
} else {
return {};
}
} catch (err) {
console.error("error", err);
}
};
async componentDidMount() {
let res = await this.search();
this.setState({
data: res.data,
current_page: res.current_page,
last_page_url: res.last_page_url,
next_page_url: res.next_page_url,
per_page: res.per_page,
totalRecords: res.total,
from: res.from,
to: res.to,
});
}
/***
* Handle Page Change
*/
handlePageChange = async (pageNumber) => {
let res = await this.search(pageNumber);
this.setState({
data: res.data,
current_page: pageNumber,
});
};
render() {
const pagination = {
enabled: true,
itemsCountPerPage: this.state.per_page,
position: ["top left"],
};
return (
<div>
<LaraTable
keyField="id"
edit={true}
pagination={pagination}
width="100%"
headers={this.state.headers}
data={this.state.data}
noData="No records!"
currentPage={this.state.current_page}
totalRecords={this.state.totalRecords}
pageRangeDisplayed={5}
onPageChange={this.handlePageChange}
//searchable
/>
</div>
);
}
}
export default App;
Pagination Params
Name | Type | Default | Description |
---|---|---|---|
enabled | Boolean | true | Enable or Disable pagination |
position | Array | "bottom left", "top left" | Postion of pagination |
totalItemsCount | Number | Required. Total count of items which you are going to display | |
onChange | Function | Required. Page change handler. Receive pageNumber as arg | |
activePage | Number | 1 | Required. Active page |
itemsCountPerPage | Number | 10 | Count of items per page |
pageRangeDisplayed | Number | 5 | Range of pages in paginator, exclude navigation blocks (prev, next, first, last pages) |
prevPageText | String / ReactElement | ⟨ | Text of prev page navigation button |
firstPageText | String / ReactElement | « | Text of first page navigation button |
lastPageText | String / ReactElement | » | Text of last page navigation button |
nextPageText | String / ReactElement | ⟩ | Text of next page navigation button |
getPageUrl | Function | Generate href attribute for page | |
innerClass | String | pagination | Class name of <ul> tag |
activeClass | String | active | Class name of active <li> tag |
activeLinkClass | String | Class name of active <a> tag | |
itemClass | String | Default class of the <li> tag | |
itemClassFirst | String | Class of the first <li> tag | |
itemClassPrev | String | Class of the previous <li> tag | |
itemClassNext | String | Class of the next <li> tag | |
itemClassLast | String | Class of the last <li> tag | |
disabledClass | String | disabled | Class name of the first, previous, next and last <li> tags when disabled |
hideDisabled | Boolean | false | Hide navigation buttons (prev, next, first, last) if they are disabled. |
hideNavigation | Boolean | false | Hide navigation buttons (prev page, next page) |
hideFirstLastPages | Boolean | false | Hide first/last navigation buttons |
linkClass | String | Default class of the <a> tag | |
linkClassFirst | String | Class of the first <a> tag | |
linkClassPrev | String | Class of the previous <a> tag | |
linkClassNext | String | Class of the next <a> tag | |
linkClassLast | String | Class of the last <a> tag |