Knockout擷取數組元素索引的2種方法,在MVC中實現

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   使用   

在遍曆數組、集合的時候,通常要擷取元素的索引,本篇體驗使用Knockout擷取索引的2種方法。

 

假設有這樣的一個模型:

namespace UseIndex.Models{    public class Student    {        public int Id { get; set; }        public string Name { get; set; }    }}

 

在HomeController中,先類比一個Student的集合,在投影出Name屬性的集合,最後以Json返回給前台視圖。

using System.Collections.Generic;using System.Linq;using System.Web.Mvc;using UseIndex.Models;namespace UseIndex.Controllers{    public class HomeController : Controller    {        public ActionResult Index()        {            return View();        }        public JsonResult GetStudentNames()         {            var students = new List<Student>()            {                new Student(){Id = 1, Name = "小明"},                new Student(){Id = 2, Name = "嘯天"}            };            var names = from student in students                select new {student.Name};            return Json(names, JsonRequestBehavior.AllowGet);        }    }}

 

在Home/Index.cshtml中:

@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Index</title>    <script src="~/Scripts/jquery-1.8.2.min.js"></script>    <script src="~/Scripts/knockout-2.2.0.js"></script>    <script type="text/javascript">        $(function() {            var vm = new ViewModel();            ko.applyBindings(vm);            vm.init();        });        function ViewModel() {            var self = this;            self.items = ko.observableArray([]);            self.showItemIndex = function(item, event) {                var context = ko.contextFor(event.target); //擷取繫結元素的上下文;event.target綁定View Model的DOM元素                var index = context.$index();                alert("當前索引是:" + index);            };            self.init = function() {                $.getJSON(‘@Url.Action("GetStudentNames","Home")‘, function(data) {                    var arr = [];                    for (var i = 0; i < data.length; i++) {                        arr[i] = data[i].Name;                    }                    self.items(arr);                });            };        }    </script>    <style type="text/css">        //樣式略    </style></head><body>    <div>        <table>            <thead>                <tr>                    <th>索引</th>                    <th>編號</th>                    <th>名稱</th>                    <th>在View Model中的索引</th>                </tr>            </thead>            <tbody data-bind="foreach: items">                <tr>                    <td data-bind="text: $index"></td>                    <td data-bind="text: $index() + 1"></td>                    <td data-bind="text: $data"></td>                    <td><a href="#" data-bind="click: $root.showItemIndex">點擊顯示索引</a></td>                </tr>            </tbody>        </table>    </div></body></html>

以上,$data是指當前集合元素。$root是指根context中的ViewModel。

 

運行:

 

總結

擷取集合或數組元素的索引有2種方式:
1、通過data-bind="text: $index"或data-bind="text: $index() + 1"
2、在View Model中通過方法來獲得:先擷取到上下文,再context.$index()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.