[英]Remove Function Javascript, knockout, c#, asp.net
您好,即時通訊正在嘗試創建類似於計划撲克的內容。 目前,我擁有它,因此您可以創建一個游戲,而我正嘗試制作它,以便您可以刪除游戲。 目前,我的刪除功能會刪除游戲,但只有在您單擊刷新后才會暫時將其刪除。 我知道我必須做的只是不怎么做。 我需要刪除按鈕才能從控制器的可觀察數組中刪除游戲。
這是我的代碼
索引(創建游戲)
<html>
<head>
<title>Planning Poker</title>
<style>
.inlinetext {
display: inline;
}
</style>
<script src="Scripts/jquery-2.1.1.js"></script>
<script src="Scripts/knockout-3.1.0.js"></script>
<script type="text/javascript">
$(function () {
$('#button').on('click', function (data) {
$.post('data/games/create/?title=5', function (d) { console.log(d) });
})
});
</script>
</head>
<body>
<h3 class='inlinetext'> Create Game: </h3>
<input type="text" id="testtext" name="ime">
<button id="button" >Create</button>
</body>
</html>
游戲列表(列出游戲的頁面,我希望能夠從此列表中刪除游戲)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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.removeGames">Remove</a>
</li>
</ul>
</body>
</html>
Gamelist.js
function AppViewModel() {
var self = this;
self.games = ko.observableArray([]);
$.getJSON("/data/games", function (d) {
self.games(d);
});
self.removeGames = function () {
self.games.remove(this);
}
}
$(function () {
ko.applyBindings(new AppViewModel());
});
控制者
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;
}
}
Knockout教程的“使用列表和集合”的第4步完全演示了您要實現的目標。
self.removeGames = function(game) { self.games.remove(game) }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.