43

我正在通过网络创建多人游戏,因此我必须对网络事件做出反应。

我有这个简单的代码,但是 removePlayer 方法不起作用。addPlayer() 工作正常。

<table id="userlist2" class="tablesorter" cellspacing="0">
    <thead>
        <tr>
            <th>Name</th>
            <th>Queue</th>
            <th>Points</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: players">
        <tr>
            <td data-bind="text: name"></td>
            <td data-bind="text: queue"></td>
            <td data-bind="text: score"></td>
        </tr>
    </tbody>
</table>


function PlayerViewModel() {
    var self = this;
    self.players = ko.observableArray();

    self.addPlayer = function (Name, QueuePos, Score) {
        self.players.push({
            name: Name,
            queue: QueuePos,
            score: Score
        });
    }


    self.removePlayer = function (Name) {
        for (var i = 0; i < self.players().length; i++) {

            if (self.players()[i].name == Name) console.log(i);
            self.players().splice(i, 1);
        }
    }
}


players = new PlayerViewModel();
ko.applyBindings(players);

players.addPlayer('Player1', '0', '0');
players.addPlayer('Player2', '0', '0');
players.removePlayer('Player2');

继承人http://jsfiddle.net/xseTc/

4

5 回答 5

79

您必须使用删除功能

self.removePlayer = function (Name) {
    self.players.remove(function(player) {
        return player.name == Name;
    });
    
}

见小提琴

html

<table id="userlist2" class="tablesorter" cellspacing="0">
    <thead>
        <tr>
            <th>Name</th>
            <th>Queue</th>
            <th>Points</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: players">
        <tr>
            <td data-bind="text: name"></td>
            <td data-bind="text: queue"></td>
            <td data-bind="text: score"></td>
        </tr>
    </tbody>
</table>

javascript

function PlayerViewModel() {
    var self = this;
    
    self.players = ko.observableArray();  

    self.addPlayer = function (Name, QueuePos, Score) {
        self.players.push({
            name: Name,
            queue: QueuePos,
            score: Score
        });
    }

    self.removePlayer = function (Name) {
        self.players.remove(function(player) {
            return player.name == Name;
        });
    }
}

players = new PlayerViewModel();
ko.applyBindings(players);

players.addPlayer('Player1', '0', '0');
players.addPlayer('Player2', '0', '0');
players.removePlayer('Player2');
于 2013-07-13T13:00:18.750 回答
12

工作删除功能:

self.removePlayer = function (name) {
    self.players.remove(function(player) { 
        return player.name == name
    });
}
于 2013-07-13T13:03:07.487 回答
3

你的代码很好,除了你有2个错误:

if (self.players()[i].name == Name) console.log(i);
    self.players().splice(i, 1);

首先在这里你正在执行 2 行,所以要执行它们,你需要使用它们,{}所以将其替换为:

if (self.players()[i].name == Name) {
    console.log(i);
    self.players.splice(i, 1);
}

第二次使用splice你需要调用你的 observable 数组,()这样你就可以按如下方式使用它:

self.players.splice(i, 1);

工作演示

于 2013-07-13T13:08:29.840 回答
0

根据淘汰赛网站的最佳来源:https ://knockoutjs.com/documentation/observableArrays.html

您可以直接使用 remove 方法 -- self.players.remove(self.players()[i]);

于 2018-11-22T05:07:31.163 回答
0

如果您将视图模型添加到播放器数组中,如下所示:

self.addPlayer = function (Name, QueuePos, Score) {
    self.players.push(new PlayerVM(Name, QueuePos, Score)));
}

然后你可以像这样删除播放器对象:

self.removePlayer = function (player) {
    self.players.remove(player);
}

或者从播放器 vm 中:

parentVM.players.remove(self);
于 2018-11-20T18:27:57.993 回答