Phi*_*hil 9 javascript reactjs react-select core-ui
我使用的是@coreui/react与React-select问题是,返回Select的元素scoped slots符core-ui的功能就像搜索和排序
但是,如果在返回带有文本Ex 的a<label>或 a时它工作正常:<p><label>{item.status}</label>
题
为什么 Select 组件会破坏功能?
任何解决方法/努力都受到高度赞赏
笔记
我尝试过类似的解决方法<p hidden >{item.status}</p>,然后渲染Select组件,但它不起作用
import React from "react";
import Select from "react-select";
import { CDataTable } from "@coreui/react";
...
<CDataTable
bordered
clickableRows
fields={fields}
hover
items={[...employeeData]}
itemsPerPage={10}
itemsPerPageSelect
loading={tableLoader}
onRowClick={(e) => rowSelectHandler(e)}
pagination
size="sm"
sorter={{ resetable: true }}
striped
tableFilter={{
placeholder: "Filter",
label: "Search:",
}}
scopedSlots={{
status: (item, index) => (
<td style={{ width: "13%" }}>
<Select
id={item.index}
placeholder="Select Status"
isSearchable={false}
className="basic-single"
onChange={(e) => selectChangeHandler(e.value, index)}
classNamePrefix="select"
defaultValue={{
label: item.status,
value: item.status,
color: getBadge(item.status),
}}
// name="color"
// inputValue={item.status}
options={[
{
value: "ACTIVE",
label: "ACTIVE",
color: "#2eb85c",
},
{
value: "DEACTIVE",
label: "DEACTIVE",
color: "#e55353",
},
]}
styles={colourStyles}
/>
</td>
),
}}
/>
...
Run Code Online (Sandbox Code Playgroud)
编辑
antd-select如果它适用,也接受答案coreui-datatable
我设法组合了一个codesandbox,并且还能够找到它不起作用的原因,并修复它。
https://codesandbox.io/s/twilight-butterfly-itppu?file=/src/App.js
我猜这个 Select 组件有一些用 defaultValue 初始化的内部状态。然后排序时表会更改索引(它对数据数组进行排序)并且您使用索引作为 id,因此 React 重用相同的元素,但无法更新值,因为您只提供 defaultValue。
基本上你应该使用value而不是defaultValue在Select.
https://codesandbox.io/s/goofy-browser-b02xb?file=/src/App.js
或者
根据项目的某些唯一属性(而不是数组中的索引)添加键。
https://codesandbox.io/s/zen-sky-56vly?file=/src/App.js