學習KnockOut第三篇之Listjavascript
欲看此篇---------------------------------------------可先看上篇。html
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head > 4 <title> mutuDu's List</ title> 5 </head > 6 <body > 7 <form> 8 New Friend: 9 <input /> 10 <button> Add</ button> 11 </form> 12 <p> My Friend:</ p> 13 <select></ select> 14 <div> 15 <button> Remove</ button> 16 <button> Sort</ button> 17 </div> 18 </body > 19 </html> 20 <script src="knockout-2.2.0.js"></ script> 21 <script type="text/javascript"> 22 23 </script>
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head > 4 <title> mutuDu's List</ title> 5 </head > 6 <body > 7 <form> 8 New Friend: 9 <input data-bind="value:itemToAdd" /> 10 <button> Add</ button> 11 </form> 12 <p> My Friend:</ p> 13 <select data-bind="options:items,selectedOptions:selectedItem" multiple="multiple"></select > 14 <div> 15 <button> Remove</ button> 16 <button> Sort</ button> 17 </div> 18 </body > 19 </html> 20 <script src="knockout-2.2.0.js"></ script> 21 <script type="text/javascript"> 22 var ListViewModel = function (items) { 23 this.itemToAdd = ko.observable( ""); 24 this.selectedItem = ko.observableArray([ "Lina"]);//綁定數組裏的元素不能忘了中括號。 25 this.items = ko.observableArray(items); 26 }; 27 ko.applyBindings(new ListViewModel([ "Jelly", "Lina" , "Ando" ])); 28 </script>
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head > 4 <title> mutuDu's List</ title> 5 </head > 6 <body > 7 <form data-bind="submit:addItem"> <!--這裏寫submit綁定,和下面的submit按鈕相對應,執行addItem方法。--> 8 New Friend: 9 <input data-bind="value:itemToAdd" /> 10 <button type="submit"> Add</ button><!--type的類型的是submit--> 11 </form> 12 <p> My Friend:</ p> 13 <select data-bind="options:items,selectedOptions:selectedItem" multiple="multiple"></select > 14 <div> 15 <button> Remove</ button> 16 <button> Sort</ button> 17 </div> 18 </body > 19 </html> 20 <script src="knockout-2.2.0.js"></ script> 21 <script type="text/javascript"> 22 var ListViewModel = function (items) { 23 this.itemToAdd = ko.observable( ""); 24 this.selectedItem = ko.observableArray([ "Lina"]); 25 this.items = ko.observableArray(items); 26 //這裏就是點Add時執行的方法。 27 this.addItem = function() { 28 this.items.push( this.itemToAdd());//push或者sort應該是蠻好理解的吧。 29 this.itemToAdd( ""); 30 }; 31 }; 32 ko.applyBindings(new ListViewModel([ "Jelly", "Lina" , "Ando" ])); 33 </script>
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head > 4 <title> mutuDu's List</ title> 5 </head > 6 <body > 7 <form data-bind="submit:addItem"> 8 New Friend: 9 <input data-bind="value:itemToAdd" /> 10 <button type="submit"> Add</ button> 11 </form> 12 <p> My Friend:</ p> 13 <select data-bind="options:items,selectedOptions:selectedItem" multiple="multiple"></select > 14 <div> 15 <button data-bind ="click:removeSelected">Remove</ button><!--click綁定,執行下面的刪除函數--> 16 <button data-bind="click:sortItems"> Sort</ button><!--click綁定,執行下面的排序函數--> 17 </div> 18 </body > 19 </html> 20 <script src="knockout-2.2.0.js"></ script> 21 <script type="text/javascript"> 22 var ListViewModel = function (items) { 23 this.itemToAdd = ko.observable( ""); 24 this.selectedItem = ko.observableArray([ "Lina"]); 25 this.items = ko.observableArray(items); 26 this.addItem = function() { 27 this.items.push( this.itemToAdd()); 28 this.itemToAdd( ""); 29 }; 30 //這裏是點擊刪除時的方法。 31 this.removeSelected = function() { 32 this.items.removeAll( this.selectedItem()); 33 this.selectedItem([]); //注意括號是不能掉了的。 34 }; 35 //這裏是寫排序的方法 36 this.sortItems = function() { 37 this.items.sort();//上面有說到這個方法。 38 }; 39 }; 40 ko.applyBindings(new ListViewModel([ "Jelly", "Lina" , "Ando" ])); 41 </script>
樓主,這個sort排序是按什麼規律排?java
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head > 4 <title> mutuDu's List</ title> 5 </head > 6 <body > 7 <form data-bind="submit:addItem"> 8 New Friend: 9 <input data-bind="value:itemToAdd,valueUpdate:'afterkeydown'" /><!--當輸入字符時就自動更新ViewModel--> 10 <button type="submit" data-bind="enable:itemToAdd().length>0">Add </button> <!--加了enable綁定--> 11 </form> 12 <p> My Friend:</ p> 13 <select data-bind="options:items,selectedOptions:selectedItem" multiple="multiple"></select > 14 <div> 15 <button data-bind="click:removeSelected,enable:selectedItem().length>0">Remove</ button><!--加了enable綁定--> 16 <button data-bind="click:sortItems,enable:items().length>1">Sort </button> <!--加了enable綁定--> 17 </div> 18 </body > 19 </html> 20 <script src="knockout-2.2.0.js"></ script> 21 <script type="text/javascript"> 22 var ListViewModel = function (items) { 23 this.itemToAdd = ko.observable( ""); 24 this.selectedItem = ko.observableArray([ "Lina"]); 25 this.items = ko.observableArray(items); 26 this.addItem = function() { 27 this.items.push( this.itemToAdd()); 28 this.itemToAdd( ""); 29 }; 30 this.removeSelected = function() { 31 this.items.removeAll( this.selectedItem()); 32 this.selectedItem([]); 33 }; 34 this.sortItems = function() { 35 this.items.sort(); 36 }; 37 }; 38 ko.applyBindings(new ListViewModel([ "Jelly", "Lina" , "Ando" ])); 39 </script>