欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

react如何實(shí)現(xiàn)篩選條件組件

 更新時(shí)間:2023年10月23日 15:02:38   作者:拇指風(fēng)云  
這篇文章主要介紹了react如何實(shí)現(xiàn)篩選條件組件問(wèn)題,具有很好的參考價(jià)價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

react實(shí)現(xiàn)篩選條件組件

篩選條件組件點(diǎn)全部時(shí)選中全部,點(diǎn)其他時(shí)全部取消,點(diǎn)擊查詢(xún)時(shí)將某個(gè)類(lèi)型下的選擇項(xiàng)傳出

screenCondition.js文件

/**
 *
 * title: screenCondition.js
 *
 * author: WangPei.
 *
 * date: 2017/5/18.
 *
 */
import React,{Component} from "react"
import ScreenConditionItem from "../../component/screenConditionItem/screenConditionItem"
export default class ScreenCondition extends Component{
    constructor(props){
        super(props);
        this.state={
            screenConditionData:[
                {
                    "screenTypeId": "1",
                    "screenTypeName": "渠道類(lèi)型",
                    "values": [
                        {
                            "sid": "-1",
                            "sname": "全部"
                        },
                        {
                            "sid": "01",
                            "sname": "線(xiàn)下實(shí)體"
                        },
                        {
                            "sid": "02",
                            "sname": "線(xiàn)上電商"
                        },
                        {
                            "sid": "03",
                            "sname": "集客渠道"
                        },
                        {
                            "sid": "04",
                            "sname": "其他"
                        }
                    ]
                },
                {
                    "screenTypeId": "2",
                    "screenTypeName": "合約類(lèi)型",
                    "values": [
                        {
                            "sid": "-1",
                            "sname": "全部"
                        },
                        {
                            "sid": "01",
                            "sname": "線(xiàn)下實(shí)體2"
                        },
                        {
                            "sid": "02",
                            "sname": "線(xiàn)上電商2"
                        },
                        {
                            "sid": "03",
                            "sname": "集客渠道2"
                        },
                        {
                            "sid": "04",
                            "sname": "其他2"
                        }
                    ]
                }
            ],
            selectedData:[]
 
        }
    }
    screenConditionItemReturn(typeId,returnData){
        debugger
        var selectedData=this.state.selectedData;
        for(var i=0;i<selectedData.length;i++){
            if(selectedData[i].hasOwnProperty(typeId)){//如果數(shù)組中存在這個(gè)類(lèi)型的數(shù)據(jù),現(xiàn)將其刪除掉,然后在push入新的數(shù)據(jù)
                selectedData.splice(i,1);
            }
        }
        selectedData.push(returnData);
        this.setState({selectedData:selectedData});
    }
    render(){
        var queryBtnStyle=null;
        switch (window.screen.width){
            case 1366:
                queryBtnStyle={
                    width:"80px",
                    height:"35px",
                    border:"1px solid #C3B295",
                    cursor:"pointer",
                    borderRadius:"10px",
                    textAlign:"center",
                    lineHeight:"35px",
                    backgroundColor:"#D1C7B0",
                    fontSize:"18px",
                    color:"#ffffff",
                    top: "-37px",
                    left: "1030px",
                    position: "relative"
                }
                break;
            case 1920:
                queryBtnStyle={
                    width:"80px",
                    height:"35px",
                    border:"1px solid #C3B295",
                    cursor:"pointer",
                    borderRadius:"10px",
                    textAlign:"center",
                    lineHeight:"35px",
                    backgroundColor:"#D1C7B0",
                    fontSize:"18px",
                    color:"#ffffff",
                    top: "-37px",
                    left: "1030px",
                    position: "relative"
                }
                break;
        }
        var screenConditionData=this.state.screenConditionData;
        if(screenConditionData.length>0){
            var screenType=screenConditionData.map((data,index)=>{
                return (<ScreenConditionItem key={index} data={data} callbackParent={this.screenConditionItemReturn.bind(this)}/>);
            })
        }
        return(
            <div>
                {screenType}
                <div style={queryBtnStyle}>查詢(xún)</div>
            </div>
        );
    }
}

screenConditionItem.js文件

/**
 *
 * title: screenConditionItem.js
 *
 * author: WangPei.
 *
 * date: 2017/5/18.
 *
 */
import React, {Component} from "react"
import "./screenConditionItem.less"
export default class ScreenConditionItem extends Component {
    constructor(props) {
        super(props);
        this.state = {
            /*itemData: {
                "screenTypeId": "1",
                "screenTypeName": "渠道類(lèi)型",
                "values": [
                    {
                        "sid": "-1",
                        "sname": "全部"
                    },
                    {
                        "sid": "01",
                        "sname": "線(xiàn)下實(shí)體"
                    },
                    {
                        "sid": "02",
                        "sname": "線(xiàn)上電商"
                    },
                    {
                        "sid": "03",
                        "sname": "集客渠道"
                    },
                    {
                        "sid": "04",
                        "sname": "其他"
                    }
                ]
            },*/
            selectedItems: {"1": ["-1"]}  //某一維度類(lèi)型選中的數(shù)據(jù){key:value} key類(lèi)型id,value選中數(shù)據(jù)的id的數(shù)組
 
        }
        this.onClickScreenItem = this.onClickScreenItem.bind(this);
    }
    componentWillMount(){
        if(this.props.data!==null){
            var typeId=this.props.data.screenTypeId;
            var newSelectedItems={};
            newSelectedItems[typeId]=["-1"]
            this.setState({selectedItems:newSelectedItems});
        }
    }
    onClickScreenItem(event) {
        debugger;
        var clickItem = event.currentTarget;
        var typeId = clickItem.parentNode.id;
        var sId = clickItem.id;
        var selectedItems = this.state.selectedItems;
        var newSelectedItems = {...selectedItems};
        var selectedItemsId = newSelectedItems[typeId] //某一類(lèi)型下選中的數(shù)據(jù)的id的數(shù)組
        if (sId === "-1" && selectedItemsId.indexOf(sId) === -1) {//當(dāng)點(diǎn)擊的是全部并且數(shù)組中沒(méi)有全部時(shí),則刪掉選中的所有數(shù)據(jù),并將全部這一項(xiàng)的id放入數(shù)組中
            for (var i = selectedItemsId.length-1; i >=0; i--) {
                selectedItemsId.splice(i, 1);
            }
            selectedItemsId.push(sId);
        }
        else if (selectedItemsId.indexOf(sId) === -1) {//如果點(diǎn)擊的不是全部,是其他選項(xiàng)時(shí),若數(shù)組中有全部這一項(xiàng),先刪除全部這一項(xiàng),然后將選中的項(xiàng)放入數(shù)組中
            for (var i = 0; i < selectedItemsId.length; i++) {
                if (selectedItemsId[i] === "-1") {
                    selectedItemsId.splice(i, 1);
                }
            }
            selectedItemsId.push(sId);
        }
        else { //如果點(diǎn)擊的是全部或者別的選項(xiàng),并且點(diǎn)擊的這一項(xiàng)已經(jīng)存在于數(shù)組中,則應(yīng)該刪除掉這一項(xiàng)
            selectedItemsId.splice(selectedItemsId.indexOf(sId), 1);
        }
        this.props.callbackParent(typeId,newSelectedItems);
        this.setState({selectedItems: newSelectedItems});
 
    }
 
    setScreenItemStyle(sid, typeId) {
        var selectedItems = this.state.selectedItems;
        if (selectedItems[typeId].indexOf(sid) !== -1) {
            return "specialReport-screenConditionItem-sName-div-change";
        } else {
            return "specialReport-screenConditionItem-sName-div";
        }
    }
 
    render() {
        var itemData = this.props.data;
        if (itemData !== null && itemData.values.length > 0) {
            var td = itemData.values.map((data, index) => {
                return (
                    <td id={itemData.screenTypeId} key={index} className="specialReport-screenConditionItem-sName">
                        <div className={this.setScreenItemStyle(data.sid, itemData.screenTypeId)}
                             onClick={this.onClickScreenItem}
                             id={data.sid}>{data.sname}</div>
                    </td>
                );
            });
        }
        return (
            <table className="specialReport-screenConditionItem-table">
                <tbody>
                <tr>
                    <td className="specialReport-screenConditionItem-typeName">{itemData.screenTypeName}:</td>
                    {td}
                </tr>
                </tbody>
            </table>
        );
    }
}

screenConditionItem.less文件

@sNameDivHeight:25px;
@sNameDivLineHeight:25px;
@fontFamily:"Microsoft Yahei";
@media only screen and (min-width: 961px) and (max-width: 1366px){
  @sNameDivWidth:80px;
  .specialReport-screenConditionItem-typeName{
    width: 100px;
    height: 40px;
  }
  .specialReport-screenConditionItem-sName{
    width: 100px;
    height: auto;
  }
  .specialReport-screenConditionItem-sName-div{
    text-align: center;
    width: @sNameDivWidth;
    height: @sNameDivHeight;
    line-height: @sNameDivLineHeight;
    color:#cfcfcf;
    cursor: pointer;
  }
  .specialReport-screenConditionItem-sName-div-change{
    text-align: center;
    width: @sNameDivWidth;
    height: @sNameDivHeight;
    line-height: @sNameDivLineHeight;
    color:#ffffff;
    background-color: #D1C7B0;
    border-radius: 10px;
    cursor: pointer;
  }
}
@media only screen and (min-width: 1367px) and (max-width: 1920px){
  @sNameDivWidth:100px;
  .specialReport-screenConditionItem-table{
    font-family: @fontFamily;
    font-size: 20px;
  }
  .specialReport-screenConditionItem-typeName{
    width: 150px;
    height: 40px;
  }
  .specialReport-screenConditionItem-sName{
    width: 150px;
    height: auto;
  }
  .specialReport-screenConditionItem-sName-div{
    text-align: center;
    width: @sNameDivWidth;
    height: @sNameDivHeight;
    line-height: @sNameDivLineHeight;
    color:#cfcfcf;
    cursor: pointer;
  }
  .specialReport-screenConditionItem-sName-div-change{
    text-align: center;
    width: @sNameDivWidth;
    height: @sNameDivHeight;
    line-height: @sNameDivLineHeight;
    color:#ffffff;
    background-color: #D1C7B0;
    border-radius: 10px;
    cursor: pointer;
  }
}

頁(yè)面效果:

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • React Native 啟動(dòng)流程詳細(xì)解析

    React Native 啟動(dòng)流程詳細(xì)解析

    這篇文章主要介紹了React Native 啟動(dòng)流程簡(jiǎn)析,文以 react-native-cli 創(chuàng)建的示例工程(安卓部分)為例,給大家分析 React Native 的啟動(dòng)流程,需要的朋友可以參考下
    2021-08-08
  • React簡(jiǎn)單介紹

    React簡(jiǎn)單介紹

    React 是一個(gè)用于構(gòu)建用戶(hù)界面的 JavaScript 庫(kù),主要用于構(gòu)建 UI,而不是一個(gè) MVC 框架,React 擁有較高的性能,代碼邏輯非常簡(jiǎn)單,越來(lái)越多的人已開(kāi)始關(guān)注和使用它
    2017-05-05
  • React解析html 標(biāo)簽的方法

    React解析html 標(biāo)簽的方法

    在React中,解析HTML標(biāo)簽通常是使用JSX(JavaScript XML)語(yǔ)法的一部分,這篇文章主要介紹了React 用來(lái)解析html 標(biāo)簽的方法,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • React中的axios模塊及使用方法

    React中的axios模塊及使用方法

    axios 是一個(gè)基于 promise 的 HTTP 庫(kù),可以用在瀏覽器和 node.js 中,本文給大家分享React中axios模塊的使用方法,感興趣的朋友一起看看吧
    2022-03-03
  • React-hooks中的useEffect使用步驟

    React-hooks中的useEffect使用步驟

    這篇文章主要介紹了React-hooks中的useEffect,對(duì)于React組件來(lái)說(shuō),主作用是根據(jù)數(shù)據(jù)(state/props)渲染UI,除此之外都是副作用(比如手動(dòng)修改DOM、發(fā)送ajax請(qǐng)求),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-05-05
  • react-redux的connect示例詳解

    react-redux的connect示例詳解

    connect()是react-redux中的核心方法之一,它將react組件預(yù)redux中的Store真正連接在一起,這篇文章主要介紹了react-redux的connect詳解,需要的朋友可以參考下
    2023-01-01
  • React如何解決樣式污染問(wèn)題

    React如何解決樣式污染問(wèn)題

    這篇文章主要介紹了React如何解決樣式污染問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • React中 Zustand狀態(tài)管理庫(kù)的使用詳解

    React中 Zustand狀態(tài)管理庫(kù)的使用詳解

    Zustand?是一個(gè)非常輕量、簡(jiǎn)潔的狀態(tài)管理庫(kù),旨在為 React 提供簡(jiǎn)便且高效的狀態(tài)管理,相比于 Redux 或 Context API,Zustand 具有更簡(jiǎn)潔、靈活和易于理解的優(yōu)點(diǎn),感興趣的朋友一起看看吧
    2024-12-12
  • 詳解React如何使用??useReducer???高階鉤子來(lái)管理狀態(tài)

    詳解React如何使用??useReducer???高階鉤子來(lái)管理狀態(tài)

    useReducer是React中的一個(gè)鉤子,用于替代?useState來(lái)管理復(fù)雜的狀態(tài)邏輯,本文將詳細(xì)介紹如何在React中使用?useReducer高階鉤子來(lái)管理狀態(tài),感興趣的可以了解下
    2025-02-02
  • 前端框架react-spring基礎(chǔ)用法

    前端框架react-spring基礎(chǔ)用法

    這篇文章主要為大家介紹了前端框架react-spring基礎(chǔ)用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04

最新評(píng)論