react实现筛选条件组件

筛选条件组件点全部时选中全部,点其他时全部取消,点击查询时将某个类型下的选择项传出
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": "渠道类型",
                    "values": [
                        {
                            "sid": "-1",
                            "sname": "全部"
                        },
                        {
                            "sid": "01",
                            "sname": "线下实体"
                        },
                        {
                            "sid": "02",
                            "sname": "线上电商"
                        },
                        {
                            "sid": "03",
                            "sname": "集客渠道"
                        },
                        {
                            "sid": "04",
                            "sname": "其他"
                        }
                    ]
                },
                {
                    "screenTypeId": "2",
                    "screenTypeName": "合约类型",
                    "values": [
                        {
                            "sid": "-1",
                            "sname": "全部"
                        },
                        {
                            "sid": "01",
                            "sname": "线下实体2"
                        },
                        {
                            "sid": "02",
                            "sname": "线上电商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)){//如果数组中存在这个类型的数据,现将其删除掉,然后在push入新的数据
                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}>查询</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": "渠道类型",
                "values": [
                    {
                        "sid": "-1",
                        "sname": "全部"
                    },
                    {
                        "sid": "01",
                        "sname": "线下实体"
                    },
                    {
                        "sid": "02",
                        "sname": "线上电商"
                    },
                    {
                        "sid": "03",
                        "sname": "集客渠道"
                    },
                    {
                        "sid": "04",
                        "sname": "其他"
                    }
                ]
            },*/
            selectedItems: {"1": ["-1"]}  //某一维度类型选中的数据{key:value} key类型id,value选中数据的id的数组

        }
        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] //某一类型下选中的数据的id的数组
        if (sId === "-1" && selectedItemsId.indexOf(sId) === -1) {//当点击的是全部并且数组中没有全部时,则删掉选中的所有数据,并将全部这一项的id放入数组中
            for (var i = selectedItemsId.length-1; i >=0; i--) {
                selectedItemsId.splice(i, 1);
            }
            selectedItemsId.push(sId);
        }
        else if (selectedItemsId.indexOf(sId) === -1) {//如果点击的不是全部,是其他选项时,若数组中有全部这一项,先删除全部这一项,然后将选中的项放入数组中
            for (var i = 0; i < selectedItemsId.length; i++) {
                if (selectedItemsId[i] === "-1") {
                    selectedItemsId.splice(i, 1);
                }
            }
            selectedItemsId.push(sId);
        }
        else { //如果点击的是全部或者别的选项,并且点击的这一项已经存在于数组中,则应该删除掉这一项
            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;
  }
}

页面效果:




  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值