Guest User

Untitled

a guest
May 17th, 2012
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <link rel="Stylesheet" href="styles.css" />
  6. <script type="text/javascript" src="../lib/jquery-1.4.2.js"></script>
  7. <script type="text/javascript" src="../lib/jquery.tmpl.js"></script>
  8. <script type="text/javascript" src="../lib/json2.js"></script>
  9. <script type="text/javascript" src="../build/output/knockout-latest.min.js"></script>
  10.  
  11. <!-- Referencing jQuery autocomplete -->
  12. <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.css" type="text/css" />
  13. <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.bgiframe.min.js"></script>
  14. <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.js"></script>
  15. </head>
  16. <body>
  17.  
  18. <table>
  19. <thead>
  20. <tr>
  21. <th>Make</th>
  22. <th>Model</th>
  23. <th>Type</th>
  24. <th>Engine</th>
  25. </tr>
  26. </thead>
  27. <tbody data-bind="template: { name: 'vehicleRowTemplate', foreach: lines }"></tbody>
  28. </table>
  29.  
  30. <script type="text/html" id="vehicleRowTemplate">
  31. <tr>
  32. <td><input data-bind="autocomplete: SuggestedMakes, value: Make, valueUpdate: 'afterkeydown'" /></td>
  33. <td><input data-bind="autocomplete: SuggestedModels, value: Model, valueUpdate: 'afterkeydown'" /></td>
  34. <td><input data-bind="autocomplete: SuggestedTypes, value: Type, valueUpdate: 'afterkeydown'" /></td>
  35. <td><input data-bind="autocomplete: SuggestedEngines, value: Engine, valueUpdate: 'afterkeydown'" /></td>
  36. </tr>
  37. </script>
  38.  
  39. <script type="text/javascript">
  40. var SuggestedMakes = ko.observableArray(["Ford", "Renault", "Nissan"]);
  41.  
  42. var vehicleLine = function(){
  43. this.Make = ko.observable('');
  44. this.Model = ko.observable('');
  45. this.Type = ko.observable('');
  46. this.Engine = ko.observable('');
  47.  
  48. this.SuggestedModels = ko.observableArray();
  49. this.SuggestedTypes = ko.observableArray();
  50. this.SuggestedEngines = ko.observableArray();
  51.  
  52. this.Make.subscribe(function() {
  53. var make = this.Make();
  54. this.SuggestedModels([]); // Erase any existing suggestions
  55. setTimeout(function() {
  56. // Using hard-coded data, but simulating slow asynchronous load via setTimeout
  57. // An Ajax call would behave the same.
  58. var result;
  59. switch (make) {
  60. case "Ford": result = ["Capri", "Mondeo", "Focus"]; break;
  61. case "Renault": result = ["Megane", "Cleo", "Scenic"]; break;
  62. case "Nissan": result = ["Micra", "Primera", "Navara"]; break;
  63. }
  64. this.SuggestedModels(result);
  65. }.bind(this), 1000);
  66. }.bind(this));
  67.  
  68. // Todo: also populate this.SuggestedTypes and this.SuggestedEngines in a similar way
  69. };
  70.  
  71. var vehicles = function() {
  72. this.lines = ko.observableArray([new vehicleLine(), new vehicleLine()]);
  73. };
  74.  
  75. ko.bindingHandlers.autocomplete = {
  76. update: function(element, list, allBindings) {
  77. $(element).autocomplete().trigger("setOptions", { data: ko.utils.unwrapObservable(list) });
  78. }
  79. };
  80.  
  81. var vehicleViewModel = new vehicles();
  82. ko.applyBindings(document.body, vehicleViewModel);
  83. </script>
  84. </body>
  85. </html>
Advertisement
Add Comment
Please, Sign In to add comment