在选择其他项目时从列表中取消选择
React unselect from list while selecting another item
我正在创建一个列表,您可以在其中选择(按钮样式),您只能单击一个按钮。当活动时,我能够突出显示列表中的项目。如果我碰巧从列表中选择另一个项目,我似乎无法取消选择。这是我的组件的代码:
var styles = {
active: {
backgroundColor: '#337ab7',
color: '#ffffff'
},
inactive: {
backgroundColor: 'inherit',
color: 'inherit'
}
};
var CustomerRolo = React.createClass({
getInitialState() {
return {active: false}
},
handleToggle: function(e) {
e.preventDefault();
//console.log(lastSelection)
this.setState({ active: !this.state.active});
console.log(React.findDOMNode(this.refs.active))
},
render: function(){
const stateStyle = this.state.active ? styles.active : styles.inactive
return(
<a href="" className='anker' onClick={this.handleToggle}>
<div id='rolo' style = {stateStyle}>
<h5 className="listitems"><strong>{this.props.customer.lastname + ", " + this.props.customer.name}</strong></h5>
<p>{this.props.customer.mobile}</p>
<hr/>
</div>
</a>
)
}
});
我将其渲染到一个主组件中并从该组件传递 props,但是活动 true 或 false 的状态正在 CustomerRolo 组件中进行管理。这是主要组件:
var Jobs = React.createClass({
getInitialState() {
return {jobs: this.props.jobs,
customers: this.props.customers}
},
addCustomer: function(customer) {
var customers = React.addons.update(this.state.customers, { $push: [customer] })
this.setState({ customers: customers });
},
buttonStyle: {
backgroundColor: 'lightblue',
paddingTop: '5px',
paddingBottom: '5px',
width: '150px',
height: '35px',
marginTop: '0',
marginBottom: '1px',
borderRadius: '5px'
},
render: function() {
return(
<div className="container">
<div className="row">
<div className="col-md-10">
<h2 className="title">Jobs</h2>
</div>
</div>
<div className="row">
<div className="col-md-4">
<CustomerForm handleNewCustomer={this.addCustomer}/>
</div>
</div>
<div className="row">
<div id='customerrolo' className="col-md-4">
{this.state.customers.map(function(customer, index) {
return <CustomerRolo key={index} customer={customer}/>}.bind(this))}
</div>
<div id = "years" className="col-md-4">
{this.props.years.map(function(year){
return <Years key={year['Year']} year={year}/>}.bind(this))}
</div>
</div>
<div className="row">
<div className="col-md-10">
<table className="table table-hover">
<thead>
<tr>
<th>Customer</th>
<th>Plate</th>
<th>Make</th>
<th>Model</th>
<th>Created</th>
<th>Status</th>
<th>Progress</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{this.state.jobs.map(function(job) {
return <Job key={job.id} job={job}/>}.bind(this))}
</tbody>
</table>
</div>
</div>
</div>
)
}
})
而不是将活动状态存储在列表项中,而是将其存储在父项中。因此,列表项onToggle
需要两个道具,active
这些道具本质上是this.state.active
和this.handleToggle
render: function() {
return (
<li
style={this.props.active ? aStyle : iStyle}
onClick={this.props.onToggle}
>
hi
</li>
)
}
现在将它们移动到父列表组件(我假设您的第二个组件),以便父存储哪个客户处于活动状态并从状态设置该属性
render: function() {
var active = this.state.activeIndex;
var Rolo = customers.map(function(customer, index){
return (
<CustomerRolo
active={index === active}
onToggle={this.handleToggle.bind(null, idx}/>
)
}, this)
},
handleToggle: function(index) {
this.setState({ activeIndex: index })
}
相关文章:
- Angular:使用选择列表选择过滤代码中的对象
- 在Twitter BootStrap嵌套下拉列表中托管一个选择列表(IE 9特定)
- Jquery不允许来自多个选择列表的相同值
- 谷歌表单-根据分配给谷歌应用程序的工作为商业用户预先填充多项选择列表
- 如何修剪表单'提交'多级选择列表的值
- 使用jQuery获取选择列表中每个更改的值
- Javascript/Jquery将选择列表更改为单选按钮
- 动态填充的选择列表
- jqgrid动态加载选择列表
- 使用peta poco ORM在MVC应用程序中创建选择列表
- 我正在尝试从 xhtml 文件中动态提取所有
元素,并使用 javascript 将它们放入选择列表中
- JQuery无法找到选择列表
- 我正试图动态地拉动<h3>元素,并将它们放在选择列表中
- 根据预选,用值填充选择列表
- 在PHP中将下拉选择列表的内容从一个表单传递到另一个表单
- 我已经创建了一个动态的下拉选择列表,它指向一行;如何使该行成为变量
- 允许用户订购选择列表
- 动态选择列表AJAX和插入数据库表通过张贴形式
- 使用jQuery的层次选择列表
- 如何在选择列表项时重新加载iframe