Array state variable doesn't render - reactjs

I have a table that needs to be rendered dynamically so I put in state, the problem is that I can't return it via state , it can only be queried with forEach function directly:
This code works:
render() {
let rows=[], rows2=[];
var PRODUCTS = [
{id:0,category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'},
{id:1,category: 'Sporting Goods', price: '$9.99', stocked: true, name: 'Baseball'},
{id:2,category: 'Sporting Goods', price: '$29.99', stocked: false, name: 'Basketball'},
{id:3,category: 'Electronics', price: '$99.99', stocked: true, name: 'iPod Touch'},
{id:4,category: 'Electronics', price: '$399.99', stocked: false, name: 'iPhone 5'},
{id:5,category: 'Electronics', price: '$199.99', stocked: true, name: 'Nexus 7'}
];
PRODUCTS.forEach((product)=>{
if( product.name.indexOf(this.props.searchText)==-1 && this.props.searchText!='')
{return;}
if(this.props.inStockOnly){
if(product.stocked){
if(product.category=="Sporting Goods"){
rows.push(<ProductRow product={product} key={product.id} onDestroy={this.Destroy}/>)
}
else {
rows2.push(<ProductRow product={product} key={product.id} onDestroy={this.Destroy}/>)
}
}
}
else {
if(product.category=="Sporting Goods"){
rows.push(<ProductRow product={product} key={product.id} onDestroy={this.Destroy}/>)
}
else {
rows2.push(<ProductRow product={product} key={product.id} onDestroy={this.Destroy}/>)
}
}
})
And this doesn't:
render() {
let rows=[], rows2=[];
this.state.products.forEach((product)=>{
if( product.name.indexOf(this.props.searchText)==-1 && this.props.searchText!='')
{return;}
if(this.props.inStockOnly){
if(product.stocked){
if(product.category=="Sporting Goods"){
rows.push(<ProductRow product={product} key={product.id} onDestroy={this.Destroy}/>)
}
else {
rows2.push(<ProductRow product={product} key={product.id} onDestroy={this.Destroy}/>)
}
}
}
else {
if(product.category=="Sporting Goods"){
rows.push(<ProductRow product={product} key={product.id} onDestroy={this.Destroy}/>)
}
else {
rows2.push(<ProductRow product={product} key={product.id} onDestroy={this.Destroy}/>)
}
}
})
It triggers this error :
product.name.indexOf is not a function
Update
this how I set my state :
var PRODUCTS = [
{id:0,category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'},
{id:1,category: 'Sporting Goods', price: '$9.99', stocked: true, name: 'Baseball'},
{id:2,category: 'Sporting Goods', price: '$29.99', stocked: false, name: 'Basketball'},
{id:3,category: 'Electronics', price: '$99.99', stocked: true, name: 'iPod Touch'},
{id:4,category: 'Electronics', price: '$399.99', stocked: false, name: 'iPhone 5'},
{id:5,category: 'Electronics', price: '$199.99', stocked: true, name: 'Nexus 7'}
];
class ProductTable extends React.Component {
constructor(props)
{
super(props)
this.state={
products:PRODUCTS
};
}......
Can you help me ?

As #AdrianoReptti said in comments ,the problem was with name.
Actually in another component i changed the product's name in order to light out if the components is in stock or not :
product.name=product.stocked?product.name:<span style={{color:"red"}}>{product.name}</span>;
i fixed the code and now it runs :)
But it's still weird i thought that the state of a component is private and can only be changed with setState and not with the return of a children component.

For me your code is working fine. I have tried in the console. Try to restart the server.

Related

Implementing React table expanded rows

It's my first time to use react-table librabry. I am struggling for the previous couple of days how to implement the expansion functionality using useExpanded hook
Docs and all the examples available are not clear about that so, here is my code and hope if somone can give me a hand in that
const accountData = [
{
company: "621bb1bda49c57b938fb9b8c",
country: "6228dac2ad9aef4e064906c3",
accountName: "Oriana Hospital",
accountType: "Hospital ",
city: "Sharjah",
area: "Al Khan",
street: "Al Taawun",
location: "https://goo.gl/maps/Jx6oTCbXyq9C8Mrt7",
hasBranches: true,
numberOfDoctors: null,
accountNotes: [],
numberOfBranches: 1,
expand: false,
branches: [
{
branchName: "Oriana Pulman ",
branchType: "Hospital ",
branchCity: "Sharjah",
branchArea: "Sharjah",
branchStreet: "Al Khan",
branchLocation: "Al Taawun",
_id: "622e2f05cc18e17eb608541a",
},
],
_id: "622e2f04cc18e17eb6085419",
},
{
company: "621bb1bda49c57b938fb9b8c",
country: "6228dac2ad9aef4e064906c3",
accountName: "Ajman Speciality",
accountType: "Hospital ",
city: "Ajman",
area: "Ajman",
street: "Al Ittihad Road",
location: "https://goo.gl/maps/9aizJaNxT7iMDVry6",
hasBranches: false,
numberOfDoctors: null,
accountNotes: [],
numberOfBranches: 0,
expand: false,
branches: [],
_id: "622e2f05cc18e17eb608541b",
},
{
company: "621bb1bda49c57b938fb9b8c",
country: "6228dac2ad9aef4e064906c3",
accountName: "Al Ittihad Medical Center",
accountType: "Clinic",
city: "Ajman",
area: "Al Swan",
street: "Ittihad ",
location: "https://goo.gl/maps/ppLNbPB9CdGWqFpy6",
hasBranches: false,
numberOfDoctors: null,
accountNotes: [],
numberOfBranches: 0,
expand: false,
branches: [],
_id: "622e2ff2a2c75bcb9aaf640e",
},
{
company: "621bb1bda49c57b938fb9b8c",
country: "6228dac2ad9aef4e064906c3",
accountName: "Al Zaora Medical Center",
accountType: "Clinic",
city: "Ajman",
area: "Al Swan",
street: "Ittihad ",
location: "https://goo.gl/maps/PZr5PqFf28rpdgYJ7",
hasBranches: false,
numberOfDoctors: null,
accountNotes: [],
numberOfBranches: 0,
expand: false,
branches: [],
_id: "622e2ff2a2c75bcb9aaf640f",
},
{
company: "621bb1bda49c57b938fb9b8c",
country: "6228dac2ad9aef4e064906c3",
accountName: "Amina Hospital",
accountType: "Hospital ",
city: "Ajman",
area: "Al Swan",
street: "Ittihad ",
location: "https://goo.gl/maps/Bxn7tedGQxUZFNqa8",
hasBranches: true,
numberOfDoctors: null,
accountNotes: [],
numberOfBranches: 3,
expand: false,
branches: [
{
branchName: "Ibn Sina ",
branchType: "Clinic",
branchCity: "Ajman",
branchArea: "Al Swan",
branchStreet: "Ittihad",
branchLocation: "https://goo.gl/maps/M3WemaX4ehbp6rSS6",
_id: "622e2ff2a2c75bcb9aaf6411",
},
{
branchName: "Amina Al Jurf",
branchType: "Clinic",
branchCity: "Ajman",
branchArea: "Al Jurf",
branchStreet: "Sheikh Khalifa",
branchLocation: "https://goo.gl/maps/VGgU5TGSBvaUnGqm6",
_id: "622e2ff2a2c75bcb9aaf6412",
},
{
branchName: "Family Medical Center",
branchType: "Clinic",
branchCity: "Ajman",
branchArea: "Ajman",
branchStreet: "Industiral Ajman",
branchLocation: "https://goo.gl/maps/wvqTbtuRakzaYTWg7",
_id: "622e2ff2a2c75bcb9aaf6413",
},
],
_id: "622e2ff2a2c75bcb9aaf6410",
},
{
company: "621bb1bda49c57b938fb9b8c",
country: "6228dac2ad9aef4e064906c3",
accountName: "Al Garhoud Private Hospital",
accountType: "Hospital ",
city: "Dubai",
area: "Al Garhoud",
street: "Reiyadh",
location: "https://goo.gl/maps/LVpuJhMBT2MrobPT6",
hasBranches: true,
numberOfDoctors: null,
accountNotes: [],
numberOfBranches: 1,
expand: false,
branches: [
{
branchName: "Al Garhoud 2",
branchType: "Hospital ",
branchCity: "Dubai",
branchArea: "Mirdif",
branchStreet: "Sheikh Mohamed Bin Zayed",
branchLocation: "https://goo.gl/maps/p2bKwpnBuLaH2yxS8",
_id: "622e2ff2a2c75bcb9aaf6415",
},
],
_id: "622e2ff2a2c75bcb9aaf6414",
},
{
company: "621bb1bda49c57b938fb9b8c",
country: "6228dac2ad9aef4e064906c3",
accountName: "Emirates Hospital",
accountType: "Hospital ",
city: "Dubai",
area: "Jumeirah",
street: "Al Wasl",
location: "https://goo.gl/maps/gqHVMFvhpnGgDCiL8",
hasBranches: true,
numberOfDoctors: null,
accountNotes: [],
numberOfBranches: 2,
expand: false,
branches: [
{
branchName: "Emirates Marina",
branchType: "Hospital ",
branchCity: "Dubai",
branchArea: "Marina",
branchStreet: "Marina",
branchLocation: "https://goo.gl/maps/p2bKwpnBuLaH2yxS8",
_id: "622e2ff2a2c75bcb9aaf6417",
},
{
branchName: "Emirates Clinic",
branchType: "Clinic",
branchCity: "RAK",
branchArea: "RAK",
branchStreet: "Khalid street ",
branchLocation: "https://goo.gl/maps/NMN68z5VqB9AoUKS7",
_id: "622e2ff2a2c75bcb9aaf6418",
},
],
_id: "622e2ff2a2c75bcb9aaf6416",
},
{
company: "621bb1bda49c57b938fb9b8c",
country: "6228dac2ad9aef4e064906c3",
accountName: "Al Shams Medical Diagnsotice Center",
accountType: "Group",
city: "Sharjah",
area: "Industrial area",
street: "Shams",
location: "https://g.page/alshamsmedicalgroup?share",
hasBranches: true,
numberOfDoctors: null,
accountNotes: [],
numberOfBranches: 2,
expand: false,
branches: [
{
branchName: "Al Shams Al Jadeed ",
branchType: "Clinic",
branchCity: "Sharjah",
branchArea: "Industrial Sharjah",
branchStreet: "Demuscus",
branchLocation: "https://goo.gl/maps/ZbEn1or58nYKhvSK9",
_id: "622e2ff2a2c75bcb9aaf641a",
},
{
branchName: "Al Shams Clinic",
branchType: "Clinic",
branchCity: "Sharjah",
branchArea: "Industrial 3",
branchStreet: "unknown street",
branchLocation: "https://goo.gl/maps/391Dz4B4EkxASjjm6",
_id: "622e2ff2a2c75bcb9aaf641b",
},
],
_id: "622e2ff2a2c75bcb9aaf6419",
},
{
company: "621bb1bda49c57b938fb9b8c",
country: "6228dac2ad9aef4e064906c3",
accountName: "Ibn Batuta DrugStore",
accountType: "Drugstore",
city: "Sharjah",
area: "Al Nema",
street: "Al Nema",
location: "https://goo.gl/maps/6jka7BaGCguTXTza7",
hasBranches: false,
numberOfDoctors: null,
accountNotes: [],
numberOfBranches: 0,
expand: false,
branches: [],
_id: "622e2ff2a2c75bcb9aaf641c",
},
{
company: "621bb1bda49c57b938fb9b8c",
country: "6228dac2ad9aef4e064906c3",
accountName: "Tamimi Drug Store",
accountType: "Drugstore",
city: "Sharjah",
area: "Industrial area",
street: "Industrial ",
location: "https://goo.gl/maps/ybWjXf2ZTBCsi5r68",
hasBranches: false,
numberOfDoctors: null,
accountNotes: [],
numberOfBranches: 0,
expand: false,
branches: [],
_id: "622e2ff2a2c75bcb9aaf641d",
},
{
company: "621bb1bda49c57b938fb9b8c",
country: "6228dac2ad9aef4e064906c3",
accountName: "Al Zaora Pharmacy",
accountType: "Pharmacy",
city: "Ajman",
area: "Al Swan",
street: "Ittihad ",
location: "https://goo.gl/maps/PZr5PqFf28rpdgYJ7",
hasBranches: false,
numberOfDoctors: null,
accountNotes: [],
numberOfBranches: 0,
expand: false,
branches: [],
_id: "622e2ff2a2c75bcb9aaf641e",
},
];
export default accountData;
Then in the table if the account hasBranches is true I want to show expanding button to show the branches only onClick
and here is table code
import {
useTable,
useGlobalFilter,
useFilters,
usePagination,
useSortBy,
useRowSelect,
useExpanded,
} from "react-table";
import ColumnFilter from "./ColumnFilter";
import accountData from "./Data";
import { Columns } from "./Columns";
import { useMemo } from "react";
import "./table.css";
import GlobalFilter from "./GlobalFilter";
import { CheckBox } from "./CheckBos";
const PaginationTable = (props) => {
const columns = useMemo(() => Columns, []);
const data = useMemo(() => accountData, []);
const defaultColumns = useMemo(() => {
return {
Filter: ColumnFilter,
};
}, []);
const onClickRow = (e) => {
const row = e.target;
console.log(row);
};
const tableInstance = useTable(
{
columns,
data,
defaultColumns,
expanded: {},
},
useFilters,
useGlobalFilter,
useSortBy,
useExpanded,
usePagination,
useRowSelect,
(hooks) => {
hooks.visibleColumns.push((columns) => [
// Let's make a column for selection
{
id: "selection",
Header: ({ getToggleAllPageRowsSelectedProps }) => (
<div>
<CheckBox {...getToggleAllPageRowsSelectedProps()} />
</div>
),
Cell: ({ row }) => (
<div>
<CheckBox {...row.getToggleRowSelectedProps()} />
</div>
),
},
{
// Build our expander column
id: "expander", // Make sure it has an ID
Header: ({ getToggleAllRowsExpandedProps, isAllRowsExpanded }) => (
<span {...getToggleAllRowsExpandedProps()}>
{isAllRowsExpanded ? "👇" : "👉"}
</span>
),
Cell: ({ row, index }) =>
// Use the row.canExpand and row.getToggleRowExpandedProps prop getter
// to build the toggle for expanding a row
row.canExpand
? (row.canExpand,
(
<span
onClick={() => {
console.log(row);
}}
{...row.getToggleRowExpandedProps({
style: {
// We can even use the row.depth property
// and paddingLeft to indicate the depth
// of the row
paddingLeft: `${row.depth * 2}rem`,
},
})}
>
{row.isExpanded ? "👇" : "👉"}
</span>
))
: null,
},
...columns,
]);
}
);
const {
getTableProps,
getTableBodyProps,
headerGroups,
page,
rows,
getRowId,
rowsById,
getSubRows,
nextPage,
previousPage,
canNextPage,
canPreviousPage,
pageOptions,
prepareRow,
gotoPage,
pageCount,
setPageSize,
canExpand,
selectedFlatRows,
allColumns,
getToggleHideAllColumnsProps,
state,
setGlobalFilter,
} = tableInstance;
const { globalFilter, pageIndex, pageSize, expanded } = state;
const expandedRows = useMemo(() => {
if (accountData?.hasBranches) {
let arr = [{ 0: false }];
let d = accountData.hasBranches;
if (d.getGroupedSamplingStationBySystemId.length > 0) {
arr = d.getGroupedSamplingStationBySystemId.map((sid, ind) => {
return { [ind]: true };
});
}
return arr;
}
}, []);
// console.log(canExpand);
return (
<>
<div className="hiding">
<div className="toggleVisibility">
<CheckBox {...getToggleHideAllColumnsProps()} /> Toggle All
</div>
{allColumns.map((column) => (
<div key={column.id}>
<label>
<input type="checkbox" {...column.getToggleHiddenProps()} />
{column.Header}
</label>
</div>
))}
</div>
<GlobalFilter filter={globalFilter} setFilter={setGlobalFilter} />
<table {...getTableProps()} className="table">
<thead>
{headerGroups.map((headerGroup) => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map((column) => (
<th {...column.getHeaderProps(column.getSortByToggleProps())}>
{column.render("Header")}
<div>{column.canFilter ? column.render("Filter") : null}</div>
<span>
{column.isSorted
? column.isSortedDesc
? " 🔽"
: " 🔼"
: ""}
</span>
</th>
))}
</tr>
))}
<tr></tr>
</thead>
<tbody {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row);
return (
<tr {...row.getRowProps()}>
{row.cells.map((cell) => {
return (
<td {...cell.getCellProps()}>{cell.render("Cell")}</td>
);
})}
</tr>
);
})}
</tbody>
</table>
<pre>
<code>
{JSON.stringify(
{ selectedFlatRows: selectedFlatRows.map((row) => row.original) },
null,
2
)}
,
</code>
</pre>
<pre>
<code>{JSON.stringify({ expanded: expanded }, null, 2)}</code>
</pre>
<div className="pagination">
<span className="pages">
Page{" "}
<strong>
{pageIndex + 1} of {pageOptions.length}
</strong>{" "}
</span>
<span className="pages">
| Go to Page{" "}
<input
type="number"
defaultValue={pageIndex + 1}
onChange={(e) => {
const pageNumber = e.target.value
? Number(e.target.value) - 1
: 0;
gotoPage(pageNumber);
}}
className="pageNumber"
style={{ width: "50px" }}
/>
</span>
<select
value={pageSize}
onChange={(e) => setPageSize(Number(e.target.value))}
className="paginationDropDown"
>
{[5, 10, 25, 50].map((pageSize) => {
return (
<option key={pageSize} value={pageSize}>
Show {pageSize} per page
</option>
);
})}
</select>
<button
onClick={() => gotoPage(0)}
disabled={!canPreviousPage}
className="paginationButton"
>
{"<<"}
</button>
<button
onClick={() => previousPage()}
disabled={!canPreviousPage}
className="paginationButton"
>
Previous
</button>
<button
onClick={() => nextPage()}
disabled={!canNextPage}
className="paginationButton"
>
Next
</button>
<button
onClick={() => gotoPage(pageCount - 1)}
disabled={!canNextPage}
className="paginationButton"
>
{">>"}
</button>
</div>
</>
);
};
export default PaginationTable;
and here is the screen I am getting
and here is row log
> allCells: (9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] canExpand:
> false cells: (9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] depth:
> 0 getRowProps: Æ’ (userProps) getToggleRowExpandedProps: Æ’ (userProps)
> getToggleRowSelectedProps: Æ’ (userProps) id: "12" index: 12
> isExpanded: undefined isSelected: false isSomeSelected: false
> original: {company: '621bb1bda49c57b938fb9b8c', country:
> '6228dac2ad9aef4e064906c3', accountName: 'Prime Medical Center',
> accountType: 'Group', city: 'Dubai', …} originalSubRows: [] subRows:
> [] toggleRowExpanded: Æ’ (set) toggleRowSelected: Æ’ (set) values:
> {accountName: 'Prime Medical Center', accountType: 'Group', city:
> 'Dubai', area: 'Qusais', street: 'Industrial ', …} [[Prototype]]:
> Object
OK, then I am sure it gonna be something very simple to be added
what I need to know is
How to detect expanded rows
Where to put the subrows code in the table
How to show them conditionally
I will be grateful if I can get any help soon
Thanks in advance
For anyone might come here I finally found the solution in this link https://www.freakyjolly.com/react-table-tutorial/ and here also my code
import accountData from "./Data";
export const modifiedData = accountData.map((d) => {
return {
accountName: d.accountName,
accountType: d.accountType,
city: d.city,
area: d.area,
street: d.street,
location: d.location,
numberOfBranches: d.numberOfBranches,
subRows: !d.hasBranches
? null
: d.branches.map((b) => {
return {
accountName: b.branchName,
accountType: b.branchType,
city: b.branchCity,
area: b.branchArea,
street: b.branchStreet,
location: b.branchLocation,
numberOfBranches: 0,
};
}),
};
});

Price filtering ascending & descending React js

I tried to set up the functionality to filter my product in an ascending or descending manner.
If you check my code below in order to access the price of each product I tried to map through the products, remove then the dollar sign and sort it to ascending and finally set the new state of the products.
But now I feel confused now because I have the sorted array but no more the entire product when trying to set the new state of the product ??
Thank you for helping
const data = [
{
category: "Sporting Goods",
price: "$49.99",
stocked: true,
name: "Football",
},
{
category: "Sporting Goods",
price: "$9.99",
stocked: true,
name: "Baseball",
},
{
category: "Sporting Goods",
price: "$29.99",
stocked: false,
name: "Basketball",
},
{
category: "Electronics",
price: "$99.99",
stocked: true,
name: "iPod Touch",
},
{
category: "Electronics",
price: "$399.99",
stocked: false,
name: "iPhone 5",
},
{ category: "Electronics", price: "$199.99", stocked: true, name: "Nexus 7" },
];
export default data;
const App = () => {
const [searchValue, setSearchValue] = useState("");
const [productsInfo, setProductsInfo] = useState([]);
const handleChange = (e) => {
setSearchValue(e.target.value);
};
const selectedChangeFilter = (e) => {
if (e.target.value === "sporting goods") {
const sportingGoods = data.filter(
(product) => product.category === "Sporting Goods"
);
setProductsInfo(sportingGoods);
}
if (e.target.value === "electronics") {
const electronicsGoods = data.filter(
(product) => product.category === "Electronics"
);
setProductsInfo(electronicsGoods);
}
if (e.target.value === "lowest price") {
const lowestPriceGoods = data
.map((product) => product.price.substr(1))
.sort((a, b) => a - b);
console.log(lowestPriceGoods);
setProductsInfo(lowestPriceGoods);
}
if (e.target.value === "all") {
setProductsInfo(data);
}
};
const searchProducts = (products) => {
if (searchValue.toLowerCase().trim() === "") {
setProductsInfo(products);
} else {
const seekedItem = productsInfo.filter(
(product) =>
product.name.toLowerCase().trim().includes(searchValue) ||
product.category.toLowerCase().trim().includes(searchValue)
);
setProductsInfo(seekedItem);
}
};
return (
<div>
<SearchInput
handleChange={handleChange}
searchValue={searchValue}
selectedChangeFilter={selectedChangeFilter}
/>
<Products
data={data}
searchValue={searchValue}
productsInfo={productsInfo}
searchProducts={searchProducts}
/>
</div>
);
};
export default App;
Try this improved code
const selectedChangeFilter = (e) => {
const { value } = e.target
if (value === "sporting goods") {
const sportingGoods = data.filter(
(product) => product.category === "Sporting Goods"
);
setProductsInfo(sportingGoods);
}
if (value === "electronics") {
const electronicsGoods = data.filter(
(product) => product.category === "Electronics"
);
setProductsInfo(electronicsGoods);
}
if (value === "lowest price") {
const lowestPriceGoods = data.sort((el1,el2) => el1.price.localeCompare(el2.price, undefined, {numeric: true}));
setProductsInfo([...lowestPriceGoods]);
}
if (value === "highest price") {
const highestPriceGoods = data.sort((el1,el2) => el2.price.localeCompare(el1.price, undefined, {numeric: true}));
setProductsInfo([...highestPriceGoods]);
}
if (value === "all") {
setProductsInfo(data);
}
};
You can use string#localeCompare to sort your array based on the number by using numeric property.
const data = [ { category: "Sporting Goods", price: "$49.99", stocked: true, name: "Football", }, { category: "Sporting Goods", price: "$9.99", stocked: true, name: "Baseball", }, { category: "Sporting Goods", price: "$29.99", stocked: false, name: "Basketball",}, { category: "Electronics", price: "$99.99", stocked: true, name: "iPod Touch", }, { category: "Electronics", price: "$399.99", stocked: false, name: "iPhone 5", }, { category: "Electronics", price: "$199.99", stocked: true, name: "Nexus 7" }, ];
data.sort((a,b) => a.price.localeCompare(b.price, undefined, {numeric: true}));
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Ant design 4 table loading not working properly

Im using my react project for ant design 4, i added table loading for loading, I have some conflict added loaded not working, anyone know how to fix that issue?
Thanks
code here
Here the stackblitz
const columns = [
{
title: "Name",
dataIndex: "name",
key: "name",
render: text => {text}
},
{
title: "Age",
dataIndex: "age",
key: "age"
},
{
title: "Address",
dataIndex: "address",
key: "address"
},
{
title: "Tags",
key: "tags",
dataIndex: "tags",
render: tags => (
<span>
{tags.map(tag => {
let color = tag.length > 5 ? "geekblue" : "green";
if (tag === "loser") {
color = "volcano";
}
return (
<Tag color={color} key={tag}>
{tag.toUpperCase()}
</Tag>
);
})}
</span>
)
},
{
title: "Action",
key: "action",
render: (text, record) => (
<span>
Invite {record.name}
<Divider type="vertical" />
Delete
</span>
)
}
];
class App extends React.Component {
state = {
data: [],
pagination: {
current: 1,
pageSize: 10
},
loading: true
};
componentDidMount() {
setTimeout(() => {
this.setState({ loading: false });
}, 1000);
}
render() {
const data = [
{
key: "1",
name: "John Brown",
age: 32,
address: "New York No. 1 Lake Park",
tags: ["nice", "developer"]
},
{
key: "2",
name: "Jim Green",
age: 42,
address: "London No. 1 Lake Park",
tags: ["loser"]
},
{
key: "3",
name: "Joe Black",
age: 32,
address: "Sidney No. 1 Lake Park",
tags: ["cool", "teacher"]
}
];
return (
<Table
columns={columns}
dataSource={data}
loading={this.state.loading}
onChange={this.handleTableChange}
/>
);
}
}
ReactDOM.render(<App />, document.getElementById("container"));

Is there a way to group columns in MUI-dataTable?

I am using MUIDataTable in my React project. I just want to group columns by adding borders to the table. Please find the code below which I am using.
const columns = [
{
name: "name",
label: "Name",
options: {
filter: true,
sort: true,
[customBodyRender: (value) => {
return (
<div style={{ borderRight: "solid 2px" }} >
{value}
</div>
)
}][1]
}
},
{
name: "company",
label: "Company",
options: {
filter: true,
sort: false,
}
},
{
name: "city",
label: "City",
options: {
filter: true,
sort: false,
customBodyRender: (value) => {
return (
<div style={{ borderRight: "solid 2px" }} >
{value}
</div>
)
}
}
},
{
name: "state",
label: "State",
options: {
filter: true,
sort: false,
}
},
];
const data = [
{ name: "Joe James", company: "Test Corp", city: "Yonkers", state: "NY" },
{ name: "John Walsh", company: "Test Corp", city: "Hartford", state: "CT" },
{ name: "Bob Herm", company: "Test Corp", city: "Tampa", state: "FL" },
{ name: "James Houston", company: "Test Corp", city: "Dallas", state: "TX" },
{ name: "", company: "", city: "Tampa", state: "" },
{ name: "", company: "", city: "", state: "" },
{ name: "", company: "", city: "Hartford", state: "" },
{ name: "", company: "", city: "", state: "" },
{ name: "", company: "", city: "", state: "" },
{ name: "", company: "", city: "", state: "" },
];
const options = {
filterType: 'checkbox',
};
<MUIDataTable
title={"Employee List"}
data={data}
columns={columns}
options={options}
/>
This is not working as expected. Either the border height is not proper or in case of no data in the table row, there is no border at all.
I also want the borders to appear on the table headers.
I have attached the snapshot of the output for reference.
OutputImage
Is there a way to fix this?
there is a PR but it's in beta only https://github.com/gregnb/mui-datatables/pull/1441, not yet merged into the master branch

AngularJS filter by dropdown value

I have a problem with filter my data by category selected by dropdownlist.
Here how looks like my controller:
var category = {
"Dairy": 1,
"Bread": 2,
"Drink": 3,
"Spices": 4,
"Meat": 5
}
$scope.categories = [
{ text: "All", value: 0 },
{ text: "Dairy", value: 1 },
{ text: "Bread", value: 2 },
{ text: "Drink", value: 3 },
{ text: "Spices", value: 4 },
{ text: "Meat", value: 5 }
];
$scope.products = [
{ name: "Milk", value: 3.28, avaible: true, expireDate: "2016-04-29", category: category.Dairy },
{ name: "Serek wiejski", value: 1.28, avaible: false, expireDate: "2016-04-26", category: category.Dairy },
{ name: "Coca-cola", value: 6.98, avaible: true, expireDate: "2017-04-29", category: category.Drink },
{ name: "Corn", value: 0.99, avaible: true, expireDate: "2016-09-19", category: category.Spices },
{ name: "Ham", value: 9.00, avaible: true, expireDate: "2016-04-29", category: category.Meat },
{ name: "Bread", value: 3.78, avaible: true, expireDate: "2016-04-29", category: category.Dairy }
];
Here is HTML
<select ng-model="selectedCategory">
<option ng-repeat="cat in categories" value="{{cat.value}}">
{{cat.text}}
</option>
</select>
<div ng-repeat="product in products | filter: product.category == selectedCategory">
After I change value of dropdown the products are not being filtered. Just for id = 5 it works as expected.
Is condition product.category = selectedCategory wrong?
try this
<div ng-repeat="product in products | filter:{category:selectedCategory}">

Resources