react-bootstrap-table2 search on column with complex (nested object) data - reactjs

I am using react-bootstrap-table2 for table in react project and using search on table. On columns with single and straight data, search is working fine. But on columns with formatted value, search is not working.
Below is structure of my data (sample) to display on table:
[
{
"id": 121212,
"user": "Test1",
"plates": [
{
"id": 101,
"plate": "AA-1122",
},
{
"id": 102,
"plate": "AB-1100",
}
]
},
...]
Below is code for Columns:
columns = [
{ dataField: "id", text: "Id", hidden: true },
{
dataField: "user",
text: "User",
sort: true,
},
{
dataField: "plates",
text: "Plates Test",
formatter: (cell, row) => row.plates.map(x => <>{x.plate}</>),
filterValue: (cell, row) => formatterFilter(cell, row),
}, ];
function formatterFilter(cell, row) {
return cell.plate;
}
Below is code for ToolkitProvider:
<ToolkitProvider
keyField="id"
data={props.data}
columns={props.columns}
// search
search={{ searchFormatted: true }}
>
...
<SearchBar
{...props.searchProps}
style={styles.search}
placeholder="Type Search criteria to filter data"
/>
...
</ToolkitProvider>
As in above code, I am displaying list of plates in a column and want to search on all the columns in table. On Id and User column search is working fine, but on Plates Test column its not working.
Any help on this is appreciated.
I have already gone through below code in Storybook, but don't understand how I can utilize this in my case:
formatter: (cell, row) => types[cell],
filterValue: (cell, row) => types[cell] // we will search the value after filterValue called

I was able to solve this issue with below approach, in case it may help anyone:
columns = [
{ dataField: "id", text: "Id", hidden: true },
{
dataField: "user",
text: "User",
sort: true,
},
{
dataField: "plates",
text: "Plates Test",
hidden: true,
formatter: (cell, row, rowIndex, extraData) => {
//Here I created array and not was able to search on this column.
let plateArr = [];
row.plates.map(x => {
plateArr.push(x.plate);
})
return plateArr.join()
},
},];
And in this case filterValue property was not required.

Related

How to get select option label in addition to its value in react antd form?

I used antd-form-builder to create a form. The form contains select field (dropdown). I used form.getFieldValue("task") to get the value of selected option, I also need to get the label of selected option. how can I get it by clicking on a button?
const meta = (
fields: [
{
key: "task",
label: "Task",
widget: "select",
widgetProps: { showSearch: true },
options: [
{ label: "Pre-filter Replacement", value: 1 },
{ label: "Oil Change", value: 2 },
],
},
]
)
const handleClick = () => {
let taskValue = form.getFieldValue("task")
}
<Form form={form} onValuesChange={forceUpdate} onFinish={onFinish}>
<FormBuilder meta={meta} form={form} />
<Button onClick={handleClick}>Done</Button>
</Form>
You can use labelInValue prop to get the value along with selected option label.
Pass labelInValue: true, in widgetProps
const meta = {
fields: [
{
key: "task",
label: "Task",
widget: "select",
widgetProps: {
showSearch: true,
labelInValue: true,
},
options: [
{ label: "Pre-filter Replacement", value: 1 },
{ label: "Oil Change", value: 2 },
],
},
],
};
Now task value will not be a number buy an object containing label, value, key, disabled.
You can follow the Select API Documentation

How can I get the Material-UI DataGrid valueGetter to read data from a distinct object in an array?

I am developing a React Admin Page for Woocommerce. I want to retrieve the 'option' value from a specific object (product attribute with name = "Farbe") to display in a MUI DataGrid. I think that valueGetter would be the right approach, but can't get it to work.
Here's what I have:
The Woocommerce Product (row record):
{
"id": 232,
"date_created": "2022-08-14T08:02:18",
...
"attributes": [
{
"id": 0,
"name": "Farbe",
"option": "0045"
},
{
"id": 1,
"name": "Material",
"option": "Cotton"
},
...
],
...
}
The DataGrid column:
I am trying to select the object that has the value 'Farbe' on the key 'name' and access the value of the property 'option'
export const VariationColumns = [
{ field: 'id', headerName: 'Id', type: 'int', width: 100},
{ field: 'sku', headerName: 'SKU', type: 'string', width: 200},
{ field: 'name', headerName: 'Name', type: 'string', width: 500,
valueGetter: ( params ) => { return params.row.attributes[name =>'Farbe'].option }},
]
But it can't find the 'option' property:
"TypeError: Cannot read properties of undefined (reading 'option')"
Also tried:
valueGetter: ( params ) => { return params.row.attributes[name =>'Farbe'].option.value
valueGetter: ( params ) => { return params.row.attributes.name['Farbe'].option
valueGetter: ( params ) => { return params.row.attributes.name['Farbe'].option.value
Is there maybe a completely different approach needed to achieve this?
Any hint is greatly apreciated
Assuming that your rows prop looks like the record you provided above, you'd get it like so:
const rows = [
{
id: 232,
date_created: "2022-08-14T08:02:18",
attributes: [
{
id: 0,
name: "Farbe",
option: "0045",
},
{
id: 1,
name: "Material",
option: "Cotton",
},
],
},
];
const variationColumns = [
{ field: "id", headerName: "Id", type: "int", width: 100 },
{ field: "sku", headerName: "SKU", type: "string", width: 200 },
{
field: "attributes",
headerName: "Name",
type: "string",
width: 500,
valueGetter: (params) => {
return params.value.find((item) => item.name === "Farbe").option;
},
},
];
The key points are:
valueGetter params are cell params vs row params
The field property in the columns needs to match the field in your rows, so if you want to grab attributes you need to have field: "attributes" in your columns.
You can use params.value.find((item) => item.name === "Farbe").option) to return the object in the array that matches your desired search string, then access its option property.

Parsing nested json data in reactjs usind datagrid from material ui

I need to parse nested JSON data to normal JSON which is coming from an API and need to pass that JSON data to a react data grid (am using one available from material ui) like below :
Json Data
{
"rows": [
{
"id": 101,
"name": "sample",
"last_modified": "sample time",
"countries": [
{
"country_id": 1,
"country_name": "countryA"
},
{
"country_id": 2,
"country_name": "countryB"
}
]
},
{
"id": 102,
"name": "sample 2",
"last_modified": "sample time",
"cdn": [
{
"country_id": 3,
"country_name": "countryC"
},
{
"country_id": 4,
"country_name": "countryD"
}
]
}
]
}
In Data.JSX
const checkKeysUnderObject = (obj, result) => {
for (let key in obj) {
if (key) {
result.push(key + " : " + obj[key]);
}
}
};
const columns = [
{ field: 'id', headerName: 'ID', width: 100, hide: true, disableColumnFilter: true, disableColumnSelector: true, headerAlign:'center'},
{
field: 'id',
headerName: 'ID',
width: 250,
editable: false,
},
{
field: 'name',
headerName: 'Name',
width: 250,
editable: false,
},
{
field: 'last_modified',
headerName: 'Last Modified',
width: 250,
editable: false,
},
{
field: 'countries',
headerName: 'Countries',
width: 500,
editable: false,
disableColumnFilter: true,
valueGetter: (params) => {
console.log({ params });
let result = [];
if (params.row.countries) {
checkKeysUnderObject(params.row.countries, result);
} else {
result = ["Unknown"];
}:
return result.join(" , ");
}
},
}
];
Getting the following output needed to print the country_id and country_name
0 : [object Object] , 1 : [object Object]
In console log
params
params
id: 101, field: countries, row:
countries: Array(2)
0: {country_id: 1, country_name: 'countryA'}
1: {country_id: 2, country_name: 'countryB'}
length: 2
[[Prototype]]: Array(0)
id: 101
last_modified: "sample time"
name: "sample"
[[Prototype]]: Object
Using DataGrid to display
<DataGrid
rows={response}
columns={columns}
//pageSize={5}
//rowsPerPageOptions={[5]}
autoPageSize
density='standard'
/>
Can anyone plz help.
Questions:How to render nested JSON elements in ReactJS material-ui based DataGrid? Please provide solution to this issue.
Got required output such as,
country_id : 1 , country_name : countryA , country_id : 2 , country_name : countryB
tried in value getter:
checkKeysUnderObject(params.row.countries[0], result);
checkKeysUnderObject(params.row.countries[1], result);
Answer by Janice Zhong in one of the post material-ui-datagrid-with-nested-data helped to find the solution for this problem faced, thanks
It may be useful to others so posting the answer here, was in confusion not getting the required answer so posted the question and then tried this it worked. Thanks!!!

Adding serial number to the react bootstrap table

I am using a React-bootstrap table in my project, I want to add a column in my table with a serial number but am unable to generate the serial number so kindly guide me on how to do it.
Currently am writing the below code to generate serial number but not getting the numbers in the columns.
const columns = [
{ text: 'Sn',
cell: (row, index) => index + 1 ,
headerAttrs: { width: "50px" }
},
{
text: "Customer Id",
dataField: "customerId",
classes: "alignment"
},
{
text: "Email",
dataField: "email",
classes: "alignment",
headerAttrs: { width: "200px" }
},
{
text: "Role",
dataField: "role"
},
{
text: "Date Created",
dataField: "dateCreated",
formatter: dateFormatter
},
{
text: "Customer Type",
dataField: "plan"
},
];
You can use the following formatter property to get the respective serial number.
{
dataField: 'sl.no',
text: 'Sl no.',
formatter: (cell, row, rowIndex, formatExtraData) => {
return rowIndex + 1;
},
sort: true,
},
👎The counter restarts in the next page.

react-bootstrap-table2 search is not working on formatted cells

I need help on including the formatted data into the search functionality. For some reason the formatted links are not included in the search
function docFormatter(cell, row, rowIndex) {
return (
<a href={${row.doc.doclink}}>
{row.doc.docname}
);
}
const columns = [
{ dataField: "sno", text: "S.No" },
{
dataField: "doc",
text: "Document Name",
formatter: docFormatter,
filterValue: docFormatter,
},
];
I have a customer formatter and that returns a anchor tag with a text. the search is not working on this.
I looked document https://react-bootstrap-table.github.io/react-bootstrap-table2/docs/basic-search.html#search-on-formatted-data
..., {
dataField: 'type',
text: 'Job Type',
formatter: (cell, row) => types[cell],
filterValue: (cell, row) => types[cell] // we will search the value after filterValue called
}
I did'nt understand how to implement this by referencing this. Please help me on this. Thanks
Here is the working model fixing this issue
function docFormatter(cell, row, rowIndex) {
return (
<a href={${row.doc.doclink}}>
{row.doc.docname}
);
}
function docFormatterFilter(cell, row) {
return cell.docname;
}
const columns = [
{ dataField: "sno", text: "S.No" },
{
dataField: "doc",
text: "Document Name",
formatter: docFormatter,
filterValue: (cell, row) => docFormatterFilter(cell, row),
},
];
I'm encountering the same issue but can't find solution.
function docFormatterFilter(cell, row) {
return row.nom;
}
export const fieldsUser = [
{
dataField: 'id',
sort:true,
text: '',
formatter: (cell, row) => {
return (
<div className={`flex-grow-1`}>
<div className="d-flex align-items-center justify-content-between">
<h4 className="m-0">{row.nom}</h4>
<small className={ `role-groupe text-center ${row.role==='s' ? 'bg-danger' : row.role==='a'?'bg-success':'bg-primary'}` }>{row.role_label}</small>
</div>
</div>
)},
filterValue: (cell, row) => docFormatterFilter(cell, row),
filter: textFilter({
caseSensitive: false,
comparator: Comparator.LIKE,
}),
classes: 'col-12',
}
];
Nothing works since a week.

Resources