cf. knockout.js

サンプルアプリケーション

Your borwser is not supporting object tag. Please use one of the latest browsers.
Go to /KnockoutExam/knockoutEx5_foreach.html


ソースコード

基本的に見ればわかるでしょ。HTML 中の $index や $parent については HTML KnockoutJS data-bind4 Context を参照

https://github.com/kagyuu/KnockoutExam/blob/master/knockoutEx5_foreach.html

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8">
    <title>knockdown.js Ex5</title>
    <style>
table{
	border-collapse: collapse;
	border-top: 1px solid gray;
	border-right: 1px solid gray;
	border-bottom: 1px solid gray;
}
th{
	padding: 6px; 
	text-align: left;
	vertical-align: top;
	color: #666666;
	background: #eee;
	border-bottom: 1px dotted #999;
	border-left: 1px solid #ccc;
}
td{
	padding: 6px;
	border-bottom: 1px dotted #999;
	border-left: 1px solid #ccc;
}
    </style>
    <script type="text/javascript" charset="UTF-8" src="js/lib/require.js" data-main="js/knockoutEx5_init.js"></script>
  </head>
  <body>
    First name: <input data-bind="value: firstName"/> <br/>
    Last name : <input data-bind="value: lastName"/> <br/>
    <button data-bind="click: addPerson, disable: disableAddPerson">Add</button>
	<table>
		<thead>
			<tr>
				<th></th>
				<th><a data-bind="click: sortByFristName">First name&#8645;</a></th>
				<th><a data-bind="click: sortByLastName">Last name&#8645;</a></th>
				<th></th>
			</tr>
		</thead>
		<tbody data-bind="foreach: people">
			<!-- foreach 節の中では this は people。view-model は $parent -->
			<tr data-bind="style: {'background-color' : ( $index() % 2 === 0 ? 'azure' : 'white')}">
				<td data-bind="text: $index() + 1"></td>
				<td data-bind="text: firstName"></td>
				<td data-bind="text: lastName"></td>
				<td><button data-bind="click: $parent.removePerson">delete</button></td>
			</tr>
		</tbody>
	</table>  
  </body>
</html>

https://github.com/kagyuu/KnockoutExam/blob/master/js/knockoutEx5_init.js

/*
 * アプリケーションのイニシャライザ.
 */
require.config({

  // Javascript の Base Url
  baseUrl: 'js/lib',

  // ライブラリのパスの別名を定義する
  paths: {
   'app':      '../',
   'knockout' : 'knockout-3.2.0',
  },

  // AMDに対応してないモジュールを読み込む
  shim: {
  }
});

require(['knockout', 'app/knockoutEx5_vmodel', 'domReady!'],
  function(ko, vmdl) {
    ko.applyBindings(new vmdl());
  }
);

https://github.com/kagyuu/KnockoutExam/blob/master/js/knockoutEx5_vmodel.js

/*
 * アプリケーションの View Model.
 */
define(['knockout'], function(ko) {

	return function appViewModel() {
		var self = this;
		
		self.firstName = new ko.observable("");
		self.lastName = new ko.observable("");
	    self.disableAddPerson = ko.computed(function() {
	    	return '' === self.firstName() || '' === self.lastName();
	    });		
		
		self.people = new ko.observableArray([
			{ firstName: 'Bert', lastName: 'Bertington' },
			{ firstName: 'Denise', lastName: 'Dentiste' },
			{ firstName: 'Charles', lastName: 'Charlesforth' }
		]);
		
		self.sortByFirstNameAsc = 1;
		self.sortByFristName = function() {
			self.people.sort(function(o1, o2){
				return o1.firstName.localeCompare(o2.firstName) * self.sortByFirstNameAsc;
			});
			self.sortByFirstNameAsc *= -1;
		};
		
		self.sortByLastNameAsc = 1;
		self.sortByLastName = function() {
			self.people.sort(function(o1, o2){
				return o1.lastName.localeCompare(o2.lastName) * self.sortByLastNameAsc;
			});
			self.sortByLastNameAsc *= -1;
		};
		
		self.addPerson = function() {
			var person = new Object();
			person.firstName = self.firstName();
			person.lastName = self.lastName();
			
			self.firstName("");
			self.lastName("");
			
			self.people.push(person)
		}
		
		self.removePerson = function(person) {
			self.people.remove(person);
		}
	};
});

コメントタグによる foreach


Your borwser is not supporting object tag. Please use one of the latest browsers.
Go to /KnockoutExam/knockoutEx5_foreach2.html


https://github.com/kagyuu/KnockoutExam/blob/master/knockoutEx5_foreach2.html

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8">
    <title>knockdown.js Ex5</title>
    <style>
table{
	border-collapse: collapse;
	border-top: 1px solid gray;
	border-right: 1px solid gray;
	border-bottom: 1px solid gray;
}
th{
	padding: 6px; 
	text-align: left;
	vertical-align: top;
	color: #666666;
	background: #eee;
	border-bottom: 1px dotted #999;
	border-left: 1px solid #ccc;
}
td{
	padding: 6px;
	border-bottom: 1px dotted #999;
	border-left: 1px solid #ccc;
}
    </style>
    <script type="text/javascript" charset="UTF-8" src="js/lib/require.js" data-main="js/knockoutEx5_init.js"></script>
  </head>
  <body>
	<table>
		<thead>
			<tr>
				<th></th>
				<th><a data-bind="click: sortByFristName">First name&#8645;</a></th>
				<th><a data-bind="click: sortByLastName">Last name&#8645;</a></th>
				<th></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td></td>
				<td><input data-bind="value: firstName"/></td>
				<td><input data-bind="value: lastName"/></td>
				<td><button data-bind="click: addPerson, disable: disableAddPerson">Add</button></td>
			</tr>
			<!-- ko foreach: people -->
			<tr data-bind="style: {'background-color' : ( $index() % 2 === 0 ? 'azure' : 'white')}">
				<td data-bind="text: $index() + 1"></td>
				<td data-bind="text: firstName"></td>
				<td data-bind="text: lastName"></td>
				<td><button data-bind="click: $parent.removePerson">delete</button></td>
			</tr>
			<!-- /ko -->
		</tbody>
	</table>  
  </body>
</html>


HTML#Knockout


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2015-03-07 (土) 21:33:18 (3530d)
Short-URL: http://at-sushi.com/pukiwiki/index.php?cmd=s&k=e4c3f6e2ec
ISBN10
ISBN13
9784061426061