zoukankan      html  css  js  c++  java
  • knockoutjs表格增加更新删除

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>Index2</title>
    <script type="text/javascript" src="js/knockout-2.2.0.debug.js"></script>
    <script type="text/javascript" src="js/jquery-2.1.0.js"></script>
    <script type="text/javascript">
    //定义user数据对象
    function UserViewModel(id, name, score) {
    var self = this;
    self.id = ko.observable(id);
    self.name = ko.observable(name);
    self.score = ko.observable(score);
    }
    //定义ViewModel
    function ViewModel() {
    var self = this;
    self.users = ko.observableArray(); //添加动态监视数组对象
    self.removeUser = function(user) {
    self.users.remove(user);
    }
    self.totalscore = ko.computed(function() {
    var total = 0;
    $.each(self.users(), function(i, u) {
    total += u.score();
    })
    return total;
    });
    };
    $(function() {
    var vm = new ViewModel();
    //预先添加一些数据
    vm.users.push(new UserViewModel("d1", "rohelm", 121));
    vm.users.push(new UserViewModel("d2", "halower", 125));
    $("#btnAddUser").click(
    function() {
    vm.users.push(new UserViewModel($("#u_id").val(), $(
    "#u_name").val(), parseInt($("#u_score").val())));
    });
    $("#btnUpdateScore").click(
    function() {
    vm.users()[vm.users().length - 1].score(
    parseInt($("#u_score").val())).name(
    $("#u_name").val()).id($("#u_id").val());
    });
    ko.applyBindings(vm);
    });
    </script>
    </head>
    
    <body>
    <section style="margin: 250px">
    <section>
    ID <input type="text" id="u_id" style=" 30px"> Name <input
    type="text" id="u_name" style=" 30px"> Score <input
    type="text" id="u_score" style=" 30px"> <br /> <input
    value="Add" id="btnAddUser" type="button"
    style=" 200px; background-color: #ff6a00;" /> <br /><span
    data-bind="text: users().length"> </span> 条--------------合计 <span
    data-bind="text: totalscore"> </span></section>
    <section>
    <table>
    <thead>
    <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Score</th>
    <th>Option</th>
    </tr>
    </thead>
    <tbody data-bind="foreach: users">
    <tr>
    <td><span data-bind="text: id"> </span></td>
    <td><span data-bind="text: name"> </span></td>
    <td><span data-bind="text: score"> </span></td>
    <td><a href='#' data-bind="click: $root.removeUser">
    Remove </a></td>
    </tr>
    </tbody>
    </table>
    <input value="Update测试" id="btnUpdateScore" type="button"
    style=" 200px; background-color: #ff6a00;" /> <br />
    </section>
    </section>
    </body>
    
    </html>

    界面:修改于http://www.aizhengli.com/knockoutjs-shizhan/88/knockoutjs-shishi.html

  • 相关阅读:
    一个别人的心得(转发的)
    常见的游戏设计技术
    查看更新
    xml,json和各种序列化工具的对比
    python游戏环境搭建
    快速制作游戏
    子网和掩码
    nat
    pycharm使用技巧
    IP的面向无连接状态
  • 原文地址:https://www.cnblogs.com/jiduoduo/p/6192548.html
Copyright © 2011-2022 走看看