过滤搜索结果做出反应

时间:2019-01-11 17:38:08

标签: html reactjs

我从搜索结果返回的数据具有以下列:enTitle,Image,url,enDescription,HasLandingPage,AddInfo。

我想按AddInfo过滤搜索结果以显示在不同的列表中。以后如果我可以添加一个更好的按钮。

渲染数据:

const ListArticle = (props) =>{
    return (
        <div className="card">
            <div className="search-img-lft">
                <a href={props.link} target="_blank">
                    <img src={props.image} alt="" />
                </a>
            </div>
            <div className="search-imgcont-rgt">
                <a href={props.link} target="_blank">
                    <h3>
                        {props.title}
                        {props.kind} // just to see if kind works
                    </h3>
                    <p>{props.desc}</p>
                </a>
                <a href={props.link} target="_blank" className="default-link">{props.link}</a>
            </div>
        </div>
    );
}

列表类别:(忽略它们用于分页的i,brac和lim)

    class List extends React.Component {
        render(){
            const liArt =[];
            const searchText = this.props.searchText.toLowerCase().replace(/[^a-z0-9]/g, '');
            var i = 0;
            const brac = this.props.start;
            const lim = brac + this.props.qtyPerPage;
//the filter below works for resources but I want all to be filtered and show in the list in previous code snippet
                this.props.list.filter(u=>u.AddInfo == "resource").map((article)=>{
                        var artText = (article.enTitle + " " + article.URL + " " + article.enDescription + " " + article.AddInfo).toLowerCase().replace(/[^a-z0-9]/g, '');
                        if(artText.indexOf(searchText)===-1){
                            return;
                        }
                        i++;
                        if(brac<i && i<lim){
                            liArt.push(
                                    <ListArticle key={article.Image+article.URL} 
                                        title={article.enTitle}
                                        image={article.Image+"?h=100&mode=crop&scale=down"}
                                        link={JSON.stringify(article.HasLandingPage).toUpperCase()=="TRUE" ? "/en/"+article.URL : "/" + article.URL}
                                        desc={article.enDescription}
                                        kind={article.AddInfo.includes("SKU") ? " Product" : (article.AddInfo.includes("resource") ? " Resource" : " Page")} />
                            );//push
                        } //limit check  
                });//map
            return (
                <div className="search-page-listbox">
                    {liArt}
                </div>
                );
   }
}

2 个答案:

答案 0 :(得分:0)

我将从您的List类中返回类似的内容(我尝试在代码内的注释中解释我的思维过程):

return (<React.Fragment>
    {
        // second (kinda), I'd convert the inside generated collection (object) into an array
        // -> where the array elements are now ["AddInfo type", [array of elements with that type]] 
        Object.entries(
            // first, convert the list into an object, collecting each type of "AddInfo" into
            // -> a unique property, and storing all objects w/ that type in an array
            this.props.list.reduce((output, u) => {
                if (!output[u.AddInfo]) output[u.AddInfo] = [];
                output[u.AddInfo].push(u);
                return output
            }, {})
        )
        // third, I'd map the new array of ["AddInfo type", [array of elements with that type]] into 
        // -> the jsx output you want, like this:
        .map(([type, array]) => {
            return <div className="search-page-listbox">
                {array.map((article, i) => {
                    // ... everything inside your ".map((article...)" function can go here
                })}
            </div>
        })
    }
</React.Fragment>)

一些注意事项:

  1. 您可以将var i = 0i++行替换为i函数中第二个参数自动生成的map索引(请参阅我的{{1} })
  2. 如果您还没有听说过数组解构之类的事情(例如:array.map((article, i) => ...),请告诉我。您可以采取一些时髦的动作来保存一些行。
  3. 我的第一步是弄清楚如何创建一个基于.map(([type, array]) => ...)保存排序数据的对象容器-这就是为什么我的AddInfo注释在技术上在// first之后出现的原因。希望有道理。
  4. 让我知道您是否有疑问或是否有打字错误破坏了我的代码。由于没有您的反应代码/变量,因此我尚未进行明显测试。

答案 1 :(得分:0)

如果我答对了,您想创建多个列表,同时每个列表显示另一个“ AddInfo”的项目。

首先,我建议将您的任务分为三个部分(而不是两个): 第一个组件是ListArticle,它将成为列表项, 第二个是组件List->,它将接收要显示的列表(在过滤之后), 最后一个组件将是ListContainer->这个组件将包含多个列表(与AddInfo的选项一样多)。

然后,您可以在ListContainer中遍历所有唯一的AddInfo,并为每个选项创建List组件-仅传递经过过滤的项目:

ListArticle.js

import React from 'react';

const ListArticle = (props) =>{
    return (
        <div className="card">
            <div className="search-img-lft">
                <a href={props.link} target="_blank">
                    <img src={props.image} alt="" />
                </a>
            </div>
            <div className="search-imgcont-rgt">
                <a href={props.link} target="_blank">
                    <h3>
                        {props.title}
                        {props.kind} // just to see if kind works
                    </h3>
                    <p>{props.desc}</p>
                </a>
                <a href={props.link} target="_blank" className="default-link">{props.link}</a>
            </div>
        </div>
    );
}

export default ListArticle;

List.js

import React from 'react';
import ListArticle from './ListArticle';

export default class List extends React.Component {
    render() {
        return (
            this.props.list.map(article => <ListArticle key={article.Image + article.URL}
                title={article.enTitle}
                image={article.Image + "?h=100&mode=crop&scale=down"}
                link={JSON.stringify(article.HasLandingPage).toUpperCase() == "TRUE" ? "/en/" + article.URL : "/" + article.URL}
                desc={article.enDescription}
                kind={article.AddInfo.includes("SKU") ? " Product" : (article.AddInfo.includes("resource") ? " Resource" : " Page")} />
            )
        )
    }
}

ListContainer.js

import React from 'react';
import List from './List';


class ListContainer extends React.Component {

    constructor(props){
        super(props);
    }

    render() {

        let lists = {};
        let searchText = this.props.searchText;
        if(this.props){
            let filteredList = this.props.list.filter(article=>(article.enTitle + " " + article.URL + " " + article.enDescription + " " + article.AddInfo).toLowerCase().replace(/[^a-z0-9]/g, '').indexOf(searchText)!==-1);
            filteredList && filteredList.forEach(u => {
                if(lists[u.AddInfo]===undefined) lists[u.AddInfo]=[];
                lists[u.AddInfo].push(u);
            });
        }

        return(
            Object.keys(lists).map(function(key, index) {
                return <List list={lists[key]} />
             })
        )
    }
}

export default ListContainer;

用法:

<ListContainer list={list} searchText={searchText} />

希望对您有所帮助:)