【发布时间】:2018-11-04 10:35:35
【问题描述】:
我遇到了一个问题,即我无法为行中每次添加的元素增加状态元素的 ID。我得到的是每次添加元素都会重复相同的数字,我需要像 ID 这样的东西应该是 1 到 n 个添加数。 在文本框中,我没有输入 ID,我只输入 LText(名称)。 通常,ID 应该为每次添加的元素生成。 我试过的是..
export default class EPIM091 extends cntrl.WITBase {
constructor(props) {
super(props);
const witState = this.state;
if (Object.keys(witState).length == 0) {
witState.model = { LID: 1, LText: '', SOrder: '', Inventoried: false, Location:[] }; //Here i need to store all other state values to the Location Array
}
}
clickAction = (e) => {
if (e.id == 'btn_add') {
//var i = 1;
const { model } = this.state;
if (model.LText != null) {
if ((model.Location || []).length == 0) {
model.Location = [];
}
// this.setState(prevState => { return { LID: e.id == 'btn_add' ? prevState.LID + 1 : prevState.LID - 1 } }); //This does not worked
model.Location.push({ "LID": model.LID.toString(), "LText": model.LText, "SOrder": model.SOrder, "Inventoried": model.Inventoried.toString() });
this.setState({
model: model,
LID: model.LID + 1 //This also not worked
});
}
}
};
render(){
// Due to confusion of code, i did not add the textboxes codes
<cntrl.WITButton id="btn_add" onWitClick={this.clickAction} />
}
我需要一些东西,比如当我添加时,我应该将唯一 LID 从 1 获取到添加的元素数。我得到的是相同的 ID,即 1。谢谢
【问题讨论】:
-
您的代码格式不正确,而且极难阅读,您要扩展的 cntrl.WITBase 是什么?并且你没有在构造函数中给你的组件任何状态。
-
i) cntrl.WITBase 是一个 Base 类,无需担心。 ii) const witState = this.state;我将 this.state 存储到 witState 并在构造函数中使用。请参考一次。
-
使用 setState 设置 LID 后如何访问它?
-
我将所有值存储到 Location[] 并使用 (dataSource: model.Location) 和 (field: 'LText'),我访问元素并显示它。我只需要在添加元素后,LID 应该由 1 生成到...添加的行数。
-
你明白我的问题了吗?