简体   繁体   English

如何在reactjs的菜单项中添加复选框?

[英]How to add checkbox to menu items in reactjs?

I am making dynamic menus using a recursive function and I have already made the menus and it display in the right order without any issues.我正在使用递归函数制作动态菜单,并且我已经制作了菜单并且它以正确的顺序显示而没有任何问题。

Requirement:要求:

Here I am in the need to find out the last level of menus and need to assign checkbox with value as their respective id {item.id} .在这里,我需要找出菜单最后一级,并需要为复选框分配值作为其各自的 id {item.id}

Eg:例如:

For First menu one ,对于第一个菜单之一

 -> [Checkbox with value as 1.1.1] One-One-One
 -> [Checkbox with value as 1.1.2] One - one - two
 -> [Checkbox with value as 1.1.3] One - one - three

For Second menu two ,对于第二个菜单

 -> [Checkbox with value as 2.1] Two - one

. .

. .

. .

For sixth menu six ,对于第六菜单

 -> [Checkbox with value as 6] Six

I hope the point is clear that I need to find out the last level children as like given above example and should assign a checkbox to it with the value of their id .我希望这一点很清楚,我需要像上面的例子一样找出最后一级的孩子,并应该为其分配一个复选框,其值为id

Please take a look at snippet provided below and help me to achieve the result of making the checkbox at the last level请查看下面提供的代码段,并帮助我实现在最后一级制作复选框的结果

Note: The checkbox have to be inline with the last level elements as like the example shown above with checkbox value as their respective id .注意:复选框必须与最后一级元素内联,就像上面显示的示例一样,复选框值作为它们各自的id

The purpose of this requirement is that it will have multiple submenus and the last level have the filter values so on click of the checkbox, I would pass the value to api and retrieve the related data for this particular checked menu item and will display.. But here I am in the need of checkbox display respective to menu item that's it..此要求的目的是它将有多个子菜单,最后一级有过滤器值,因此单击复选框,我会将值传递给api并检索此特定选中菜单项的相关数据,并将显示..但是在这里我需要与菜单项相对应的复选框显示就是这样..

 const loadMenu = () => Promise.resolve([{id:"1",name:"One",children:[{id:"1.1",name:"One - one",children:[{id:"1.1.1",name:"One - one - one"},{id:"1.1.2",name:"One - one - two"},{id:"1.1.3",name:"One - one - three"}]}]},{id:"2",name:"Two",children:[{id:"2.1",name:"Two - one"}]},{id:"3",name:"Three",children:[{id:"3.1",name:"Three - one",children:[{id:"3.1.1",name:"Three - one - one",children:[{id:"3.1.1.1",name:"Three - one - one - one",children:[{id:"3.1.1.1.1",name:"Three - one - one - one - one"}]}]}]}]},{id:"4",name:"Four"},{id:"5",name:"Five",children:[{id:"5.1",name:"Five - one"},{id:"5.2",name:"Five - two"},{id:"5.3",name:"Five - three"},{id:"5.4",name:"Five - four"}]},{id:"6",name:"Six"}]); const {Component, Fragment} = React; const {Button, Collapse} = Reactstrap; class Menu extends Component { constructor(props) { super(props); this.state = {menuItems: []}; } render() { return <MenuItemContainer menuItems={this.state.menuItems} />; } componentDidMount() { loadMenu().then(menuItems => this.setState({menuItems})); } } function MenuItemContainer(props) { if (!props.menuItems.length) return null; const renderMenuItem = menuItem => <li key={menuItem.id}><MenuItem {...menuItem} /></li>; return <ul>{props.menuItems.map(renderMenuItem)}</ul>; } MenuItemContainer.defaultProps = {menuItems: []}; class MenuItem extends Component { constructor(props) { super(props); this.state = {isOpen: false}; this.toggle = this.toggle.bind(this); } render() { return ( <Fragment> <Button onClick={this.toggle}>{this.props.name}</Button> <Collapse isOpen={this.state.isOpen}> <MenuItemContainer menuItems={this.props.children} /> </Collapse> </Fragment> ); } toggle() { this.setState(({isOpen}) => ({isOpen: !isOpen})); } } ReactDOM.render(<Menu />, document.getElementById("root"));
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/reactstrap/8.4.1/reactstrap.min.js"></script> <div id="root"></div>

From my understanding, you need checkbox for the last child.根据我的理解,您需要最后一个孩子的复选框。 This can be achieved by checking whether the element has child or not.这可以通过检查元素是否有子元素来实现。

Below are the changes that made in order to add the checkbox.以下是为添加复选框所做的更改。

 render() { let isLastChild = this.props.children ? false : true; return ( <Fragment> <Button onClick={this.toggle}>{this.props.name}</Button> <Fragment> {isLastChild ? <Input type="checkbox" /> : ''} </Fragment> <Collapse isOpen={this.state.isOpen}> <MenuItemContainer menuItems={this.props.children} /> </Collapse> </Fragment> ); }

Here is the plnkr link for your reference.这是plnkr链接供您参考。

Hope it helps :)希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM