【问题标题】:Material Table Search doesn't work for custom column材料表搜索不适用于自定义列
【发布时间】:2021-03-08 00:47:01
【问题描述】:

伙计们,

我有一个垫表,其中有一个自定义列作为 "Items" ,它可以有多个值。 我可以将其渲染为逗号分隔值,但是为了更好的外观和感觉,我将每个项目渲染为芯片。

现在的问题是 mat table 默认搜索不适用于该字段。

任何帮助将不胜感激。

import React, { Fragment, useState } from "react";
import MaterialTable from "material-table";
import { Box, makeStyles, Chip } from "@material-ui/core";

const useStyles = makeStyles((theme) => ({
    chip: {
      margin: 2
    },
    noLabel: {
      marginTop: theme.spacing(3)
    },
  }));
  

const originalData = [
  {
    id: "1",
    productName: "Meat",
    items: [
        {id : 1, name : "chicken"},
        {id : 2, name : "pork" },
        {id : 3, name : "lamb" }
        ]
    
  },
  {
    id: "2",
    productName: "Vegetables",
    items: [
        {id : 1, name : "Okra"},
        {id : 2, name : "Pumpkin" },
        {id : 3, name : "Onion" }
        ]
    
  },

];

export default function MatTableTest(props) {
  const [data, setData] = useState(originalData);
  const classes = useStyles();
  const tableColumns = [
    { title: "id", field: "id", editable : "never" },
    { title: "Product Name", field: "productName" , editable : "never" },
    { title: "Item", field: "items", editable : "never", 
    render: rowData => {
        return (
          <Box className="box" id="style-7">
            { rowData.items.map((exprt) => <Chip
              key={exprt.id}
              label={exprt.name}
              className={classes.chip}
            />)}
          </Box>
        );
      }
},

    
  ];

  return (
    <Fragment>
      <MaterialTable
        columns={tableColumns}
        data={data}
        title="Material Table - Custom Colum  Search"
      />
    </Fragment>
  );
}


【问题讨论】:

    标签: reactjs material-table


    【解决方案1】:

    有一个名为customFilterAndSearch 的道具,您可以在其中定义自定义过滤器功能。您可以在其中定义要匹配的行数据。 Here is an example 来自 Github 存储库中创建的问题。

    【讨论】:

    • 谢谢,工作就像一个魅力,这是我用来解决我的问题的:customFilterAndSearch: (term, rowData) =&gt; { return rowData.items.findIndex(item =&gt; item.name.includes(term)) != -1; }
    • 太棒了。提示:您可能希望toLowerCase() 搜索词和items 使其不区分大小写...
    猜你喜欢
    • 2020-04-27
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    相关资源
    最近更新 更多