简体   繁体   English

使用 react redux 和 firebase 按值过滤表

[英]filter table by value with react redux and firebase

I am trying to filter a table in a react component by a value inside a document in firebase.我正在尝试通过 firebase 中文档内的值过滤 react 组件中的表。

The important code looks like this.重要的代码看起来像这样。 Claims is being parsed into another component for the table rows.声明正在被解析为表行的另一个组件。

class Claims extends Component {
  componentDidMount() {
    this.props.getClaims();
  }
  render() {
    const { Claims, loading } = this.props.data;
    let recentClaimsMarkup = !loading ? (
      Claims.map(Claim => <ClaimsTable Claim={Claim} />)
    ) : (
      <p>Loading...</p>
    );
    return (
      <Grid>
        <Paper className = {mergeClasses.root}>
          <Typography> {this.props.match.params.Project} Claim's</Typography>{" "}
        </Paper>
        <Paper className={mergeClasses.root}>
          <Table className={mergeClasses.table} aria-label="simple table">
            <TableHead>
              <TableRow>
                <TableCell>Claim ID</TableCell>
                <TableCell align="right">Date received</TableCell>
                <TableCell align="right">Progress</TableCell>
                <TableCell>Edit Claim</TableCell>
              </TableRow>
            </TableHead>
            <TableBody>{recentClaimsMarkup}</TableBody>
          </Table>
        </Paper>

I am wanting to filter it by the url Project, shown below我想通过 url 项目过滤它,如下所示

{this.props.match.params.Project}

This is due to the claims being tied to the project name.这是因为声明与项目名称相关联。

If this was in Python I would just use a for loop or something similar, but a little bit confused how to filter what Claims are able to be mapped.如果这是在 Python 中,我将只使用 for 循环或类似的东西,但有点困惑如何过滤可以映射的 Claims 。

Basically want it to be, for claims with Project == {this.props.match.params.Project}: map claims.基本上希望它是,对于带有 Project == {this.props.match.params.Project} 的声明:地图声明。

Appreciate any help :)感谢任何帮助:)

Thanks谢谢

So basically filter also returns a new copy of array and you can read more here , but the problem is you can't create the jsx element, if you want to know more you can read this question所以基本上 filter 也返回一个新的数组副本,你可以在这里阅读更多,但问题是你不能创建 jsx 元素,如果你想了解更多,你可以阅读这个问题

so in your case you need to first filter and then you need to render the jsx element using map like shown below所以在你的情况下,你需要先过滤,然后你需要使用地图渲染 jsx 元素,如下所示

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Route } from "react-router-dom";

import "./styles.css";

const User = props => {
  const claimsUserHasAccess = props.data.claims.filter(
    claim => claim.project === props.match.params.access
  );
  return (
    <>
      {claimsUserHasAccess.map(claim => (
        <p key={claim.name}>{claim.name}</p>
      ))}
    </>
  );
};

const userData = {
  claims: [
    { project: "enabled", name: "job" },
    { project: "enabled", name: "nick" },
    { project: "disabled", name: "jef" }
  ]
};

function App() {
  return (
    <div className="App">
      <BrowserRouter>
        <Route
          path="/user/:access"
          render={props => <User data={userData} {...props} />}
        />
      </BrowserRouter>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

in the url you need to enter enabled or disabled to see the users.在 url 中,您需要输入启用或禁用才能查看用户。 I hope this will give a better view of your problem.我希望这能让您更好地了解您的问题。

working codesandbox工作代码沙盒

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

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