有没有一种方法可以在物料表中创建带有自动编号的自定义列?

时间:2019-04-10 01:27:20

标签: reactjs material-ui material-table

我想生成一个基于api数据的自定义列,即使删除行,该数据也能保持顺序。类似于下面的图片,但不包含硬编码的数字。我要引用的库不是angular-material-table,而是这个react-table

enter image description here

这是我正在使用的代码:

import React from "react";
import ReactDOM from "react-dom";
import MaterialTable from "material-table";
import Check from "@material-ui/icons/Check";
import Clear from "@material-ui/icons/Clear";
import Delete from "@material-ui/icons/Delete";
import Add from "@material-ui/icons/Add";
import Edit from "@material-ui/icons/Edit";
import Search from '@material-ui/icons/Search'
import "./styles.css";

function App() {
  return (
    <div className="App">
      <MaterialTable
        icons={{
          Add: () => <Add />,
          Check: () => <Check />,
          Clear: () => <Clear />,
          ResetSearch: () => <Clear />,
          Delete: () => <Delete />,
          Search: () => <Search />,
          Edit: () => <Edit />
        }}
        columns={[
          { title: "Name", field: "name" },
          { title: "Surname", field: "surname" },
          { title: "Custom", field: "number", type: "numeric" },
          {
            title: "Birth Place",
            field: "birthCity",
            lookup: { 34: "İstanbul", 63: "Şanlıurfa" }
          }
        ]}
        data={[
          { name: "Mehmet", surname: "Baran", number: 1, birthCity: 63 },
          {
            name: "Zerya Betül",
            surname: "Baran",
            number: 2,
            birthCity: 34
          }
        ]}
        editable={{
          onRowAdd: newData => {
            new Promise((resolve, reject) => {
              setTimeout(() => {
                {
                  /* const data = this.state.data;
        data.push(newData);
        this.setState({ data }, () => resolve()); */
                }
                resolve();
              }, 1000);
            });
          },
          onRowUpdate: (newData, oldData) => {
            onClick = () => console.log("hi");

            new Promise((resolve, reject) => {
              setTimeout(() => {
                {
                  /* const data = this.state.data;
    const index = data.indexOf(oldData);
    data[index] = newData;                
    this.setState({ data }, () => resolve()); */
                }
                resolve();
              }, 1000);
            });
          },
          onRowDelete: oldData =>
            new Promise((resolve, reject) => {
              setTimeout(() => {
                {
                  /* let data = this.state.data;
    const index = data.indexOf(oldData);
    data.splice(index, 1);
    this.setState({ data }, () => resolve()); */
                }
                resolve();
              }, 1000);
            })
        }}
      />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);


1 个答案:

答案 0 :(得分:1)

您能尝试一下吗?

如果要使用材料表生成的ID,请查看id字段。但是,如果您需要从1而不是0开始,则可以使用id+1字段

<MaterialTable       
        columns={[
          { title: "Name", field: "name" },
          { title: "Surname", field: "surname" },
          { title: "Id", field: "tableData.id" },
          { title: "Id+1", render: rowData => rowData.tableData.id + 1 },
        ]}
/>