物料表使行在单击时可编辑

时间:2020-06-11 03:44:37

标签: javascript reactjs material-ui material-table

使用material-table库,我试图使表行在双击时可编辑。单击该行应与单击“操作”列最左侧的“编辑”按钮具有相同的效果。我已经成功链接到正确的事件处理程序,现在双击一行时,该警告处理程序由警报框表示。

https://codesandbox.io/s/lucid-microservice-73iq8?file=/src/App.js:0-1203

import React from "react";
import MaterialTable, { MTableBodyRow } from "material-table";

export default function App() {
  return (
    <MaterialTable
      columns={[
        { title: "Adı", field: "name" },
        { title: "Soyadı", field: "surname" },
        { title: "Doğum Yılı", field: "birthYear", type: "numeric" },
        {
          title: "Doğum Yeri",
          field: "birthCity",
          lookup: { 34: "İstanbul", 63: "Şanlıurfa" }
        }
      ]}
      components={{
        Row: props => (
          <MTableBodyRow
            {...props}
            onDoubleClick={() => {
              alert("Make row editable");
            }}
          />
        )
      }}
      editable={{
        onRowAdd: newData =>
          new Promise((resolve, reject) => {
            resolve();
          }),
        onRowUpdate: (newData, oldData) =>
          new Promise((resolve, reject) => {
            resolve();
          }),
        onRowDelete: oldData =>
          new Promise((resolve, reject) => {
            resolve();
          })
      }}
      data={[
        { name: "Mehmet", surname: "Baran", birthYear: 1987, birthCity: 63 }
      ]}
      title="Demo Title"
    />
  );
}

1 个答案:

答案 0 :(得分:2)

这是一个纯粹的hack,没有可以触发的事件,

因此,我只是深入研究object的核心以查找事件触发器,然后在props.actions内找到了它

props.actions包含actions的数组,例如addeditdelete,因此通过引用它可以触发来自它。

这是它的代码段,看看:

<MTableBodyRow
    {...props}
    onDoubleClick={(e) => {
        console.log(props.actions) // <---- HERE : Get all the actions
        props.actions[1]().onClick(e,props.data); // <---- trigger edit event
        alert("Make row editable");
    }}
/>

工作演示

Edit #SO-material-onclick-edit