Javascript 如何使删除按钮永久删除。
到目前为止,索引页面允许您创建一个游戏。然后将游戏发布到/data/Games(Gamelist.html)。我想有一个按钮,永久删除列表中的游戏。我目前有一个删除按钮,但它不做任何事情 索引(从此处创建游戏)Javascript 如何使删除按钮永久删除。,javascript,asp.net,knockout.js,Javascript,Asp.net,Knockout.js,到目前为止,索引页面允许您创建一个游戏。然后将游戏发布到/data/Games(Gamelist.html)。我想有一个按钮,永久删除列表中的游戏。我目前有一个删除按钮,但它不做任何事情 索引(从此处创建游戏) 敲除将自动传回当前项作为单击事件绑定中的参数 self.removeGame = function(game) { self.games.remove(game); } 请使用或其他。。。。。。除了把你的代码放在这里。另外,请注意,在你的js示例中,你使用的是self.game
敲除将自动传回当前项作为单击事件绑定中的参数
self.removeGame = function(game) {
self.games.remove(game);
}
请使用或其他。。。。。。除了把你的代码放在这里。另外,请注意,在你的js示例中,你使用的是self.game而不是self.games…它会删除,但当我点击refresh时,它仍然在那里。我希望它在我点击Delete后永远消失。嗯,是的……你还必须从数据源中删除它。您需要在self.removeGame方法中触发对路由GMController的异步调用。我将如何触发该调用,类似于您初始化应用程序的方式。在removeGame方法中,调用$.getJSON方法并将游戏传递给它以删除。在控制器或服务调用中,处理从持久数据中删除游戏的操作。就目前情况而言,您将始终将D&D作为一个游戏,因为它只是控制器中的一个静态参数。
using PlanningPoker.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace PlanningPoker.Controllers
{
public class GMController : ApiController
{
private static List<Game> games = new List<Game>() {
new Game() {
ID = Guid.NewGuid(),
Title = "D&D"
}
};
[Route("data/games")]
public IEnumerable<Game> GetAllGames() {
return games;
}
[Route("data/games/create"), HttpPost]
public Guid CreateGame(string title) {
Game g = new Game() {
ID = Guid.NewGuid(),
Title = title
};
games.Add(g);
return g.ID;
}
}
}
<title>Game List</title>
<script src="Scripts/jquery-2.1.1.js"></script>
<script src="Scripts/knockout-3.1.0.js"></script>
<script src="Gamelist.js"></script>
</head>
<body>
<h4>Games</h4>
<ul data-bind="foreach: $data.games">
<li>
Game <span data-bind="text: $index"> </span>:
<span data-bind="text: Title"> </span>
<a href="#" data-bind="click: $parent.removeGame">Remove</a>
</li>
</ul>
<button data-bind="click: addGame">Add</button>
</body>
</html>
function AppViewModel() {
var self = this;
self.games = ko.observableArray([]);
$.getJSON("/data/games", function (d) {
self.games(d);
});
self.addGame = function () {
self.game.push({ name: "Created On " + new Date() });
};
self.removeGame = function () {
self.game.remove(this);
}
}
$(function () {
ko.applyBindings(new AppViewModel());
});
self.removeGame = function(game) {
self.games.remove(game);
}