disabling a row based on dataSource property - antd table - reactjs

In antd table, while rendering the data can i make a row as disabled.
so i have a dataSource so while rendering the table i need to make a table row as disabled based on the dataSource property.
In the dataSource there is a property of enabled, if its false i need to make the row as disabled kind of css.
first i thought of having a className but is there any inbuilt way, i dont want to have the selected kind of checkbox just plane a row to be disabled.
import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import { Table } from "antd";
import "./styles.css";
function App() {
const dataSource = [
{
key: "1",
name: "Mike",
age: 32,
address: "10 Downing Street",
enabled: true
},
{
key: "2",
name: "John",
age: 42,
address: "10 Downing Street",
enabled: false
}
];
const columns = [
{
title: "Name",
dataIndex: "name",
key: "name"
},
{
title: "Age",
dataIndex: "age",
key: "age"
},
{
title: "Address",
dataIndex: "address",
key: "address"
}
];
return (
<>
<Table dataSource={dataSource} columns={columns} />;
</>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Codesandbox
is there any way for this

Was able to find the solution
in App.js
<Table dataSource={dataSource} rowClassName={record => !record.enabled && "disabled-row"}
columns={columns} />;
and in css file
.disabled-row {
background-color: #dcdcdc;
}

Related

React split table column values into different rows

I have a table with columns and basically the data Im sending it, in some columns is another array. If its an array I need to split that into separate rows.
import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Table } from "antd";
const columns = [
{
title: "Classes",
dataIndex: "classes"
},
{
title: "Grades",
dataIndex: "grades"
},
{
title: "Credentials",
dataIndex: ["classMap", "Teacher with credentials"]
},
{
title: "No Credentials",
dataIndex: ["classMap", "Teacher no credentials"]
}
];
const data = [
{
key: "1",
classes: ["Chinese", "Italian"],
grades: ["98", "64"],
classMap: {
"Teacher with credentials": ["School board cred", "Teaching school"],
"Teacher no credentials": ["no credentials"]
}
}
];
ReactDOM.render(
<Table columns={columns} dataSource={data} pagination={false} />,
document.getElementById("container")
);
I made this example. If you look in codepen the data lets say for classes is in the same row. I need them to be split into separate rows

React Data Grid shows bad

I have a problem with ReactDataGrid component. I have already installed react-data-grid. The code is the same as in the reac grid's web:
const columns = [
{ key: 'id', name: 'ID' },
{ key: 'title', name: 'Title' },
{ key: 'count', name: 'Count' }];
const rows = [{ id: 0, title: 'row1', count: 20 }, { id: 1, title: 'row1', count: 40 }, { id: 2, title: 'row1', count: 60 }];
class App extends React.Component {
render() {
return (
<ReactDataGrid
columns={columns}
rowGetter={i => rows[i]}
rowsCount={3}
minHeight={150} />
)
}
}
export default App;
and i get:
Result
Thank you!
Import the CSS like so :
import 'react-data-grid/dist/react-data-grid.css';
It should be fine.
import React from "react";
import ReactDOM from "react-dom";
import ReactDataGrid from "react-data-grid";
const columns = [
{ key: "id", name: "ID", editable: true },
{ key: "title", name: "Title", editable: true },
{ key: "count", name: "Count", editable: true }
];
const rows = [
{ id: 0, title: "row1", count: 20 },
{ id: 1, title: "row1", count: 40 },
{ id: 2, title: "row1", count: 60 }
];
class App extends React.Component {
render() {
return (
<ReactDataGrid
columns={columns}
rowGetter={i => rows[i]}
rowsCount={3}
minHeight={150}
enableCellSelect={true}
/>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
https://codesandbox.io/s/rdg-cell-editing-h8bnr
the answer is in the above code this above code is working
You don't really need to downgrade. The issue is that the css is not being imported.
If you can import css from node-modules, it'll work.
Workaround for me was I took the whole css and we are now self-maintaining the css, making changes when needed.
I couldn't load the css either, I got around this by including
import ReactDataGrid from 'react-data-grid/dist/react-data-grid.min.js';
instead of
import ReactDataGrid from 'react-data-grid';

react-bootstrap-table2 column width change on cell click

I have base react-bootstrap-table code:
import React from 'react'
import BootstrapTable from 'react-bootstrap-table-next';
import cellEditFactory from 'react-bootstrap-table2-editor';
const products = [
{
id: "0",
name: "test",
price: "2100"
},
{
id: "1",
name: "test",
price: "2100"
},
{
id: "2",
name: "test",
price: "2120"
}
];
const columns = [
{
dataField: "id",
text: "Product ID"
},
{
dataField: "name",
text: "Product Name",
},
{
dataField: "price",
text: "Product Price"
}
];
export default class App extends React.Component {
render() {
return (
<BootstrapTable
keyField="id"
data={ products }
columns={ columns }
cellEdit={ cellEditFactory({mode: 'click'})}
/>
)
}
}
My problem is that column changes width, when I click editable cell. I see on live demo that is way to block this effect. live demo
I try avoid this effect by adding css with max width, but it's not problem cell, but whole column:
editorStyle: {
backgroundColor: '#20B2AA',
maxWidth: '30%',
},
You need add .table {table-layout: 'fixed'}.
I was able to resolve this issue by using style props for each column in my table. Specifically, I added the following props:
editorStyle: {width:'100%', overflow:'scroll', padding:'inherit', height:'inherit'},
style: {width:'20%'}
To come up with the width of 20% I had to play around a bit. My table has more than 5 columns, and I found that having my total width across all columns sum to more than 100% gave me good looking columns that didn't expand when editing.

React-Table, each cell goes to a new line, why?

I'm implementing ReactTable, the first example from its documentation, but each cell from the table goes to a new line:
ReactTable
This is my react table component:
import React, {PureComponent} from 'react';
import ReactTable from 'react-table';
export default class ExampleTable extends PureComponent {
render() {
const data = [{
name: 'Tanner Linsley',
age: 26,
friend: {
name: 'Jason Maurer',
age: 23,
}
},{
name: 'Bla bla',
age: 29,
friend: {
name: 'RAra Ra',
age: 98,
}
}]
const columns = [{
Header: 'Name',
accessor: 'name' // String-based value accessors!
}, {
Header: 'Age',
accessor: 'age',
Cell: props => <span className='number'>{props.value}</span> // Custom cell components!
}, {
id: 'friendName', // Required because our accessor is not a string
Header: 'Friend Name',
accessor: d => d.friend.name // Custom value accessors!
}, {
Header: props => <span>Friend Age</span>, // Custom header components!
accessor: 'friend.age'
}]
return <ReactTable
data={data}
columns={columns}
/>
}
}
And this is how I import the component:
import ExampleTable from './tables/example';
class TableView extends Component {
getExample() {
return (
<ExampleTable/>
)
}
render() {
return (
<div>
{this.getExample()}
</div>
);
}
}
Any idea why each cell is going to a new line?
Probably is something stupid I'm just starting with React.
Thanks in advance.
Found it!
It was something stupid, I wasn't importing the ReactTable css:
import "react-table/react-table.css";

How to make table header tooltip work with enabled sorting?

Here is the code (live codesandbox):
import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Table, Tooltip } from "antd";
const columns = [
{
title: <Tooltip title="Address">A</Tooltip>,
dataIndex: "address",
sorter: (a, b) => a.address.length - b.address.length,
render: cell => <Tooltip title={cell}>{cell[0]}</Tooltip>
}
];
const data = [
{
key: "1",
address: "New York No. 1 Lake Park"
},
{
key: "2",
address: "London No. 1 Lake Park"
},
{
key: "3",
address: "Sidney No. 1 Lake Park"
},
{
key: "4",
address: "London No. 2 Lake Park"
}
];
ReactDOM.render(
<Table columns={columns} dataSource={data} />,
document.getElementById("container")
);
When I hover table header, it shows plain tooltip instead of showing antd Tooltip:
However after disabling sorter the tooltip displays correctly:
How to make Tooltip and sorter work together?
Looks like sorter applies ant-table-column-sorters css class, which causes css to overlap the Tooltip.
A hacky way to fix this could be: apply title as a function, and override css:
const columns = [
{
title: () => <Tooltip title="Address">A</Tooltip>, // note it's func
// apply custom css class, so we can track it
className: "sorter-no-tooltip",
dataIndex: "address",
sorter: (a, b) => a.address.length - b.address.length,
render: cell => <Tooltip title={cell}>{cell[0]}</Tooltip>
}
];
and in index.css:
.sorter-no-tooltip .ant-table-column-sorters:before {
content: none !important;
}
Actually it looks like an framework issue, so it could be submitted.
Antd now supports a property for hiding sorter tooltip.
showSorterTooltip
This option is supported for Table as a whole or at column level.
This issue is fixed, you can update version to 3.12.2. And apply title as a function:
title: () => (your ReactNode),

Resources