[英]How to dynamically add the valueOptions of a singleSelect column in Material-UI's DataGrid?
This is my second question in SO, so apologize for any error in asking and formatting my question.这是我在 SO 中的第二个问题,对于在询问和格式化我的问题时出现的任何错误,我们深表歉意。 I've been trying to accomplish what the tile said.我一直在努力完成瓷砖所说的。 Basically I have the following example of an array that I use to define my columns:基本上,我有以下用于定义列的数组示例:
[
{
field: 'name',
headerName: 'Name',
flex: 1,
editable: true
},
{
field: 'address',
headerName: "Address",
flex: 1,
editable: true
},
{
field: 'country',
headerName: "Country",
flex: 1,
editable: true,
type: 'singleSelect',
valueOptions:
[
// This should be dynamic
]
},
]
This is exported from a separate file, which is then imported in the React component that uses the DataGrid with these columns definitions.这是从一个单独的文件中导出的,然后将其导入到使用具有这些列定义的 DataGrid 的 React 组件中。
The options are fetched from my REST API when a user logs in and they will be different for each user.当用户登录时,这些选项是从我的 REST API 中获取的,每个用户的选项都不同。
Is there anyway I could assign the valueOptions
for the column country
dynamically after the initial definitions?无论如何,我可以在初始定义之后动态地为列country
分配valueOptions
吗?
Thank you!谢谢!
PS: I'm using the free version of Material-UI's DataGrid. PS:我使用的是 Material-UI 的 DataGrid 的免费版本。
You could change the value of the "valueOptions" field after you have the answer of your REST service:在获得 REST 服务的答案后,您可以更改“valueOptions”字段的值:
fetch("your endpoint").then(answer => { columnsConfig.filter(column => column.field === "country").valueOptions = answer; })
PS: you may have to transform the answer before setting the value of valueOptions PS:您可能必须在设置 valueOptions 的值之前转换答案
valueOpt accepts a function that you can use to dynamically reference the values you are looking for. valueOpt 接受 function ,您可以使用它来动态引用您正在查找的值。
Relevant issue: https://github.com/mui/mui-x/issues/3528相关问题: https://github.com/mui/mui-x/issues/3528
[{
field: 'country',
headerName: "Country",
flex: 1,
editable: true,
type: 'singleSelect',
valueOptions: ({id, row, field}) => {
return row.countryOpt;
}
}]
you can do by valueOptions field你可以通过 valueOptions 字段来做
const types = { // get value from services } const types = { // 从服务中获取价值 }
{
field: 'country', width: 180,
headerName: 'Country',
type: 'singleSelect',
editable: true,
valueOptions: ({ row }) => { const options = [];
types?.map((type) =>options.push(type.name))
return options
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.