【问题标题】:Search Filter from json file data in React从 React 中的 json 文件数据中搜索过滤器
【发布时间】:2021-02-12 09:33:59
【问题描述】:

我已经为 React 构建了一个搜索过滤器。 JSON 文件存储模型的名称和图像。出现错误,例如可以在网站上显示名称但无法读取图像。 我的代码在这里https://codesandbox.io/s/search-filter-in-reactjs-forked-k8nrf?file=/src/Search/index.js

import React, { Component } from "react";
import {
  Input,
  Card,
  CardBody,
  CardTitle
} from "mdbreact";

import "./style.css";
import modelList from "./models.json";

class App extends Component {
  state = {
    search: ""
  };

  renderModel = model => {

    return (
      <div className="col-md-3" style={{ marginTop: "20px" }}>
        <Card>
          <CardBody>
            <p className="">
              <img
                src={model.image}
                className={model.image}
                alt={model.name}
              />
            </p>
            <CardTitle title={model.name}>
              {model.name.substring(0, 15)}
              {model.name.length > 15 && "..."}
            </CardTitle>
          </CardBody>
        </Card>
      </div>
    );
  };

  onchange = e => {
    this.setState({ search: e.target.value });
  };

  render() {
    const { search } = this.state;
    const filteredModels = modelList.filter(model => {
      return model.name.toLowerCase().indexOf(search.toLowerCase()) !== -1;
    });

    return (
      <div className="flyout">
        <main style={{ marginTop: "4rem" }}>
          <div className="container">
            <div className="row">
              <div className="col">
                <Input
                  label="Search Model"
                  icon="search"
                  onChange={this.onchange}
                />
              </div>
              <div className="col" />
            </div>
            <div className="row">
              {filteredModels.map(model => {
                return this.renderModel(model);
              })}
            </div>
          </div>
        </main>
       
      </div>
    );
  }
}

export default App;

有人可以帮我在网站上以 JSON 格式显示图像吗?非常感谢

【问题讨论】:

  • 当你想要的是src={model.image}时,你有src={blankImg}。请注意,它们不会正确解析,最好有一个公共目录来存储图像并从那里加载它们
  • 公共目录是什么意思?
  • 你能举个例子吗?
  • 据我所知,消隐似乎不存在。如果我只是自然地制作了一个 ,它什么也不会产生。
  • @AlexanderHemming 我已经更新了代码,无法读取网站上的图像。我该如何解决?

标签: json reactjs search filter


【解决方案1】:

只需将图像移动到您的公用文件夹,这样它们就不会在启动时全部加载,而是仅在需要时加载。

您可以按原样移动整个文件夹。

&lt;img src会自动搜索公用文件夹中的图片,如果你在url前面加上/

[
  {
    "name": "Sample 1",
    "image": "/asset/crab-nebuala.png"
  },
  {
    "name": "Sample 2",
    "image": "/asset/crab-nebula.png"
  },
  {
    "name": "Sample 3",
    "image": "/asset/ghost-nebua.png"
  },
  {
    "name": "Sample 4",
    "image": "/asset/sample-nebula-2.png"
  },
  {
    "name": "Sample 5",
    "image": "/asset/sample-nebula.png"
  }
]

这就是你所需要的。这是您更新后的sandbox

【讨论】:

  • 图像显示无序。如何重新排列框内的图像?
  • 设置宽度和高度,例如50 到 img 组件。如果这解决了您的问题,您能否将其标记为已接受,以便其他人也能找到答案?
  • 你想通过沙盒链接告诉我什么?
  • 如我上面所说,为img组件添加一个宽度和高度。
猜你喜欢
  • 2017-06-24
  • 2020-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多