React Bootstrap Table modifications and filtering - reactjs

Modifying react bootstrap table layout to look like a react card list rather than table and enabling button filters.
I've added a basic table with filter. I wanted to find out if there is a way to modify the table's layout structure so that it looks more like bootstrap cards rather than a table. Also, my filter doesn't seem to be working. Once I click a button to apply a particular category nothing is shown. Also, is there a way to hide the input selection on the table and just leave the button?
import React, { Component } from 'react';
import BootstrapTable from 'react-bootstrap-table-next';
import paginationFactory, { PaginationProvider, PaginationListStandalone } from 'react-bootstrap-table2-paginator';
import filterFactory, { textFilter, selectFilter } from 'react-bootstrap-table2-filter';
import 'react-bootstrap-table-next/dist/react-bootstrap-table2.min.css';
import ToolkitProvider, { Search } from 'react-bootstrap-table2-toolkit';
class Favourites extends Component {
constructor(props){
super(props);
}
render() {
const { SearchBar } = Search;
const products_data = [
{
"id": "28f07624",
"name": "Workbook1",
"createdAt": "2019-05-17T17:51:17Z"
},
{
"id": "a3c798fd",
"name": "Workbook2",
"createdAt": "2019-05-14T12:19:38Z",
},
{
"id": "520dccf8",
"name": ["Workbook3", "Workbook4"],
"createdAt": "2019-05-16T13:31:07Z"
}
];
let qualityFilter;
const selectOptions = {
0: 'Workbook1',
1: 'Workbook2',
2: 'Workbook3'
};
const columns = [{
dataField: 'id',
text: 'Product ID'
}, {
dataField: 'name',
text: 'Product Name',
filter: selectFilter({
options: selectOptions,
getFilter: (filter) => {
// qualityFilter was assigned once the component has been mounted.
qualityFilter = filter;
}
})
}, {
dataField: 'createdAt',
text: 'Product Quality',
}];
const handleClick = () => {
qualityFilter(0);
};
return (
<div className="fluid-container">
<div className="container body_wrap">
<h1>Favourites</h1>
<button className="btn btn-lg btn-primary" onClick={ handleClick }>{' Workbook1 '}</button>
<BootstrapTable keyField='id' data={ products_data } columns={ columns } filter={ filterFactory() } />
</div>
</div>
);
}
}
export default Favourites;

Related

react material-ui mui-datatables onRowSelectionChange ID

This is my react code. I am using the material UI. I am working with ID related events. the full code is provided below.
Here, the index ID is getting automatically generated. The issue has to do with that.
import React, { useState } from "react";
import ReactDOM from "react-dom";
import MUIDataTable from "mui-datatables";
import InputLabel from "#material-ui/core/InputLabel";
import MenuItem from "#material-ui/core/MenuItem";
import FormHelperText from "#material-ui/core/FormHelperText";
import FormControl from "#material-ui/core/FormControl";
import Select from "#material-ui/core/Select";
function Ag() {
const [responsive, setResponsive] = useState("vertical");
const onCellClick = () => {
console.log("sadf");
};
const onRowsDelete = () => {
console.log("remove");
};
const onRowSelectionChange = (ev, ex, ez) => {
console.log(ez);
};
const columns = ["Name", "Title", "Location"];
const options = {
filter: true,
filterType: "dropdown",
responsive,
onCellClick,
onRowsDelete,
onRowSelectionChange,
};
const data = [
{
Id: "1",
Name: "sunder",
Title: "dlamds",
Location: "asdfsa",
},
{
Id: "2",
Name: "cvzx",
Title: "sadfsda",
Location: "sadfsdacv",
},
{
Id: "3",
Name: "dsfas",
Title: "werq",
Location: "ewqrwqe",
},
{
Id: "4",
Name: "wqer",
Title: "gfdsg",
Location: "bvcxb",
},
{
Id: "5",
Name: "ereq",
Title: "qwer",
Location: "sdafas",
},
];
return (
<React.Fragment>
<MUIDataTable
title={"ACME Employee list"}
data={data}
columns={columns}
options={options}
/>
</React.Fragment>
);
}
export default Ag;
I want to get a data ID instead of an index ID that was automatically generated when I clicked.
What should I do?
onRowSelectionChange: (currentSelect, allSelected) => {
const result = allSelected.map(item => { return data.at(item.index) });
const selectedIds = result.map(item => {
return item.id;
});
console.log(selectedIds);
}

Data does't appear in datatables

Data tidak tampil pada datatables
import React, { Component } from 'react';
import Linkify from 'react-linkify';
import './CobaData.css';
import './css/jquery.dataTables.css';
const $ = require('jquery')
$.Datatable = require('datatables.net')
export default class CobaData extends Component {
constructor(props) {
super(props);
}
render() {
let data = this.props.data;
let printData = data;
console.log(printData);
let table = $('#exc').dataTable(
{
data: printData,
columns: [
{ title: "ID" },
{ title: "Nama" },
{ title: "Nomor KTP" },
{ title: "Nomor HP" },
],
searchable: false,
orderable: false,
targets: 0,
}
);
console.log(table);
return (
<div>
<table className="display" width="100%" id="exc" >
</table>
</div>
);
}
}
data can't display datatables, when in console.log(printData) can be read in 'console'
before I used a normal table can be read on the table, but when using datatables on the data can not be read on the table
Using jQuery in a react app is not a good approach.
Instead I would recommend you to use react-data-table-component.
And then your Component would be like this:
import React, { Component } from 'react';
import DataTable from 'react-data-table-component';
import Linkify from 'react-linkify';
import './CobaData.css';
const columns = [
{
name: 'ID',
selector: 'id_karyawan'
},
{
name: 'Nama',
selector: 'nama'
},
{
name: 'Nomor KTP',
selector: 'KTP'
},
{
name: 'Nomor HP',
selector: 'no_hp'
},
];
export default class CobaData extends Component {
render() {
let data = this.props.data;
return (
<DataTable
title="Arnold Movies"
columns={columns}
data={data}
/>
);
}
}

Footer in react-table is giving 'Reference Error-data is not defined'

I am making API call and displaying the results in React-Table. What i want to show additionally is in footer i want to show sum of column values for column totalCount. I have given below my code and i am getting
ReferenceError: data is not defined in
Footer: <span>{_.sum(_.map(data, d => d.totalCount))}</span>
My Sample Json data from API would be
[
{
"receiveDate": "2019-01-28",
"hour": "00",
"integrationName": "lms_job",
"totalCount": 61
},
{
"receiveDate": "2019-01-28",
"hour": "01",
"integrationName": "lms_job",
"totalCount": 43
}
]
If i move const columns block inside the class BalancingTable extends React.Component, then it works as expected. But I have also other code which is to download data in CSV and that logic demands me to put the const columns out side of the app.
So If I remove these 2 lines, code still works with out footer logic.
Cell: props => <span>{props.value}</span>,
Footer: <span>{_.sum(_.map(data, d => d.totalCount))}</span>
As i wanted to keep const columns outside of the app, Is there a way to resolve this issue which provides sum of columns as needed?
import React from 'react'
import { bindActionCreators } from 'redux'
import PropTypes, { object } from 'prop-types'
import { withStyles } from '#material-ui/core/styles'
import Button from '#material-ui/core/Button'
import axios from 'axios'
import ReactTable from 'react-table'
import '../FirmJson/ReactTableCustom.css'
import _ from "lodash";
const styles = theme => ({
})
const columns = [
{
Header: 'Date',
accessor: 'receiveDate',
width: 80,
},
{
Header: 'Hour',
accessor: 'hour',
width: 50,
},
{
Header: 'Integration',
accessor: 'integrationName',
width: 140,
},
{
Header: 'Total Count',
accessor: 'totalCount',
width: 105,
id: 'totalCount',
Cell: props => <span>{props.value}</span>,
Footer: <span>{_.sum(_.map(data, d => d.totalCount))}</span>
},
]
class BalancingTable extends React.Component {
constructor (props) {
super(props)
this.state = {
data: [],
isLoaded: false,
}
this.handleSubmit = this.handleSubmit.bind(this)
}
handleSubmit (index) {
let url = 'https://myapp.company.com/balancing/lms/2019-01-29'
axios.get(url).then(response => {
this.setState({
data: response.data,
isLoaded: true,
})
}).catch()
}
render () {
var {isLoaded,data} = this.state
const { classes } = this.props
if (isLoaded) {
return (
<div>
<div>
<ReactTable ref={(r) => this.reactTable = r}
columns={columns}
data={data}
filterable
defaultFilterMethod={(filter, row) =>
String(row[filter.id]).toLowerCase().includes(filter.value.toLowerCase())}
style={{
minWidth: '800px',
border: '50px',
}}
/>
</div>
</div>
)
}
else
{
return(
<div>
<Button variant="raised" color="primary" aria-label="ret" onClick={this.handleSubmit}>
Submit
</Button>
</div>
)
}
}
}
BalancingTable.propTypes = {
classes: PropTypes.object.isRequired,
}
export default (withStyles(styles)(BalancingTable))

How to make a component using <TableRow/>, for breaking up <Table/> component in Ant Design

I want to make a component for the rows in Ant Design Table, where each component will have their own state and based on that table rows will be shown. is it possible?
if possible how can I do it? an example will be helpful.
I tried the following example but it's showing 'No Data'
in item.js
import React, { Component } from "react";
import TableRow from "antd";
class Item extends Component {
constructor(props) {
super(props);
}
dataSource = [
{
key: "1",
details: "test",
price: 50
}
];
render() {
return <TableRow dataSource={this.dataSource} />;
}
}
export default Item;
and in itemlist.js
import React, { Component } from "react";
import { Table } from "antd";
import Item from "./item";
class ItemList extends Component {
constructor(props) {
super(props);
}
columns = [
{
title: "Item Details",
dataIndex: "details",
key: "details"
},
{
title: "Price",
dataIndex: "price",
key: "price"
}
];
render() {
return (
<Table column={this.columns}>
<Item key="1" />
</Table>
);
}
}
export default ItemList;
I'm still not totally get your requirements, but from I understand: you can define render in column definition, and there you'll get an row item:
class ItemList extends Component {
dataSource = [
{
key: "1",
details: "test",
price: 50
}
];
columns = [
{
title: "Item Details",
dataIndex: "details",
render: (text, record, index) => <Item {...record} />
key: "details"
}
];
render() {
return (
<Table columns={this.columns} dataSource={this.dataSource} />
);
}
}
If you just need to not show some rows based on data, you can filter dataSource accordingly.

Pagination not working in react-bootstrap-table2

I am using react-bootstrap-table2-paginator for adding Pagination to my BootstrapTable. I get the below error when I add the Pagination part in the table. Am I missing some imports or something ?
invariant.js:39 Uncaught Error: a.render(): A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object.
Here is the code for the component.
import React, { Component } from 'react';
import BootstrapTable from 'react-bootstrap-table-next';
import cellEditFactory from 'react-bootstrap-table2-editor';
import paginationFactory from 'react-bootstrap-table2-paginator';
class OverviewComponent extends Component {
constructor(props) {
super(props);
}
const products = [];
const columns = [{
dataField: 'id',
text: 'Product ID'
}, {
dataField: 'name',
text: 'Product Name'
}, {
dataField: 'price',
text: 'Product Price'
}];
function addProducts(quantity) {
const startId = products.length;
for (let i = 0; i < quantity; i++) {
const id = startId + i;
products.push({
id: id,
name: 'Item name ' + id,
price: 2100 + i
});
}
}
addProducts(12);
render() {
return (
<div className="container-fluid">
<div className="row">
<BootstrapTable
striped
hover
condensed
maxHeight={15}
keyField="id"
data={ products }
columns={ columns }
cellEdit={ cellEditFactory({ mode: 'dbclick' }) }
pagination={ paginationFactory() }
/>
</div>
</div>
);
}
}
export default OverviewComponent;
Any help would be appreciated.
Thanks
I've corrected some faults with the original implementation. You want to make use of this.state
import React, { Component } from "react";
import BootstrapTable from "react-bootstrap-table-next";
import cellEditFactory from "react-bootstrap-table2-editor";
import paginationFactory from "react-bootstrap-table2-paginator";
class OverviewComponent extends Component {
constructor(props) {
super(props);
this.state = {
products: [],
columns: [
{
dataField: "id",
text: "Product ID"
},
{
dataField: "name",
text: "Product Name"
},
{
dataField: "price",
text: "Product Price"
}
]
};
this.addProduct = this.addProduct.bind(this);
}
addProduct(quantity) {
const startId = products.length;
let { products } = this.state;
for (let i = 0; i < quantity; i++) {
const id = startId + i;
products.push({
id: id,
name: "Item name " + id,
price: 2100 + i
});
}
this.setState({ products: products });
}
render() {
const { products, columns } = this.state;
return (
<div className="container-fluid">
<div className="row">
<BootstrapTable
striped
hover
condensed
maxHeight={15}
keyField="id"
data={products}
columns={columns}
cellEdit={cellEditFactory({ mode: "dbclick" })}
pagination={paginationFactory()}
/>
</div>
</div>
);
}
}
export default OverviewComponent;
Try this, it worked for me. I also had a problem with the wrapper at runtime.
npm install react-bootstrap-table2-paginator#0.1.6 --save
Also, this only works for react#16.0.0 and up. You can upgrade to the latest version of react and react-dom with this:
npm install --save react#latest react-dom#latest
for specific version just specify the version in place of the word latest. eg
npm install --save react#16.4.2 react-dom#16.4.2

Resources