0

我有一个问题:我有一个想要包含在选择复选框中的自定义onClick和功能onChange

我怎样才能做到这一点?这样当有人选择从 MT 生成的复选框时,他们会运行自定义onClickonChange

还想知道为搜索栏添加自动填充的可能性吗?

即您开始输入,它会为您提供与数据中的内容或前 5 个值或数据中的内容相匹配的值,并允许用户通过单击下拉选项来填写自动填充。


  state = {
    items: [],
    chosen: [],
    selected: []
  }

  toggle = (id) => {

    const selected = this.state.selected;

    if (selected.includes(id)) selected.splice(selected.indexOf(id), 1); else selected.push(id);

    this.setState({ selected });
    localStorage.setItem('chosen', JSON.stringify(selected));

    console.log(this.state)

  }

  async componentDidMount() {

    this.setState({
      items: await (await fetch(`${process.env['REACT_APP_API']}/items`)).json(),
      selected: JSON.parse(localStorage.getItem('chosen')) || []
    }); 

  }

render() {
return (

<Fragment>
 <MaterialTable
  columns={[
    { 
      title: 'myChecbox',
      render: rowData => {
        return (
          {/* THIS WORKS AS IT SHOULD  */}
          <input className="toggle_checkbox toggle" type="checkbox" checked={this.state.selected.includes(rowData.id)} onChange={this.toggle.bind(this, rowData.id)} />
        )
      },
      cellStyle: {
        width:20,
        maxWidth:20,
        padding:20
      },
      headerStyle: {
      width:20,
      maxWidth:20,
      padding:20
      }
    },
    { 
      title: 'Logo', field: 'name',
      render: rowData => {
        return (

          <a href={rowData.link} key={rowData.id}>
            <img className="image" src={rowData.logo} alt={rowData.name} />
          </a> 
        )
      }
    },
    { 
      title: 'Name', field: 'name',
      render: rowData => {
        return (

          <a href={rowData.link} key={rowData.id}>
            <div className="NameContainer">
              <h5>{rowData.name}</h5>
              <span>{rowData.type}</span>
            </div>
          </a>

        )
      }
    },
    { 
      title: 'score', field: 'score', 
      cellStyle: data => {
        if (data === "N/A") {
          return {
            color: "#aaa",
          }
        }
      } 
    },
    { title: 'Size', field: 'size' },
  ]}

  data=
    {
      this.state.items.length ? this.state.items.map((item) => (
        { 
          id: item.id,  
          logo: item.Logo.url, 
          name: item.Name,
          type: item.Type.replace(/_/g, ' '),
          score: item.Score, 
          size: item.Size
        }
      )) : [{ name: 'LOADING' }]
    }

  options={{
    maxBodyHeight: 1000,
    grouping: true,
    selection: true,
    pageSize: 10,
    doubleHorizontalScroll: true,

  }}

{/* I CANT GET THIS TO WORK =/ */}
  onRowClick={(event, rowData) => { this.state.selected.includes(rowData.id); }}
  onSelectionChange={data => {this.toggle.bind(this, data.id)}}
 />
</Fragment>

);
}
});
4

3 回答 3

0

onSelectionChange传递选定行的数组。所以数据参数没有id。

对于搜索字段的自动完成,您可以在 github 上打开功能请求问题。

于 2019-04-11T19:41:02.867 回答
0

您不应该直接更改状态,而是使用 setState。

import React, { Fragment, Component } from "react";
import MaterialTable from "material-table";
import ReactDOM from "react-dom";

import "./styles.css";

class App extends Component {
  state = {
    items: [],
    chosen: [],
    selected: []
  };

  toggle = id => {
    const selected = this.state.selected;

    if (selected.includes(id)) selected.splice(selected.indexOf(id), 1);
    else selected.push(id);

    this.setState({ selected });
    localStorage.setItem("chosen", JSON.stringify(selected));

    console.log(this.state);
  };

  async componentDidMount() {
    this.setState({
      items: await (await fetch(
        `${process.env["REACT_APP_API"]}/items`
      )).json(),
      selected: JSON.parse(localStorage.getItem("chosen")) || []
    });
  }

  render() {
    return (
      <Fragment>
        <MaterialTable
          columns={[
            {
              title: "myChecbox",
              render: rowData => {
                return (
                  <input
                    className="toggle_checkbox toggle"
                    type="checkbox"
                    checked={this.state.selected.includes(rowData.id)}
                    onChange={this.toggle.bind(this, rowData.id)}
                  />
                );
              },
              cellStyle: {
                width: 20,
                maxWidth: 20,
                padding: 20
              },
              headerStyle: {
                width: 20,
                maxWidth: 20,
                padding: 20
              }
            },
            {
              title: "Logo",
              field: "name",
              render: rowData => {
                return (
                  <a href={rowData.link} key={rowData.id}>
                    <img
                      className="image"
                      src={rowData.logo}
                      alt={rowData.name}
                    />
                  </a>
                );
              }
            },
            {
              title: "Name",
              field: "name",
              render: rowData => {
                return (
                  <a href={rowData.link} key={rowData.id}>
                    <div className="NameContainer">
                      <h5>{rowData.name}</h5>
                      <span>{rowData.type}</span>
                    </div>
                  </a>
                );
              }
            },
            {
              title: "score",
              field: "score",
              cellStyle: data => {
                if (data === "N/A") {
                  return {
                    color: "#aaa"
                  };
                }
              }
            },
            { title: "Size", field: "size" }
          ]}
          data={
            this.state.items.length
              ? this.state.items.map(item => ({
                  id: item.id,
                  logo: item.Logo.url,
                  name: item.Name,
                  type: item.Type.replace(/_/g, " "),
                  score: item.Score,
                  size: item.Size
                }))
              : [{ name: "LOADING" }]
          }
          options={{
            maxBodyHeight: 1000,
            grouping: true,
            selection: true,
            pageSize: 10,
            doubleHorizontalScroll: true
          }}
          onRowClick={(event, rowData) => {
            this.setState({
              selected: [...this.state.selected, rowData.tableData.id]
            });
          }}
          onSelectionChange={data => {
            this.toggle.bind(this, data.id);
          }}
        />
      </Fragment>
    );
  }
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
于 2019-04-11T19:42:22.370 回答
0

你调用错了,当你在选项中激活选择时,你必须像数组一样使用 dataRow 。所以这里有一个如何使用它的例子 https://codesandbox.io/s/7zz0v3m91x

还有关于 autodfill,它现在在这个表中不可用,但是如果你像在远程模式下使用这个表,你几乎可以有相同的效果,因为它会出现在表中完成的元素

我希望我能帮上忙

于 2019-04-11T19:46:28.507 回答