简体   繁体   English

在材料表中添加自定义添加按钮

[英]Add custom add-button in material-table

Currently I have a simple material-table like this:目前我有一个像这样的简单材料表:

<MaterialTable
    options={myOptions}
    title="MyTitle"
    columns={state.columns}
    data={state.data}
    tableRef={tableRef} // Not working
    editable={{
      onRowAdd: ...,
      onRowDelete: ...,
      onRowUpdate: ...
    }}
  />;

where I'm trying to a create new add button (not edit the current one): each Row in the Bar Column should have a custom add button.我正在尝试创建新的添加按钮(不编辑当前按钮):Bar Column 中的每一行都应该有一个自定义添加按钮。 I've looked through the MaterialTable source code but I couldn't reproduce the code that is used for the default add button which is:我查看了 MaterialTable 源代码,但无法重现用于默认添加按钮的代码:

        calculatedProps.actions.push({
          icon: calculatedProps.icons.Add,
          tooltip: localization.addTooltip,
          position: "toolbar",
          disabled: !!this.dataManager.lastEditingRow,
          onClick: () => {
            this.dataManager.changeRowEditing();
            this.setState({
              ...this.dataManager.getRenderState(),
              showAddRow: !this.state.showAddRow,
            });
          },
        });

in particular I can't get to access the dataManager variable.特别是我无法访问dataManager变量。

当前表

That is how the current table looks like, and I need to add the add button where there is the red sign.这就是当前表格的样子,我需要在有红色标志的地方添加添加按钮。

I think this is what you are looking for:我认为这就是你要找的:

在此处输入图像描述

The Actions column represents the default actions set. Actions列表示默认操作集。 I added an specific button using custom column rendering ( docs ):我使用自定义列渲染 ( docs ) 添加了一个特定按钮:

//..previous columns definition
{
  title: "Custom Add",
  field: "internal_action",
  editable: false,
  render: (rowData) =>
    rowData && (
      <IconButton
        color="secondary"
        onClick={() => addActionRef.current.click()}
      >
        <AddIcon />
      </IconButton>
    )
}

* Using rowData as conditional, prevents from rendering while filling the addition row. *使用 rowData 作为条件,防止在填充添加行时进行渲染。

Then I triggered the add action as shown here :然后我触发了添加操作,如下所示

const MyComponent() {

const addActionRef = React.useRef();

return (
    <>
        <button onClick={() => addActionRef.current.click()}>
            Add new item
        </button>

        <MaterialTable
            //...
            components={{
                Action: props => {
                    //If isn't the add action
                    if (typeof props.action === typeof Function || props.action.tooltip !== 'Add') {
                            return <MTableAction {...props} />
                    } else {
                            return <div ref={addActionRef} onClick={props.action.onClick}/>;
                    }}
                }}
            editable={{
                onRowAdd: (newData, oldData) => Promise.resolve(); //your callback here
            }}
        />
    </>
);
}

I extended the original snippet in order to complete the addition cycle.我扩展了原始片段以完成添加周期。 If you need to handle different types of actions, I think Editable section from the oficial docs would be handy.如果您需要处理不同类型的操作,我认为官方文档中的可编辑部分会很方便。

Hope this works for you!希望这对你有用! Full code and sandbox here :完整代码和沙箱在这里

import React, { Fragment, useState } from "react";
import MaterialTable, { MTableAction } from "material-table";
import AddIcon from "@material-ui/icons/AddAlarm";
import IconButton from "@material-ui/core/IconButton";

export default function CustomEditComponent(props) {
const tableRef = React.createRef();
const addActionRef = React.useRef();

const tableColumns = [
    { title: "Client", field: "client" },
    { title: "Name", field: "name" },
    { title: "Year", field: "year" },
    {
    title: "Custom Add",
    field: "internal_action",
    editable: false,
    render: (rowData) =>
        rowData && (
        <IconButton
            color="secondary"
            onClick={() => addActionRef.current.click()}
        >
            <AddIcon />
        </IconButton>
        )
    }
];

const [tableData, setTableData] = useState([
    {
    client: "client1",
    name: "Mary",
    year: "2019"
    },
    {
    client: "client2",
    name: "Yang",
    year: "2018"
    },
    {
    client: "client3",
    name: "Kal",
    year: "2019"
    }
]);

return (
    <Fragment>
    <MaterialTable
        tableRef={tableRef}
        columns={tableColumns}
        data={tableData}
        title="Custom Add Mode"
        options={{
        search: false
        }}
        components={{
        Action: (props) => {
            //If isn't the add action
            if (
            typeof props.action === typeof Function ||
            props.action.tooltip !== "Add"
            ) {
            return <MTableAction {...props} />;
            } else {
            return <div ref={addActionRef} onClick={props.action.onClick} />;
            }
        }
        }}
        actions={[
        {
            icon: "save",
            tooltip: "Save User",
            onClick: (event, rowData) => alert("You saved " + rowData.name)
        }
        ]}
        editable={{
        onRowAdd: (newData) =>
            Promise.resolve(setTableData([...tableData, newData]))
        }}
    />
    </Fragment>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM