【问题标题】:How to perform inline edit while using react-table or react-table-6?如何在使用 react-table 或 react-table-6 时执行内联编辑?
【发布时间】:2020-11-17 16:39:23
【问题描述】:

我正在尝试使用 react-table-6 库在 React 中执行 CRUD 操作,因为它轻量级并且具有基本功能。我不希望我的捆绑包尺寸更大。

这是我的代码https://codesandbox.io/s/crud-using-react-table-6-rs2r9

每当我尝试使用输入编辑和更改值时,表格都会重新呈现。我认为问题在于,当我更改状态时,列内的单元格会重新呈现。但我无法为此找到解决方法。任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: javascript reactjs react-table react-table-v6


    【解决方案1】:

    我做了一些实验,发现 react-table-6 库不支持 input 元素。

    但是使用 contentEditable div,它似乎可以完美地工作。

    带输入的代码

    import React from "react";
    import "./styles.css";
    import ReactTable from "react-table-6";
    import "react-table-6/react-table.css";
    
    export default function App() {
      const [state, setState] = React.useState({
        data: [
          {
            name: "Adam",
            age: 20
          },
          {
            name: "Eve",
            age: 19
          },
          {
            name: "Abel",
            age: 4
          },
          {
            name: "Coin",
            age: 5
          }
        ],
        edit: {}
      });
    
      return (
          <ReactTable
            data={state.data}
            columns={[
              {
                Header: "Name",
                accessor: "name",
                Cell: ({ value, ...props }) =>
                  state.edit.name === props.original.name ? (
                    <input
                      value={state.edit.name}
                      onChange={(e) => {
                        console.log(e.target.value);
                        setState({
                          ...state,
                          edit: { ...state.edit, name: e.target.value }
                        });
                      }}
                    />
                  ) : (
                    value
                  )
              },
              {
                Header: "Age",
                accessor: "age"
              },
              {
                Cell: ({ value, ...props }) =>
                  Object.keys(state.edit).length > 0 &&
                  state.edit.name === props.original.name ? (
                    <button>Save</button>
                  ) : (
                    <button
                      onClick={(e) => setState({ ...state, edit: props.original })}
                    >
                      Edit
                    </button>
                  )
              }
            ]}
          />
      );
    }
    

    带有内容可编辑 div 的代码

    import React from "react";
    import "./styles.css";
    import ReactTable from "react-table-6";
    import "react-table-6/react-table.css";
    
    export default function App() {
      const [state, setState] = React.useState({
        data: [
          { id: 1, name: "Adam", age: 20 },
          { id: 2, name: "Eve", age: 19 },
          { id: 3, name: "Abel", age: 4 },
          { id: 4, name: "Coin", age: 5 }
        ],
        edit: {}
      });
    
      return (
        <ReactTable
          data={state.data}
          columns={[
            {
              Header: "Name",
              accessor: "name",
              Cell: ({ value, ...props }) => (
                <div
                  style={
                    state.edit.id === props.original.id
                      ? {
                          backgroundColor: "#ddd",
                          padding: "5px",
                          outline: "1px solid #0000ff"
                        }
                      : {}
                  }
                  contentEditable={state.edit.id === props.original.id}
                  suppressContentEditableWarning
                  onBlur={(e) => {
                    setState({
                      ...state,
                      edit: { ...state.edit, name: e.target.innerHTML }
                    });
                  }}
                  dangerouslySetInnerHTML={{
                    __html:
                      state.edit.id === props.original.id ? state.edit.name : value
                  }}
                />
              )
            },
            {
              Header: "Age",
              accessor: "age"
            },
            {
              Cell: ({ value, ...props }) =>
                Object.keys(state.edit).length > 0 &&
                state.edit.id === props.original.id ? (
                  <button
                    onClick={(e) => {
                      let newdata = state.data;
                      newdata.map((d) => {
                        if (d.id === state.edit.id) {
                          d.name = state.edit.name;
                        }
                        return d;
                      });
                      console.log(newdata);
                      setState({ ...state, data: newdata, edit: {} });
                    }}
                  >
                    Save
                  </button>
                ) : (
                  <button
                    onClick={(e) => setState({ ...state, edit: props.original })}
                  >
                    Edit
                  </button>
                )
            }
          ]}
          defaultPageSize={5}
        />
      );
    }
    
    

    【讨论】:

      猜你喜欢
      • 2020-12-28
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 2019-10-06
      相关资源
      最近更新 更多