[英]How to handle OnChange of array property which is 'react-select' Select element?
How to Handle Onchange set e.value to 'prodId' only.如何处理 Onchange 仅将 e.value 设置为“prodId”。
I want to set onChange for property 'prodId' of the array of object using Select element of 'react-select'我想使用“react-select”的 Select 元素为 object 数组的属性“prodId”设置 onChange
I know it take value in e.value but dont know how to do in array of object.我知道它在 e.value 中具有价值,但不知道如何在 object 数组中执行。
OR can I create 'prodId' usestate array variable separately and after select values push it to the 'Product_Details' array.或者我可以单独创建“prodId”usestate 数组变量,然后在 select 值将其推送到“Product_Details”数组之后。
Please help in this!请帮忙!
I have Array Of Object like this:-我有这样的 Object 数组:-
const [Product_Details, setProduct_Details] = useState([]);
useEffect(() => {
if (BillIdFetch === undefined) return;
setProduct_Details([
{
index: Math.random(),
billId: BillIdFetch,
//I want to handle OnChange for this property on react-select.
prodId: "",
price: "",
prod_SrNo: "",
discount: "0",
Qty : "1",
Prod_Status: "NULL",
proData_warranty_In_Prcnt: "NULL",
Credit_Note: "NULL",
Replacement: "NULL",
},
]);
}, [BillIdFetch]);
I Created handle Input function for handle input onchange event triggered but in this i cant set value of 'prodId' bcoz it take e.value.我为触发的句柄输入 onchange 事件创建了句柄输入 function,但在此我无法设置“prodId”的值,因为它采用 e.value。
handleInput() :-处理输入() : -
const handleProductDetailsInputs = (index, e) => {
const Pdetails = [...Product_Details];
Pdetails[index][e.target.name] = e.target.value;
setProduct_Details(Pdetails);
};
return ()返回 ()
return (
<>
<tr key={val.index}>
<td>
<Select
key={val.id}
required
class="form-select"
id={prodId}
data-id={idx}
name="prodId"
onChange={(e) => handleProductDetailsInputs(idx, e)}
options={Select_Product_Options}
/>
</td>
</tr>
<>)
I didn't understand your question completely.我没有完全理解你的问题。 but if you are trying to make options from data fetched from API then here it is how I am doing by using 'react-select' package:但是,如果您尝试从从 API 获取的数据中进行选择,那么这就是我使用“react-select”package 所做的事情:
const [selectedOption, setSelectedOption] = useState(null)
const [clientsData, setClientsData] = useState([]) // coming from api
const clientIdOptions = clientsData.map((row) => {
return { value: row.id, label: row.lead_name }
})
return (
<Select isSearchable defaultValue={selectedOption} onChange={setSelectedOption} options={clientIdOptions} />
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.