Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

请问这种情况下怎样把子组件定义的属性传递回父组件?

clipboard.png

这是我定义的组件代码,目的是为了能够把选中的值放到文本框input里,同时要能够把选中的当前数据的id获取到。
请问我要怎样在引用的地方中获取到这个wareId

clipboard.png

clipboard.png

var LinkComplete = React.createClass({
    getInitialState: function () {
        return {

        };
    },
    componentDidMount: function () {
        this.loadWareHouse();
    },
    loadWareHouse(){
        SuwenClient.post({
            url:'/warehouse',
            success(rsp){
                var data = rsp.payload.warehouse;
                this.setState({warehouse:data})
            }
        },this)
    },
    render: function () {
        return (
            <div>
                <input type="text"
                       name={this.props.name}
                       onFocus={this.onfocus}
                       wareId={this.state.wareId}
                       value={this.state.wareData != undefined ? this.state.wareData.name : ''}/>
                  {this.state.show ? this.renderWarehouse(): ''}
            </div>
        );
    },
    onfocus(){
        this.setState({show:true});
    },
    renderWarehouse(){
        var arr = [];
        var that = this;
        var expandState = this.state.expandState || {};
        var showWares = function (ware,lvl) {
            var key = ware.id +'-'+ware.pid;
            var has = hasWare(ware)
            var openBtn = <span className="open_btn" ref="openBtn" onClick={expand.bind(that, key)}>
            <i className="fa fa-caret-right" style={{fontSize: "1.5rem", marginRight: '5px'}}></i>
              </span>

            var closeBtn = <span className="open_btn" ref="openBtn" onClick={expand.bind(that, key)}>
            <i className="fa fa-caret-down" style={{fontSize: "1.5rem", marginRight: '5px'}}></i>
             </span>
            arr.push(<li key={key} onClick={this.selectWarehouse.bind(null,ware)}>
                {createIndent(lvl, has)}
                {expandState[key] ? openBtn : closeBtn}
                {ware.name}
            </li>)

            if (has && !expandState[key]){
                ware.warehouseList.map(ware=>showWares(ware,lvl+1));
            }
        }.bind(this);
        function hasWare(ware) {
            return ware.warehouseList && ware.warehouseList.length
        }
        function expand(key) {
            expandState[key] = !expandState[key];
            this.setState({expandState: expandState});
        }
        function createIndent(lvl, has) {
            var x = [];
            for (var i = 0; i < lvl; i++) {
                x.push(<span key={i}>&nbsp;&nbsp;</span>);
            }
            return x;
        }
        $.map(this.state.warehouse.content,function (ware) {
            showWares(ware,0);
        }.bind(this))
        return (
            <div className="ware-container">
                <div className="ware-content scroll-style">
                    <ul>
                        {arr}
                    </ul>
                </div>
            </div>
        )
    },
    saveWare(e){
    },
    selectWarehouse(ware,e){
        e.target.value = ware.id
        this.setState({wareData:ware,show:false,wareId:e.target.value})
    }
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
// 父组件
class Something extends React.Component {
  handler(wareId) {
    // do something here
  }
  render() {
    <LinkComplete
      xxx=xxx
      xxx=xxx
      onChange={this.handler}
    />
  }
}

// LinkComplete 
class LinkComplete extends React.Component {
  ...
  selectWarehouse(ware,e){
    e.target.value = ware.id
    this.setState({wareData:ware,show:false,wareId:e.target.value})
    // 使用传进来的 onChange 方法
    this.props.onChange(ware.id)
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...