这是我定义的组件代码,目的是为了能够把选中的值放到文本框input里,同时要能够把选中的当前数据的id获取到。
请问我要怎样在引用的地方中获取到这个wareId
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}> </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})
}
})
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…