KnockoutJS
2015-06-23 15:31
701 查看
1. 在Table中添加一条新的item,并绑定数据时,按钮的绑定事件应该这样写:
HTML:
JS:
2.
HTML:
<table id="tblCountryList" border="1" class="tableStyle"> <thead> <tr> <th>Country</th> <th>State</th> </tr> </thead> <!-- Iterating through every list item using foreach of KO --> <tbody data-bind="foreach: Countries"> <tr> <td><input data-bind="value: CountryName" /></td> <td><input data-bind="value: StateName" /></td> </tr> </tbody> </table> <br /> <button data-bind="click: AddCountries.bind($data,'', '', true)">Add State</button>
JS:
$(document).ready(function(){ var completeCountryList = new CountryListViewModel(); var cityArray = [["Beijing", "China"],["New York","US"],["Tokyo","Japan"]]; $.each(cityArray, function(index, item){ completeCountryList.AddCountries(item[0], item[1], false); }); ko.applyBindings(completeCountryList); }); //class for saving the countries and their states function CountryList(countryName, stateName, isUpdated) { var self = this; self.CountryName = countryName; self.StateName = stateName; //IsUpdated is just to keep tab of rows that are added/removed from the table. This is not a SP column self.IsUpdated = isUpdated; } //View Model to combine data from list into the format which view expects function CountryListViewModel() { var self = this; self.Countries = ko.observableArray([]); self.AddCountries = function (countryName, stateName, isUpdated) { self.Countries.push(new CountryList(countryName, stateName, isUpdated)); //hide success message $("#success").hide(); } }
2.
相关文章推荐
- 分享的js代码,从w3c上拓下来的
- json中key大小写转换
- js新增ul
- JS动态标签创建
- JavaScript学习笔记(对象)
- bzoj1013 [JSOI2008]球形空间产生器sphere
- SeaJS入门
- js编写中遇到的问题-TypeError: document.body is null
- js遍历数组的错误方法
- js游戏引擎探索指南之Collie
- json学习小记
- JavaScript instanceof 的用法
- 深入了解javascript第一篇实现篇
- JSP验证码的实现
- 腾讯QQ音乐网页版 音频初始化模块解压混淆js源码
- javascript 数据类型
- JS小知识点三(for gis):openAtStart
- js去除空格(trim方法)
- js 验证身份证号码
- strusts2_json