最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamically create dropdown menu options from array with react-bootstrap - Stack Overflow

programmeradmin5浏览0评论

I'm building a drupal based e-merce site right now and got stuck. Still new to react and coding in general, but trying to learn. So I've got all my data pulled in, using redux and rest api, and I'm getting my products, variations and attributes. The product page is setting a specific product based on url, and now I need to be able to select the different attributes via a dropdown menu. Currently I have a place holder dropdown that matches the one shown in react-bootstrap documentation. However, I need to be placing options in that dropdown based off of my array holding the attributes.

I'm sure it's simple but I've been searching around and haven't found an answer yet that works. Hopefully you guys can help.

As you look through the code, keep in mind that sizes = [] is the array I'm looking to place data from as the selectable options in the dropdown.

Here's the product page:

import React, { Component} from 'react';
import '../../css/Home.css';
import MenuBar from "../sub-ponents/MenuBar";
import LeftMenuBar from "../sub-ponents/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;

I'm building a drupal based e-merce site right now and got stuck. Still new to react and coding in general, but trying to learn. So I've got all my data pulled in, using redux and rest api, and I'm getting my products, variations and attributes. The product page is setting a specific product based on url, and now I need to be able to select the different attributes via a dropdown menu. Currently I have a place holder dropdown that matches the one shown in react-bootstrap documentation. However, I need to be placing options in that dropdown based off of my array holding the attributes.

I'm sure it's simple but I've been searching around and haven't found an answer yet that works. Hopefully you guys can help.

As you look through the code, keep in mind that sizes = [] is the array I'm looking to place data from as the selectable options in the dropdown.

Here's the product page:

import React, { Component} from 'react';
import '../../css/Home.css';
import MenuBar from "../sub-ponents/MenuBar";
import LeftMenuBar from "../sub-ponents/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;
Share Improve this question asked Nov 29, 2018 at 1:25 Morgan CaldbeckMorgan Caldbeck 1112 silver badges8 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

Neat thing about React is that you can use regular 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>

Sidenote: select seems to be more suitable element for this but that wasn't your question.

发布评论

评论列表(0)

  1. 暂无评论