我有两个组成部分。我从componentA获得roles
字段的值,并使用此值我需要为users
的componentB创建另一个记录。
componentA的结构如下:
[
{
"id": "507f1f77bcf86cd799439011",
"title": "admin",
"status": "active"
},
...
]
componentB的结构是这样的,在该结构中,我正在获取角色数据,并且当我创建另一个记录时,那时应该以这种格式插入角色的值。
[
{
"id": "507f1f77bcf86cd799439017",
"email": "admin@admin.com",
"roles": {
"id": "507f1f77bcf86cd799439011",
"title": "admin",
"status": "active"
}
},
...
]
=>用户插入组件以插入记录。
import { BootstrapTable, TableHeaderColumn } from "react-bootstrap-table";
import RolesField from "./Editor/Roles/Field";
class UserInsertRow extends React.Component {
constructor(props) {
super(props);
this.roles = [];
this.getRoles();
}
customRoleField = (
column,
attr,
editorClass,
ignoreEditable,
defaultValue
) => (
<RolesField
roles={this.roles}
ref={attr.ref}
{...this.props}
editorClass={editorClass}
ignoreEditable={ignoreEditable}
/>
);
render() {
return (
<BootstrapTable
ref="table"
insertRow={true}
deleteRow={true}
pagination={true}
data={this.props.data}
>
<TableHeaderColumn
dataField="role"
customInsertEditor={{ getElement: this.customRoleField }}
customEditor={{
getElement: this.roleEditor,
customEditorParameters: { roles: this.roles }
}}
>
Role
</TableHeaderColumn>
</BootstrapTable>
);
}
}
=> RolesField组件
import { rolesContext } from "../../Context";
class RolesField extends React.Component {
constructor(props) {
super(props);
}
getFieldValue = () => rolesContext._currentValue.selectedRoles;
render() {
console.log(this.props.roles);
return (
<div className="form-group">
<select
className="form-control editor edit-select"
onChange={this.selectRole}
>
{this.props.roles.map(name => (
<option ref={name.id} key={name.id} value={JSON.stringify(role)}>
{name.title}
</option>
))}
</select>
</div>
);
}
selectRole(e) {
var options = e.target.options;
var value = [];
for (var i = 0, l = options.length; i < l; i++) {
if (options[i].selected) {
value.push(options[i].value);
}
}
rolesContext._currentValue.selectedRoles = [];
value.map(value => rolesContext._currentValue.selectedRoles.push(value));
}
export default RolesField;
我用于存储数据的Context API。
import React from 'react';
export const roles = {selectedRoles: []};
export const rolesContext = React.createContext(roles);
email: "qqq@qqq.com"
id: "507f1f77bcf86cd799439123"
role: []
如何管理<select>
字段的roles
标记,以便从选定的下拉值中插入的新记录应如下所示:
{
"id": "507f1f77bcf86cd799439017",
"email": "qqqq@qqqq.com",
"roles": {
"id": "507f1f77bcf86cd799439123",
"title": "admin",
"status": "active"
}
},
P.S请使用此冗长的代码。
答案 0 :(得分:1)
您可以通过使用映射来存储role_id-> role_data对来突破它。将role_id分配为每个选项的值,并在onChange处理程序中获取该值。使用此值,您将能够找到确切的角色。然后执行下一步所需的操作。
编辑:添加示例代码
角色来自八人制状态/道具
const roles = [
{id: 'id_1', name: 'Admin'},
{id: 'id_2', name: 'Super Admin'}
]
onChange处理程序
onChange = e => {
const id = e.target.value
const seletedRole = roles.find(role => role.id === id)
this.setState({
selectedRole
})
}
渲染选择和选项
<select value={this.state.selectedRole.id} onChange={this.onChange}>
{
roles.map(role => <option id={role.id} value={role.id}>{role.name}</option>)
}
</select>
答案 1 :(得分:0)
由于要通过从列表中选择一个选项来获取整个对象,因此可以像这样将整个对象存储为data-value
的{{1}},可以将自定义对象替换为{{1 }}
<option>
答案 2 :(得分:0)
我认为您可以将类组件转换为功能组件,并与挂钩一起使用可以使您的生活变得轻松。 要使用上下文的值并更新值上下文,请查看此链接。 How to change Context value while using React Hook of useContext