react 下拉框內(nèi)容回顯的實(shí)現(xiàn)思路
需要實(shí)現(xiàn)效果如下

目前效果如下

思路 : 將下拉框選項的value和label一起存儲到state中 , 初始化表單數(shù)據(jù)時 , 將faqType對應(yīng)的label查找出來并設(shè)置到Form.Item中 , 最后修改useEffect
舊代碼
//可以拿到faqType為0 但是卻沒有回顯出下拉框的內(nèi)容 我需要faqType為0 回顯出下拉框內(nèi)容為對應(yīng)的label
<Form
form={form2}
initialValues={{
question: currentRecord.question || '',
faqType: currentRecord.faqType || '',
}}
>
<Form.Item
label='問題類型'
name='faqType'
colon={false}
rules={[{ required: true, message: '請輸入問題類型' }]}
>
<Select
onChange={value => {
setSelectedCon1(value)
form2.setFieldsValue({ faqType: value })
}}
allowClear
showSearch
placeholder='請輸入問題類型'
style={{ width: 300, height: 40 }}
options={[
{ value: 0, label: '如何使用' },
{ value: 1, label: '常見情況' },
{ value: 2, label: '日常維護(hù)' },
{ value: 3, label: '如何更換' }
]}
/>
</Form.Item>
// 彈窗內(nèi)部數(shù)據(jù)回顯
useEffect(() => {
if (currentRecord) {
form2.setFieldsValue({
question: currentRecord.question || '',
faultInformationId: currentRecord.faultInformationId || '',
faqType: currentRecord.faqType || '',
answer: currentRecord.answer || ''
})
}
}, [currentRecord, form2])
解決問題的代碼
const [faqTypes, setFaqTypes] = useState([
{ value: 0, label: '如何使用' },
{ value: 1, label: '常見情況' },
{ value: 2, label: '日常維護(hù)' },
{ value: 3, label: '如何更換' }
]);
<Form.Item
label='問題類型'
name='faqType'
colon={false}
rules={[{ required: true, message: '請輸入問題類型' }]}
>
<Select
onChange={value => {
setSelectedCon1(value)
form2.setFieldsValue({ faqType: value })
}}
allowClear
showSearch
placeholder='請輸入問題類型'
style={{ width: 300, height: 40 }}
options={faqTypes.map(type => ({ value: type.value, label: type.label }))}
/>
</Form.Item>
useEffect(() => {
if (currentRecord) {
const selectedFaqType = faqTypes.find(type => type.value === currentRecord.faqType);
form2.setFieldsValue({
question: currentRecord.question || '',
faultInformationId: currentRecord.faultInformationId || '',
faqType: selectedFaqType ? selectedFaqType.label : '',
answer: currentRecord.answer || ''
})
}
}, [currentRecord, form2, faqTypes])到此這篇關(guān)于react 下拉框內(nèi)容回顯的文章就介紹到這了,更多相關(guān)react 下拉框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在React頁面重新加載時保留數(shù)據(jù)的實(shí)現(xiàn)方法總結(jié)
在React頁面重新加載時保留數(shù)據(jù),可以通過多種方法來實(shí)現(xiàn),常見的方法包括使用瀏覽器的本地存儲(Local Storage 或 Session Storage)、URL參數(shù)、以及服務(wù)器端存儲等,本文給大家總結(jié)了一些具體實(shí)現(xiàn)方法,需要的朋友可以參考下2024-06-06
react-native使用leanclound消息推送的方法
這篇文章主要介紹了react-native使用leanclound消息推送的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
一百多行代碼實(shí)現(xiàn)react拖拽hooks
這篇文章主要介紹了一百多行代碼實(shí)現(xiàn)react拖拽hooks,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
react router 4.0以上的路由應(yīng)用詳解
本篇文章主要介紹了react router 4.0以上的路由應(yīng)用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09

