单击时按值分类React Dynamic List

发布于 2025-02-05 18:58:14 字数 3060 浏览 2 评论 0 原文

我正在尝试通过等级,下降,在渲染之前对此数组(参与者)进行排序,但是在添加值覆盖数组中的“以下对象”的问题。

这是一个示例数组,也是我正在使用的代码的一部分,其后是我收到的输出示例。

const participants= [
    {rank: 15, name: 'Alex'},
    {rank: 5, name: 'Eric'},
    {rank: 16, name: 'Jon'},
    {rank: 20, name: 'Zach'},
]
renderRows() {
    var context = this;

    const sortedParticipants = [...this.state.participants].sort((a, b) => b.rank - a.rank);
    console.log(sortedParticipants); //confirms sortedParticipants is sorted

    // return  [...this.state.participants].sort((a, b) => b.rank - a.rank).map(function(o, i) { //ALTERNATE RETURN
    return  this.state.participants.map(function(o, i) {
    /* using this.state.sortedParticipants results in a blank screen */
        return (               
            <tr key={'participants-' + i}>
                <td>
                    <input
                        id='rank'
                        type='text'
                        value={o.rank}
                        onChange={context.handleParticipantChanged.bind(context, i)}
                    />
                </td>
                <td>
                    <input
                        id='name'
                        type='text'
                        value={o.name}
                        onChange={context.handleParticipantChanged.bind(context, i)}
                    />
                </td>                                     
                <td> 
                    <button onClick={context.handleParticipantDeleted.bind(context, i)}> 
                        Delete 
                    </button>
                </td>                    
            </tr>
        );
    });
}

当我运行它时,根据需要将 console.log 确认 sortedparticipant 进行排序,但使用 return this.state.state.state.state.sortedparticipants.map(function(o,i) {给出一个空白屏幕。 如果我使用替代返回运行几乎可以工作,但是如果更改了任何变量,它将旧值推到下一个相应的密钥。例如,数组中的前三个对象将输出;

0: Object { rank: "16", name: "Jon" }​
1: Object { rank: "15", name: "Alex" }​
2: Object { rank: "5", name: "Eric" }

但是,如果您尝试将第一个对象更改为 20,Zach 在表输入中,对象0覆盖对象1。

0: "Zach"​
1: Object { rank: "16", name: "Jon" }​
2: Object { rank: "5", name: "Eric" }

此外,如果您尝试输入 15,Alex 又回到对象1,当前数据覆盖下一个对象。

0: "Zach"​
1: "Alex"​
2: Object { rank: "16", name: "Jon" }

最后,如果我们输入第五参与者( 18,jeff ),则会发生这种情况;

0: "Zach"​
1: "Alex"​
2: Object { rank: "18", name: "Jeff" }​
3: Object { rank: "16", name: "Jon" }

我不明白为什么会发生这种情况,也无法找到任何有帮助的文献。事先感谢您提供的任何建议!

编辑: 这是 handle -participantchanged ;

handleParticipantChanged(i, event) {        
        var participants = this.state.participants;
        participants[i] = event.target.value;
        this.setState({
            participants: participants
        });
    }

I am trying to sort this array (participants) by rank, descending, before render, but am running into issues where added values overwrite following objects in the array.

Here is a sample array and part of the code I'm using, followed by examples of output I'm receiving.

const participants= [
    {rank: 15, name: 'Alex'},
    {rank: 5, name: 'Eric'},
    {rank: 16, name: 'Jon'},
    {rank: 20, name: 'Zach'},
]
renderRows() {
    var context = this;

    const sortedParticipants = [...this.state.participants].sort((a, b) => b.rank - a.rank);
    console.log(sortedParticipants); //confirms sortedParticipants is sorted

    // return  [...this.state.participants].sort((a, b) => b.rank - a.rank).map(function(o, i) { //ALTERNATE RETURN
    return  this.state.participants.map(function(o, i) {
    /* using this.state.sortedParticipants results in a blank screen */
        return (               
            <tr key={'participants-' + i}>
                <td>
                    <input
                        id='rank'
                        type='text'
                        value={o.rank}
                        onChange={context.handleParticipantChanged.bind(context, i)}
                    />
                </td>
                <td>
                    <input
                        id='name'
                        type='text'
                        value={o.name}
                        onChange={context.handleParticipantChanged.bind(context, i)}
                    />
                </td>                                     
                <td> 
                    <button onClick={context.handleParticipantDeleted.bind(context, i)}> 
                        Delete 
                    </button>
                </td>                    
            </tr>
        );
    });
}

When I run it, the console.log confirms sortedParticipants is sorted as desired, but using return this.state.sortedParticipants.map(function(o, i) { gives a blank screen.
If I run with the alternate return it almost works fine, but if any variable is changed, it pushes the old value to the next corresponding key. For example, the first three objects in the array will output;

0: Object { rank: "16", name: "Jon" }​
1: Object { rank: "15", name: "Alex" }​
2: Object { rank: "5", name: "Eric" }

But if you try to change the first object to 20, Zach in the table inputs, object 0 overwrites object 1.

0: "Zach"​
1: Object { rank: "16", name: "Jon" }​
2: Object { rank: "5", name: "Eric" }

Further, if you try to enter 15, Alex back into object 1, the current data overwrites the next object.

0: "Zach"​
1: "Alex"​
2: Object { rank: "16", name: "Jon" }

Finally, if we enter a 5th participant (18, Jeff), this happens;

0: "Zach"​
1: "Alex"​
2: Object { rank: "18", name: "Jeff" }​
3: Object { rank: "16", name: "Jon" }

I do not understand why this is happening and have been unable to find any literature that helps just yet. Thanks in advance for any advice you can offer!

EDIT:
Here is handleParticipantChanged;

handleParticipantChanged(i, event) {        
        var participants = this.state.participants;
        participants[i] = event.target.value;
        this.setState({
            participants: participants
        });
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2025-02-12 18:58:14

sortedparticipant

解决问题的第一部分, sortedParticipant 看起来正确 this.state.state.sortedparticipants 没有,因为您没有存储输出 .sort 方法中的该状态值。要么将其存储在此处,或者最好是在没有 this.State的变量名称中。前缀。

const sortedParticipants = [...this.state.participants].sort((a, b) => b.rank - a.rank);
console.log(sortedParticipants); //confirms sortedParticipants is sorted

以上是有道理的,因为您正在记录由此产生的分类参与者数组。

return  this.state.participants.map(function(o, i) {
/* using this.state.sortedParticipants results in a blank screen */

您在此处获得空白屏幕,因为您从未将 sortedParticipants 数组存储到 this.state 对象中。它仅作为局部变量存在,因此您可以将其引用为 sortedParticipant 而不是 this.state.sortedParticipants (或将结果存储在状态下,但这不会在这里有意义)。

handerparticipantchanged

您正在看到该值仅由名称代替,因为您将整个值设置为新字符串,而不仅仅是名称属性。

请注意,...

participants[0] = "hello";
// [ "hello", { rank: 10, name: "Jesse" }, { rank: 11, name: "Emily" } ]

和此...

participants[0].name = "hello";
// [ { rank: 9, name: "hello" }, { rank: 10, name: "Jesse" }, ... ]

您想在该索引上替换参与者的属性 name 属性,而不是该索引处的整个值。这是一个非常简单的修复:

// change this
participants[i] = event.target.value;

// to this
participants[i].name = event.target.value;

sortedParticipants

Addressing the first part of your question, sortedParticipants looks right while this.state.sortedParticipants does not, since you aren't storing the output of the .sort method into that state value. Either store it there, or maybe preferably, just reference the variable name without the this.state. prefix.

const sortedParticipants = [...this.state.participants].sort((a, b) => b.rank - a.rank);
console.log(sortedParticipants); //confirms sortedParticipants is sorted

The above makes sense, as you are logging the resulting, sorted participants array.

return  this.state.participants.map(function(o, i) {
/* using this.state.sortedParticipants results in a blank screen */

You get a blank screen here since you never store the sortedParticipants array into the this.state object. It only exists as the local variable, so you can reference it as just sortedParticipants rather than this.state.sortedParticipants (or store the result in state, but that wouldn't make sense here).

handleParticipantChanged

You are seeing that the value is being replaced by just the name, because you are setting the entire value to the new string rather than just the name property.

Notice the difference between...

participants[0] = "hello";
// [ "hello", { rank: 10, name: "Jesse" }, { rank: 11, name: "Emily" } ]

...and this...

participants[0].name = "hello";
// [ { rank: 9, name: "hello" }, { rank: 10, name: "Jesse" }, ... ]

You want to replace the name property of the participant at that index, not the entire value at that index. This is a pretty easy fix:

// change this
participants[i] = event.target.value;

// to this
participants[i].name = event.target.value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文