【问题标题】:Table rows disappearing on going to the next page转到下一页时表格行消失
【发布时间】:2020-10-06 07:43:18
【问题描述】:

我是新来的反应。我正在使用 addrow 方法将值添加到使用 {this.state.rows.map} 创建的表中。该表显示输入字段的值。但是,在按继续按钮转到下一页然后按返回按钮从页面返回时,该行消失。

代码如下:

<Grid item xs={10} direction="row" alignItems="center">
              <table
                className="table table-bordered table-hover"
                id="tab_logic"
              >
                <thead>
                  {this.state.rows.length > 0 ? (
                    <tr>
                      <th className="text-center"> # </th>
                      <th className="text-center"> KPI </th>
                      <th className="text-center"> UOM </th>
                      <th className="text-center"> Base </th>
                      <th className="text-center"> Target </th>
                      <th className="text-center"> Target Date </th>
                      <th className="text-center"> Delete Row </th>
                    </tr>
                  ) : null}
                </thead>
                <tbody>
                  {this.state.rows.map((item, idx) => (
                    <tr id="addr1" key={idx}>
                      <td>{idx + 1}</td>
                      <td>
                        <input
                          type="text"
                          name="Kpi_Before"
                          defaultValue={Kpi_Before}
                          onChange={this.handleChangeRows(idx)}
                          className="form-control"
                          readOnly
                        />
                      </td>
                      <td>
                        <input
                          type="text"
                          name="UOM_Before"
                          defaultValue={UOM_Before}
                          onChange={this.handleChangeRows(idx)}
                          className="form-control"
                          readOnly
                        />
                      </td>
                      <td>
                        <input
                          type="text"
                          name="Base_Before"
                          defaultValue={Base_Before}
                          onChange={this.handleChangeRows(idx)}
                          className="form-control"
                          readOnly
                        />
                      </td>
                      <td>
                        <input
                          type="text"
                          name="Target_Before"
                          defaultValue={Target_Before}
                          onChange={this.handleChangeRows(idx)}
                          className="form-control"
                          readOnly
                        />
                      </td>
                      <td>
                        <input
                          type="text"
                          name="dateTime"
                          defaultValue={dateTime}
                          onChange={this.handleChangeRows(idx)}
                          className="form-control"
                          readOnly
                          size="38"
                        />
                      </td>
                      <td>

更改和后退的代码是这样的:

continue = e => {
e.preventDefault();
      this.props.nextStep();
    }
  };

back = e => {
    e.preventDefault();
    this.props.prevStep();
  };

handleChangeRows = idx => e => {
    const { name, value } = e.target;
    const rows = [...this.state.rows];

    rows[idx] = {
      [name]: value
    };

    this.setState({
      rows
    });
  };



handleAddRow = () => {const item = {
        Kpi_Before: [],
        UOM_Before: "",
        Base_Before: "",
        Target_Before: "",
        dateTime: "",
        rows:[]
      };
      this.setState({
        rows: [...this.state.rows, item]
      });
    }
  };




nextStep = () => {
    const { step } = this.state;
    this.setState({
      step: step + 1
    });
  };

  //Go back to previous step
  prevStep = () => {
    const { step } = this.state;
    this.setState({
      step: step - 1
    });
  };

如何防止行消失并在前后移动时保持不变?

【问题讨论】:

  • 不能使用 idx 作为键,因为当你改变状态(数组)时,项目会改变,但索引会保留。您必须使用某种 id 来映射到每个项目,并且当您在状态内更改它们时保持不变。
  • 您好,感谢您提供的信息,您能举个例子吗?

标签: javascript reactjs react-redux react-router material-ui


【解决方案1】:

当您在页面之间导航或刷新时,您的状态会重新呈现,这意味着状态内的所有内容都会丢失,您可以:

1) 使用 redux 存储更改并从那里检索数据。

2) 使用与 redux 相同但内置的 Context API 来响应。

3) 使用 REST API,这意味着有某种后端来存储值并在组件挂载时获取它们。

【讨论】:

  • 有没有可以举的例子。一个简短的例子就可以了。
  • youtube.com/watch?v=93p3LxR9xfM 这应该解释了您需要了解的有关 redux 的所有信息。它是 react 中非常重要的一部分,因此学习它将在未来对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 2019-02-10
相关资源
最近更新 更多