简体   繁体   English

REACT-重定向链接不起作用并在表上应用过滤器

[英]REACT- Redirection Link not working and applying filters on table

I have my table that displays all the id and title of my menu (1) (see menus.json).我的表格显示了我的菜单 (1) 的所有 id 和标题(请参阅 menus.json)。 I want to add an hyperlink to my title but it doesn't work:我想为我的标题添加一个超链接,但它不起作用:

{
    Header: "Title",
    // accessor: "title"
    accessor: (row) => row.title,
    // Link the content of my cell. NOT WORKING 
    Cell: ({ row }) => (
      <Link to={{ pathname: `/menu/${row.id}` }}>{row.title}</Link>
    )
}

By clicking on my title name,ex Menu1 (1), it will redirect me to a new page with a table that contains all the names of the menus that match my id_menu (2) .通过单击我的标题名称,例如 Menu1 (1),它会将我重定向到一个新页面,其中包含一个包含与我的 id_menu (2) 匹配的所有菜单名称的表格。

Here the sandbox link: https://codesandbox.io/s/frosty-tree-ggwuty?这里是沙盒链接: https://codesandbox.io/s/frosty-tree-ggwuty?

Thanks for your time.谢谢你的时间。

The Cell appears to used for formatting of a value. Cell似乎用于格式化值。 accessor property is only telling the hook how to access and read values from your data. accessor属性只是告诉钩子如何访问和读取数据中的值。

  • Cell: Function | React.Component => JSX单元格: Function | React.Component => JSX Function | React.Component => JSX
    • Optional可选的
    • Defaults to ({ value }) => String(value)默认为({ value }) => String(value)
    • Receives the table instance and cell model as props接收表格实例和单元模型作为道具
    • Must return valid JSX必须返回有效的 JSX
    • This function (or component) is primarily used for formatting the column value, eg.此函数(或组件)主要用于格式化列值,例如。 If your column accessor returns a date object, you can use a Cell function to format that date to a readable format.如果您的列访问器返回一个日期对象,您可以使用Cell函数将该日期格式化为可读格式。

Emphasis is mine.重点是我的。

Use the accessor property to read the row value and return the value you want to access in the Cell function.使用accessor属性读取行值并返回要在Cell函数中访问的值。 You can either return the entire row value, or create a new object with the specific properties you want to use to render.您可以返回整个row值,也可以使用要用于渲染的特定属性创建一个新对象。 In the Cell function access the value property and return the JSX, ie computed Link component.Cell函数中访问value属性并返回 JSX,即计算的Link组件。

const data = useMemo(
  () => [
    {
      Header: "Id",
      // accessor: "id"
      accessor: (row) => row.id
    },
    {
      Header: "Title",
      // accessor: "title"
      accessor: (row) => ({ title: row.title, id: row.id }),
      Cell: ({ value }) => (
        <Link to={{ pathname: `/menu/${value.id}` }}>{value.title}</Link>
      )
    }
  ],
  []
);

You will need to also import and use a router component and create routes to render each different table you want to render ( ie one for each view you shared ).您还需要导入和使用路由器组件并创建路由来渲染您想要渲染的每个不同的表(即,您共享的每个视图一个)。

For the purpose of getting the existing Link you were trying to use working I only wrapped the Display component in a BrowserRouter .为了获取您尝试使用的现有Link ,我仅将Display组件包装在BrowserRouter中。 You'll need to create the routes you actually want to render different table content into.您需要创建实际想要将不同表格内容呈现到其中的路由。

编辑 react-redirection-link-not-working-and-applying-filters-on-table

单击“鸡肉招标”导航到“/menu/Menu2”

Build out routes for each table you want to display.为要显示的每个表构建路线。

Example:例子:

App应用程序

<Router>
  <Routes>
    <Route path="/menu" element={<Display />} />
    <Route path="/menu/:menuId" element={<MenuDisplay />} />
    <Route path="*" element={<Navigate to="/menu" replace />} />
  </Routes>
</Router>

MenuDisplay菜单显示

function MenuDisplay() {
  const { menuId } = useParams();
  const { match } = JsonData;

  const matchData = match.find((el) => el._id_menu === menuId)?._ids ?? [];

  const data = useMemo(
    () => [
      {
        Header: "Name",
        accessor: (row) => row.name
      },
      {
        Header: "Description",
        accessor: (row) => row.description
      },
      {
        Header: "Dishes",
        accessor: (row) => row.dishes,
        Cell: ({ value }) => Object.values(value[0]).join(", ")
      }
    ],
    []
  );

  const initialState = {
    sortBy: [
      { desc: false, id: "id" },
      { desc: false, id: "description" },
      { desc: false, id: "dishes" }
    ]
  };

  return (
    <Table
      data={matchData}
      columns={data}
      initialState={initialState}
      withCellBorder
      withRowBorder
      withSorting
      withPagination
    />
  );
}

编辑 react-redirection-link-not-working-and-applying-filters-on-table (分叉)

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

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