reactjs - 如何在 mui-datatable React.js 的第一个位置添加 SrNo (#) 列
问题描述
使用 React 和MUI-Datatables创建表:首先需要添加 SrNo 列,还需要添加 Delete 和 IsActive 按钮。
import React, { useState, useEffect } from 'react';
import Grid from '@material-ui/core/Grid';
import Switch from '@material-ui/core/Switch';
import MUIDataTable from "mui-datatables";
export const Services = () => {
const [itemList, setItemList] = useState([]);
const [mainColumns, setMainColumns] = useState([]);
const selectData = async () => {
const response = await ServiceAPI(); // data from web wervice
var data = JSON.parse(response.data);
setItemList(data);
};
const createColumns = () => {
var columns = [];
// how to add Sr No column
columns.push(
{
label: 'ID',
name: 'service_id',
});
columns.push(
{
label: 'Name',
name: 'service_name',
});
setMainColumns(columns);
};
useEffect(() => {
createColumns();
selectData();
}, []);
return (
<>
<h3>Service</h3>
<Grid container >
<Grid item xs={12}>
<MUIDataTable
title={"Service"}
data={itemList}
columns={mainColumns}
className="table nowrap"
/>
</Grid>
</Grid>
</>
);
}
尝试了下面的激活/停用代码,但每次都运行 OnChange(),它应该只调用点击事件(也尝试过 OnClick()):
columns.push(
{
label: 'Active',
name: 'is_active',
options: {
filter: false,
sort: false,
customBodyRender: (value, tableMeta, updateValue) => {
const serviceID = tableMeta.rowData[0];
return (
<Switch color="primary" checked={value} value={value} onChange={activate}/>
/>
);
}
}
});
解决方案
请检查示例。我在这个例子中添加了序列号和删除按钮。
import React from "react";
import ReactDOM from "react-dom";
import MUIDataTable from "mui-datatables";
import IconButton from "@material-ui/core/IconButton";
import Button from "@material-ui/core/Button";
export default class MuiDatatableSerial extends React.Component {
render() {
const columns = [
{name: "sl", label: "Serial No"},
{name: "name", label: "Name"},
{name: "title", label: "Title"},
{name: "location", label: "Location"},
{name: "age", label: "Age"},
{name: "salary", label: "Salary"},
{
name: "delete",
label: "",
options: {
filter: false,
sort: false,
customBodyRender: (value, tableMeta, updateValue) => {
return <Button
color="secondary"
onClick={(ev) =>
alert('deleted')
}> Delete
</Button>;
},
}
}
];
const data = [
{name: "Khabir Uddin", title: "Senior Software Engineer", location: "Dhaka", age: 38, salary: "$150,000"},
{name: "Gabby George", title: "Business Analyst", location: "Minneapolis", age: 30, salary: "$100,000"},
{name: "Aiden Lloyd", title: "Business Consultant", location: "Dallas", age: 55, salary: "$200,000"}
];
let newData = [];
data.map((item, index) => {
newData.push({sl: index + 1, ...item});
});
const options = {
selectableRows: "none",
onRowsSelect: (rowsSelected, allRows) => {
},
};
return (
<MUIDataTable
title={"ACME Employee list"}
data={newData}
columns={columns}
options={options}
/>
);
}
}
推荐阅读
- svg - FreeCAD:导入的 SVG 被取代
- c# - 如何在 Elastic Search NEST 7.x 中设置设置
- javascript - 如果前一列具有行跨度,则将类设置为下一列
- c++ - C++ STL中数组队列的前面在将数组推入队列时发生变化
- php - 我无法使用 count() 集合检查 laravel 6 中的变量中是否有数据
- c++ - 如何在 yaml 文件中发出或编写地图地图?C++
- swiftui - 希望在将 UIKeyboardType.numberPad 与 SwiftUI 一起使用时添加一个完成按钮
- google-kubernetes-engine - GKE 上的 cloudsql-proxy 使用 golan 应用程序返回“dial tcp:lookup 127.0.0.1:3306: no such host”
- sql - 用于创建表的 Redshift SQL 过程
- node.js - 如何使用 yargs 为命令的位置参数提供别名