【问题标题】:Button inside a table does not work clicking at all表格内的按钮根本不起作用
【发布时间】:2021-02-24 04:19:55
【问题描述】:

我有一个按钮,当我点击它时会改变它在表格中的状态,当我加载页面并点击任何一个时,它只会激活上面的第一个而其他的不会,而且它也不会返回到以前的状态,我想知道我做错了什么。

在上图中,当我点击任何一个时,它只会激活第一个并且不会将其停用,

而且3号已经激活了,我不能动,我觉得地图有问题什么的……

{
    this.state.dataSource.map((data) => {
        function ChangeIcon(e) {
            e.preventDefault();
            if (data.statusPedido === "Em aberto") {
                var btn = document.getElementById("btn-times");
                btn.style.display = "none";

                var btn2 = document.getElementById("btn-check");
                btn2.style.display = "block";
            }
        }
        return (
            <tr>
                <td>{data.nrPedido}</td>
                <td>{data.dataPedido}</td>
                <td>{data.dtEntrega}</td>
                <td>{data.totalDoses}</td>
                <td>{data.totalDosesConvencionais}</td>
                <td>{data.totalDosesCervicais}</td>
                <td>{data.rota}</td>
                <td>{data.periodoLactacao}</td>
                <td>{data.plantelMatrizes}</td>
                <td>{data.porcentagemReposicaoLeitoas}</td>

                <td>
                    {data.statusPedido === "Em aberto" ? (
                        <div>
                            <button
                                className="btn"
                                onClick={ChangeIcon}
                                style={{ backgroundColor: "transparent" }}
                                id="btn-times"
                            >
                                <FontAwesome name="times" />
                            </button>
                            <button
                                className="btn"
                                id="btn-check"
                                style={{ display: "none", backgroundColor: "transparent" }}
                            >
                                <FontAwesome name="check" />
                            </button>
                        </div>
                    ) : (
                        <button
                            className="btn"
                            style={{ backgroundColor: "transparent" }}
                            id="btn-check"
                        >
                            <FontAwesome name="check" />{" "}
                        </button>
                    )}
                </td>
            </tr>
        );
    });
}

【问题讨论】:

  • 您缺少结束标记,我在格式编辑中修复了它。
  • 示例中的tag我没有复制,但是关闭了呵呵

标签: javascript reactjs


【解决方案1】:

在您的示例中,您呈现了由 map 函数生成的元素列表,但您忘记为元素分配键(在您的情况下为 tr)。键帮助 React 识别哪些项目已更改、添加或删除。如果没有键,你可能会遇到很多难以捕捉的错误,因为 React 不知道要更新哪一行。键的最佳选择是标识此表中每一行的 ID 或其他唯一字符串。

【讨论】:

  • 我正在尝试这样做,但我没有让它工作。
【解决方案2】:

你是对的!问题在于 map 方法。原因是您没有为每个返回的元素提供唯一的键属性。 你可以这样修复它:

{
    // Add an index argument
    this.state.dataSource.map((data, index) => {
        function ChangeIcon(e) {
            e.preventDefault();
            if (data.statusPedido === "Em aberto") {
                var btn = document.getElementById("btn-times");
                btn.style.display = "none";

                var btn2 = document.getElementById("btn-check");
                btn2.style.display = "block";
            }
        }
        return (
        // Use that index here as key property
            <tr key={index}>
                <td>{data.nrPedido}</td>
                <td>{data.dataPedido}</td>
                <td>{data.dtEntrega}</td>
                <td>{data.totalDoses}</td>
                <td>{data.totalDosesConvencionais}</td>
                <td>{data.totalDosesCervicais}</td>
                <td>{data.rota}</td>
                <td>{data.periodoLactacao}</td>
                <td>{data.plantelMatrizes}</td>
                <td>{data.porcentagemReposicaoLeitoas}</td>

                <td>
                    {data.statusPedido === "Em aberto" ? (
                        <div>
                            <button
                                className="btn"
                                onClick={ChangeIcon}
                                style={{ backgroundColor: "transparent" }}
                                id="btn-times"
                            >
                                <FontAwesome name="times" />
                            </button>
                            <button
                                className="btn"
                                id="btn-check"
                                style={{ display: "none", backgroundColor: "transparent" }}
                            >
                                <FontAwesome name="check" />
                            </button>
                        </div>
                    ) : (
                        <button
                            className="btn"
                            style={{ backgroundColor: "transparent" }}
                            id="btn-check"
                        >
                            <FontAwesome name="check" />{" "}
                        </button>
                    )}
                </td>
            </tr>
        );
    });
}

要了解更多关于 React 中的 Lists 和 Keys,你可以查看The official documentation

【讨论】:

  • 我添加了相同的代码,但它不起作用,格式相同
【解决方案3】:

我认为这与您正在执行的条件渲染有关。在这种情况下,试试这个:

{data.statusPedido === "Em aberto" ? (
                        <button
                            className="btn"
                            onClick={ChangeIcon}
                            style={{ backgroundColor: "transparent" }}
                            id="btn-times"
                        >
                            <FontAwesome name="times" />
                        </button>
                ) : (
                   <button
                            className="btn"
                            id="btn-check"
                            style={{ display: "none", backgroundColor: "transparent" }}
                        >
                            <FontAwesome name="check" />
                        </button>
                )}

现在,它将返回“检查”按钮或“X”按钮。另外:

  • 避免用大写字母命名函数,这是为类保留的。您的代码仍然可以工作,但它不符合标准。
  • 您甚至不需要调用 event.preventDefault(),因为按钮没有“提交”或“清除”输入等默认行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多