How can we disable antd table's checkboxes based on condition? - reactjs

For example there are n no of records I want to enable only selected checboxed reamining all records should be disabled automatically?
please check this below my getCheckboxProps() function check is wrong unable to solve it
Sandbox link
I wanted to disable all non selected checkboxes and here I have no of checboxes can be enabled length we should not cross that length.In my case remaining_total variable.Can anyone help me in right direction?

Your provided link was broken, however one way to disabled all the checkbox in antd table row is to return {disabled: true} on getCheckboxProps function on each cycle. If you have a unique key or item, you can use that to disable specific row, just return {disabled: true} if meet a condition. Here's sample on enabling the first two rows checkbox:
const data = [
{
key: "1",
name: "John Brown",
age: 32,
address: "New York No. 1 Lake Park"
},
{
key: "2",
name: "Jim Green",
age: 42,
address: "London No. 1 Lake Park"
},
{
key: "3",
name: "Joe Black",
age: 32,
address: "Sidney No. 1 Lake Park"
},
{
key: "4",
name: "Bon Jov",
age: 99,
address: "Sidney No. 1 Lake Park"
},
{
key: "5",
name: "User 5",
age: 99,
address: "Sidney No. 1 Lake Park"
},
{
key: "6",
name: "User 6",
age: 99,
address: "Sidney No. 1 Lake Park"
}
];
const rowSelection = {
getCheckboxProps: (record) => {
const rowIndex = data.findIndex((item) => item.key === record.key);
return {
disabled: rowIndex > 1 //enable first 2 rows only
};
}
};
Here is a sample of disabling the first 4 rows:
const rowSelection = {
getCheckboxProps: (record) => {
const rowIndex = data.findIndex((item) => item.key === record.key);
return {
disabled: rowIndex < 4 //disable the first 4 rows only
};
}
};
Here's a working sample:

Related

How to filter a column that has multiple data in Material-table in React?

I have been struggling to set a filter a column that has multiple data in Material-table.
The data is below:
data: [
{
id: 1,
staffName: "John",
role: "1"
},
{
id: 2,
staffName: "Smith",
role: "1,2"
},
{
id: 3,
staffName: "Alex",
role: "2"
}
],
role: {
1: "administrator",
2: "sales"
}
};
The column is below:
const columns = [
{ title: "Id", field: "id" , filtering: false},
{ title: "Name", field: "staffName" , filtering: false},
{
title: "Role",
field: "role",
lookup: roles,
render: (data) =>
data.role
.split(",")
.map((s) => roles[s])
.join(", ")
}
];
I want to show John and Smith when administrator is checked to filter in Role,
but it shows only John.
Similarly, when sales is checked only Alex shows up(I want to show both Smith and Alex).
How can I change the filter including data checked?
Link to code sandbox here

Antd table sorting ignores children

I am trying to sort a table that has children, but it ignores the value "Age" of the children. All items including children should sort in Ascending or descending order. Not sure if this is a bug or my implementation is incorrect.
Codesanbox: https://codesandbox.io/s/hardcore-paper-nvodp
Current Result, Children are ignored. The idea is too sort every item in the tree.
js:
const columns = [
{
title: "Name",
dataIndex: "name",
key: "name"
},
{
title: "Age",
dataIndex: "age",
key: "age",
width: "12%",
sorter: {
compare: (a, b) => a.age - b.age,
multiple: 3
}
},
{
title: "Address",
dataIndex: "address",
width: "30%",
key: "address"
}
];
const data = [
{
key: 1,
name: "John Brown sr.",
age: 60,
address: "New York No. 1 Lake Park",
children: [
{
key: 11,
name: "John Brown",
age: 42,
address: "New York No. 2 Lake Park"
}
]
},
{
key: 2,
name: "Joe Black",
age: 22,
address: "Sidney No. 1 Lake Park",
children: [
{
key: 10,
name: "John Brown",
age: 12,
address: "New York No. 2 Lake Park"
}
]
}
];
function onChange(pagination, filters, sorter, extra) {
console.log("params", extra);
}
function TreeData() {
return (
<>
<Table
columns={columns}
dataSource={data}
onChange={onChange}
indentSize={0}
/>
</>
);
}
You can not resort parent and children in the same tree, so that all elements are sorted by age.
This wouldn't be a tree any longer and does not make sense at all. You would lost the parent - child relation.
If you want to sort all elements by age, you have to move the items into a list and sort them there.
You can only sort the items at the same level and that is what #edo9k 's implementation does correctly!
Tree sorting works as expected by age at for every tree level:
parents [22, 61, 62]
children1 [12]
children2 [10, 30, 40]
children3
[41, 42]
Best way you can achieve this through AntD is by using the Nested Tables component. You can display your expandable/children data for each row in your main table into this nested table and then by incorporating the sortable functions in your nested table you can sort that data.

React ant design table cell collapse issue

Im using my react type script project for ant design table
i want to know how to do that following image like as table, im search any tutorial but not seen anything, any one know how to do that correctly.
code sand box here
Thanks
image here
code here
class App extends React.Component {
columns: any = [
{
title: "Name",
dataIndex: "name",
key: "name"
},
{
title: "Age",
dataIndex: "age",
key: "age"
},
{
title: "Address",
dataIndex: "address",
key: "address"
},
{
title: "Tags",
key: "tags",
dataIndex: "tags"
},
{
title: "Action",
key: "action"
}
];
data: any = [
{
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"]
}
];
render() {
return <Table columns={this.columns} dataSource={this.data} />;
}
}
You want to create 2 sub rows in each row, but only for some columns. you can use rowspan for this.
you can duplicate your rows (row1-row1-row2-row2-row3-row3-...), and put the subrow values in them (row1_subrow1-row1_subrow2-row2_subrow1-row2_subrow2-...), then use rowspan for the columns you want to expand (like Section and Name in your image), and expand the odd rows and collapse the even rows for this columns.
the full code : (Codesandbox Demo)
import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Table } from "antd";
let multiRowRender = (value, row, index) => {
const obj = {
children: value,
props: {}
};
if (index % 2 === 0) {
obj.props.rowSpan = 2;
}
if (index % 2 === 1) {
obj.props.rowSpan = 0;
}
return obj;
};
const columns = [
{
title: "Number",
dataIndex: "number"
},
{
title: "Issue",
dataIndex: "issue"
},
{
title: "Name",
dataIndex: "name",
render: multiRowRender
},
{
title: "Section",
dataIndex: "section",
render: multiRowRender
}
];
let data = [
{
key: "1",
name: "John Brown",
issues: [32, 100],
numbers: [18889898989, 545054],
section: "sec 1"
},
{
key: "2",
name: "Jim Green",
issues: [42, 50],
numbers: [18889898888, 1420054],
section: "sec 2"
}
];
let data2 = [];
data.forEach(d => {
data2.push({ ...d, issue: d.issues[0], number: d.numbers[0] });
data2.push({
...d,
issue: d.issues[1],
number: d.numbers[1],
key: d.key + "-row2"
});
});
data = data2;
ReactDOM.render(
<Table columns={columns} dataSource={data} bordered />,
document.getElementById("container")
);
Codesandbox Demo

Units for specifying width (of a table column) in Antd?

We are using the latest stable version 2 of Antd (and cannot make the jump to version 3 due to version dependency constraints). How do we specify the width of my table columns, ideally to the length of the text in title property? According to the official table component docu, there is the obvious property width for a column. All the documentation says in English is
Property: width
Description: Width of this column
Type: string|number
Default: -
1st part of the question: If I specify only a number, what unit is used? I tried different things, but it does not really look like it is dp as described e.g at What is the default unit for width, height, padding etc in React Native for iOS?
2nd part: If I specify a string, which units can I use? I tried ex, em, and px and none of them seems to work.
The 3rd part of the question: Maybe we overlooked a property of the table tag which overwrites or limits the usage of width is a column?
Any input is appreciated, already the translation of the original documentation to English would help. Or explaining the a related Github issue: 'how to set table columns width? the width property is invalid'.
Thanks for your time in advance - a puzzled Antd newbie.
From the antd official document:
import { Table } from 'antd';
const columns = [{
title: 'Name',
dataIndex: 'name',
key: 'name',
width: '40%',
}, {
title: 'Age',
dataIndex: 'age',
key: 'age',
width: '30%',
}, {
title: 'Address',
dataIndex: 'address',
key: 'address',
}];
const data = [{
key: 1,
name: 'John Brown sr.',
age: 60,
address: 'New York No. 1 Lake Park',
children: [{
key: 11,
name: 'John Brown',
age: 42,
address: 'New York No. 2 Lake Park',
}, {
key: 12,
name: 'John Brown jr.',
age: 30,
address: 'New York No. 3 Lake Park',
children: [{
key: 121,
name: 'Jimmy Brown',
age: 16,
address: 'New York No. 3 Lake Park',
}],
}, {
key: 13,
name: 'Jim Green sr.',
age: 72,
address: 'London No. 1 Lake Park',
children: [{
key: 131,
name: 'Jim Green',
age: 42,
address: 'London No. 2 Lake Park',
children: [{
key: 1311,
name: 'Jim Green jr.',
age: 25,
address: 'London No. 3 Lake Park',
}, {
key: 1312,
name: 'Jimmy Green sr.',
age: 18,
address: 'London No. 4 Lake Park',
}],
}],
}],
}, {
key: 2,
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
}];
// rowSelection objects indicates the need for row selection
const rowSelection = {
onChange: (selectedRowKeys, selectedRows) => {
console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
},
onSelect: (record, selected, selectedRows) => {
console.log(record, selected, selectedRows);
},
onSelectAll: (selected, selectedRows, changeRows) => {
console.log(selected, selectedRows, changeRows);
},
};
ReactDOM.render(
<Table columns={columns} rowSelection={rowSelection} dataSource={data} />
, mountNode);

How might I uncheck all rows of MaterialTable(mbrn/material-table) with a button?

I am building a reactjs web app where I use mbrn/material-table(
https://github.com/mbrn/material-table) .The table has a functionality to check all rows in a field at once.But to unselect(or uncheck) all rows ,you need to click on the select all checkbox and then click on it again to uncheck all rows.I have read the documentation of the framework but have not been able to find a functionality implement unchecking all rows at once with a button.
import MaterialTable from "material-table";
const table = () => {
return (
<MaterialTable
columns={[
{ title: "ID" },
{ title: "name" },
{ title: "SurName" },
{
title: "Birthyear"
},
{ title: "BirthCity" },
{
title: "Sex"
},
{
title: "Type"
}
]}
data={[
{
id: 1,
name: "a",
surname: "Baran",
birthYear: 1987,
birthCity: 63,
sex: "Male",
type: "adult"
},
{
id: 2,
name: "b",
surname: "Baran",
birthYear: 1987,
birthCity: 34,
sex: "Female",
type: "adult",
parentId: 1
},
{
id: 3,
name: "c",
surname: "Baran",
birthYear: 1987,
birthCity: 34,
sex: "Female",
type: "child",
parentId: 1
},
{
id: 4,
name: "d",
surname: "Baran",
birthYear: 1987,
birthCity: 34,
sex: "Female",
type: "child",
parentId: 3
},
{
id: 5,
name: "e",
surname: "Baran",
birthYear: 1987,
birthCity: 34,
sex: "Female",
type: "child"
},
{
id: 6,
name: "f",
surname: "Baran",
birthYear: 1987,
birthCity: 34,
sex: "Female",
type: "child",
parentId: 5
}
]}
actions={[
{
tooltip: "Remove All Selected Users",
icon: icons,
onClick: (evt, data) =>
alert("You want to delete " + data.length + " rows")
}
]}
// onSelectionChange={rows =>
// // alert("You selected " + rows.length + " rows")
// }
options={{
selection: true
}}
parentChildData={(row, rows) => rows.find(a => a.id ===
row.parentId)}
title="Search Results"
/>);
I want that on click of a button,all selected rows should get unselected
Since material table actually mutates your data, you can just use something like this:
this.state.data.forEach(d => {if(d.tableData)d.tableData.checked = false});
and set that as the new state. That is not the prettiest, because of the state mutation, but it is the only way, since material table itself mutates it.
You can use onAllSelected function from the tableRef like that
import MaterialTable from "material-table";
import { useRef } from 'react';
const Table = () => {
const tableRef = useRef()
return (
<>
<button onClick={
() => {tableRef.current?.onAllSelected(false)}}>
clear selection
</button>
<MaterialTable {...all props you need} tableRef={tableRef} />
</>
);
The issue was raised here.

Resources