Description
Extended SimpleList sample: addition of an element is more complicated, delete and sort operations are added, work with multiple elements is available.
The original example: http://knockoutjs.com/examples/betterList.htmlExtended SimpleList sample: addition of an element is more complicated, delete and sort operations are added, work with multiple elements is available.
The original example: http://knockoutjs.com/examples/betterList.htmlpublic class BetterListModel { public string ItemToAdd { get; set; } public List<string> AllItems { get; set; } public List<string> SelectedItems { get; set; } public void AddItem() { if (!string.IsNullOrEmpty(ItemToAdd) && !AllItems.Contains(ItemToAdd)) AllItems.Add(ItemToAdd); ItemToAdd = ""; } public void RemoveSelected() { AllItems.RemoveAll(item => SelectedItems.Contains(item)); SelectedItems.Clear(); } public void SortItems() { AllItems.Sort(); } }
@using PerpetuumSoft.Knockout @model KnockoutMvcDemo.Models.BetterListModel @{ var ko = Html.CreateKnockoutContext(); } @using (ko.Html.Form("AddItem", "BetterList", null, new { id = "myform" })) { <span>Add item: @ko.Html.TextBox(m => m.ItemToAdd).ValueUpdate(KnockoutValueUpdateKind.AfterKeyDown)</span> <button type="submit" @ko.Bind.Enable(m => m.ItemToAdd.Length > 0)>Add</button> <p>Your values:</p> @ko.Html.ListBox(m => m.AllItems, new { width = 100, size = 10 }).SelectedOptions(m => m.SelectedItems) <div> @ko.Html.Button("Remove", "RemoveSelected", "BetterList").Enable(m => m.SelectedItems.Count > 0) @ko.Html.Button("Sort", "SortItems", "BetterList").Enable(m => m.AllItems.Count > 1) </div> } <script type="text/javascript"> $('#myform').ajaxForm(); </script> @ko.Apply(Model)
public class BetterListController : BaseController { public ActionResult Index() { InitializeViewBag("Better list"); var model = new BetterListModel { AllItems = new List<string> { "Fries", "Eggs Benedict", "Ham", "Cheese" }, SelectedItems = new List<string> { "Ham" } }; return View(model); } public ActionResult AddItem(BetterListModel model) { model.AddItem(); return Json(model); } public ActionResult RemoveSelected(BetterListModel model) { model.RemoveSelected(); return Json(model); } public ActionResult SortItems(BetterListModel model) { model.SortItems(); return Json(model); } }
<form data-bind="submit : function() {executeOnServer(viewModel, '/BetterList/AddItem');}" id="myform"> <span>Add item: <input data-bind="value : ItemToAdd,valueUpdate : 'afterkeydown'" name="ItemToAdd" type="text" /></span> <button type="submit" data-bind="enable : (ItemToAdd().length > 0)">Add</button> <p>Your values:</p> <select data-bind="options : AllItems,selectedOptions : SelectedItems" multiple="multiple" size="10" width="100"></select> <div> <button data-bind="click : function() {executeOnServer(viewModel, '/BetterList/RemoveSelected');},enable : (SelectedItems().length > 0)">Remove</button> <button data-bind="click : function() {executeOnServer(viewModel, '/BetterList/SortItems');},enable : (AllItems().length > 1)">Sort</button> </div> </form> <script type="text/javascript"> $('#myform').ajaxForm(); </script> <script type="text/javascript"> var viewModelJs = {"ItemToAdd":"","AllItems":["Fries","Eggs Benedict","Ham","Cheese"],"SelectedItems":["Ham"]}; var viewModel = ko.mapping.fromJS(viewModelJs); ko.applyBindings(viewModel); </script>