使用react-bootstrap从阵列动态创建下拉菜单选项

时间:2018-11-29 01:25:15

标签: javascript reactjs bootstrap-4 react-bootstrap

我现在正在建立一个基于drupal的电子商务网站,并陷入困境。总体而言,它仍然是新的反应和编码方式,但仍在尝试学习。因此,使用redux和rest api获取了所有数据,并获得了我的产品,版本和属性。产品页面基于URL设置特定产品,现在我需要能够通过下拉菜单选择其他属性。目前,我有一个占位符下拉列表,该下拉列表与react-bootstrap文档中显示的匹配。但是,我需要根据保存属性的数组在该下拉菜单中放置选项。

我敢肯定这很简单,但是我一直在搜索,但是还没有找到可行的答案。希望你们能提供帮助。

在浏览代码时,请记住,sizes [[]是我要从中放置数据的数组,作为下拉菜单中的可选选项。

这是产品页面:

import React, { Component} from 'react';
import '../../css/Home.css';
import MenuBar from "../sub-components/MenuBar";
import LeftMenuBar from "../sub-components/LeftMenuBar";
import "../../css/ProductPage.css"
import WomensWear from "../../media/WomensWear.jpg"
import {
    Dropdown,
    DropdownToggle,
    DropdownMenu,
    DropdownItem } from 'reactstrap';

class ProductPage extends Component {
    constructor(props) {
        super(props);

        this.toggle = this.toggle.bind(this);
        this.state = {
            dropdownOpen: false
        };
    }

    toggle() {
        this.setState(prevState => ({
            dropdownOpen: !prevState.dropdownOpen
        }));
    }

    getProduct() {

        let product = null;
        let sizes = [];
        if (this.props.products && this.props.products.items.length) {
            product = this.props.products.items.find(o => o.path[0].alias === this.props.router.match.url);

            if (product && this.props.variations && this.props.attributes) {
                product.something = [];
                for (let i = 0; i < product.variations.length; i++) {
                    let varid = product.variations[i].target_id;
                    let variation = this.props.variations.items.find(o => o.variation_id[0].value === varid);

                    variation.size = this.props.attributes.items.find(o => o.attribute_value_id[0].value === variation.attribute_size[0].target_id);

                    sizes.push({value: variation.size.attribute_value_id[0].value, name: variation.size.name[0].value});
                    product.something.push(variation);

                    console.log(sizes);
                }
            }
        }
        return product;
    }


    render() {
        let style = {
            height: this.props.height - 56,
        };

        let product = this.getProduct();

        let body = product && product.body.length ? product.body[0].value : null;

        return (
            <div className="App" id="default">
                <div className='MenuBar'>
                    <MenuBar/>
                </div>
                <div>
                    <div style={style} className="ProductPage row no-gutters">
                        <div className="col-xs-3 col-md-3">
                            <LeftMenuBar/>
                        </div>
                        <div className="outer col-xs-4 col-md-4">
                            <div>
                                <div id="ProductPlacement">
                                  <img src={WomensWear} alt=""/>
                                    <div id="alternate-pics">
                                        <div id="alt-pic">
                                        </div>
                                        <div id="alt-pic">
                                        </div>
                                        <div id="alt-pic">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div className="col-xs-5 col-md-5">
                            <div id="ImagePlacement">
                                <div className="ProductTitle">
                                    <h1>First Product</h1>
                                </div>
                                <hr/>
                                <div className="ProductDescription">
                                    <div dangerouslySetInnerHTML={{__html: body}} />
                                </div>
                                <div id="options">
                                    <div id="color">
                                    </div>
                                    <div id="color2">
                                    </div>
                                    <div id="color3">
                                    </div>
                                </div>
                                <div id="options">
                                    <div>
                                        <Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
                                            <DropdownToggle caret id="size-dropdown">
                                                Size
                                            </DropdownToggle>
                                            <DropdownMenu>
                                                <DropdownItem>1</DropdownItem>
                                                <DropdownItem>3</DropdownItem>
                                                <DropdownItem>5</DropdownItem>
                                            </DropdownMenu>
                                        </Dropdown>
                                        <div className="AddToCart">
                                            <button className="AddToCart">Add To Cart</button>
                                            <button className="Price">$34.99</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

export default ProductPage;

1 个答案:

答案 0 :(得分:3)

React的整洁之处在于您可以使用常规JS。

<Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
   <DropdownToggle caret id="size-dropdown">
     Size
   </DropdownToggle>
   <DropdownMenu>
     {sizes.map(size => (
       <DropdownItem>{size}</DropdownItem>
     ))}
   </DropdownMenu>
</Dropdown>

旁注:select似乎是更合适的元素,但这不是您的问题。