简体   繁体   English

在 React 中从 json 文件数据中搜索过滤器

[英]Search Filter from json file data in React

I have built a search filter to React.我已经为 React 构建了一个搜索过滤器。 The JSON file stores the name and the image of the model. JSON 文件存储模型的名称和图像。 There is an error like the name can be displayed on the website but the image is not read.有一个错误,如名称可以显示在网站上,但无法读取图像。 My code is here https://codesandbox.io/s/search-filter-in-reactjs-forked-k8nrf?file=/src/Search/index.js我的代码在这里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;

Can someone stop by to help me display the image on JSON on the website?有人可以过来帮我在网站上的 JSON 上显示图像吗? Thanks a lot非常感谢

Just move the images to your public folder, so that they are not all loaded on startup but only if needed.只需将图像移动到您的公用文件夹,这样它们就不会在启动时全部加载,而仅在需要时加载。

You can just move the whole folder like it is.您可以像原样移动整个文件夹。

<img src will automatically search for images in the public folder, if you add a / in front of the url: <img src会自动搜索public文件夹中的图片,如果你在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"
  }
]

That's all you need.这就是你所需要的。 Here is your updated sandbox .这是您更新的沙箱

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

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