code_junkie

Using javascript and jquery, to populate related select boxes with array structure

Nov 11th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.50 KB | None | 0 0
  1. cars : [Honda[Accord[Lx, Dx]], [Civic[2dr, Hatchback]],
  2. [Toyota[Camry[Blk, Red]], [Prius[2dr,4dr]]
  3.  
  4. var cars = [
  5. {"makes" : "Honda",
  6. "models" : [
  7. {'Accord' : ["2dr","4dr"]} ,
  8. {'CRV' : ["2dr","Hatchback"]} ,
  9. {'Pilot': ["base","superDuper"] } ]
  10. },
  11. {"makes" :"Toyota",
  12. "models" : [
  13. {'Prius' : ["green","reallyGreen"]} ,
  14. {'Camry' : ["sporty","square"]} ,
  15. {'Corolla' : ["cheap","superFly"] } ]
  16. } ] ;
  17.  
  18. alert(cars[0].models[0].Accord[0]); ---> 2dr
  19.  
  20. var carMakers = [
  21. { name: 'Honda', models: [
  22. { name: 'Accord', features: ['2dr', '4dr'] },
  23. { name: 'CRV', features: ['2dr', 'Hatchback'] },
  24. { name: 'Pilot', features: ['base', 'superDuper'] }
  25. ]},
  26.  
  27. { name: 'Toyota', models: [
  28. { name: 'Prius', features: ['green', 'superGreen'] },
  29. { name: 'Camry', features: ['sporty', 'square'] },
  30. { name: 'Corolla', features: ['cheap', 'superFly'] }
  31. ]}
  32. ];
  33.  
  34. // returns array of elements whose 'prop' property is 'value'
  35. function filterByProperty(arr, prop, value) {
  36. return $.grep(arr, function (item) { return item[prop] == value });
  37. }
  38.  
  39. // populates select list from array of items given as objects: { name: 'text', value: 'value' }
  40. function populateSelect(el, items) {
  41. el.options.length = 0;
  42. if (items.length > 0)
  43. el.options[0] = new Option('please select', '');
  44.  
  45. $.each(items, function () {
  46. el.options[el.options.length] = new Option(this.name, this.value);
  47. });
  48. }
  49.  
  50. // initialization
  51. $(document).ready(function () {
  52. // populating 1st select list
  53. populateSelect($('#maker').get(0), $.map(carMakers, function(maker) { return { name: maker.name, value: maker.name} }));
  54.  
  55. // populating 2nd select list
  56. $('#maker').bind('change', function() {
  57. var makerName = this.value,
  58. carMaker = filterByProperty(carMakers, 'name', makerName),
  59. models = [];
  60.  
  61. if (carMaker.length > 0)
  62. models = $.map(carMaker[0].models, function(model) { return { name: model.name, value: makerName + '.' + model.name} });
  63.  
  64. populateSelect($('#model').get(0), models);
  65. $('#model').trigger('change');
  66. });
  67.  
  68. // populating 3rd select list
  69. $('#model').bind('change', function () {
  70. var nameAndModel = this.value.split('.'),
  71. features = [];
  72.  
  73. if (2 == nameAndModel.length) {
  74. var makerName = nameAndModel[0],
  75. carModel = nameAndModel[1],
  76. carMaker = filterByProperty(carMakers, 'name', makerName);
  77.  
  78. if (carMaker.length > 0) {
  79. var model = filterByProperty(carMaker[0].models, 'name', carModel)
  80.  
  81. if (model.length > 0)
  82. features = $.map(model[0].features, function(feature) { return { name: feature, value: makerName + '.' + carModel + '.' + feature} })
  83. }
  84. }
  85.  
  86. populateSelect($('#feature').get(0), features);
  87. })
  88.  
  89. // alerting value on 3rd select list change
  90. $('#feature').bind('change', function () {
  91. if (this.value.length > 0)
  92. alert(this.value);
  93. })
  94. });
  95.  
  96. <html><head>
  97. <script language="Javascript" src="javascript/jquery-1.2.6.min.js"></script>
  98. <script type="text/JavaScript">
  99. var cars = [
  100. { name: 'Honda', models: [
  101. { name: 'Accord', features: ['2dr', '4dr'] },
  102. { name: 'CRV', features: ['2dr', 'Hatchback'] },
  103. { name: 'Pilot', features: ['base', 'superDuper'] }
  104. ]},
  105. { name: 'Toyota', models: [
  106. { name: 'Prius', features: ['green', 'superGreen'] },
  107. { name: 'Camry', features: ['sporty', 'square'] },
  108. { name: 'Corolla', features: ['cheap', 'superFly'] }
  109. ]
  110. }
  111. ];
  112. $(function() {
  113. var options = '' ;
  114. for (var i = 0; i < cars.length; i++) {
  115. var opt = cars[i].name ;
  116. if (i == 0){ options += '<option selected value="' + opt + '">' + opt + '</option>'; }
  117. else {options += '<option value="' + opt + '">' + opt + '</option>'; }
  118. }
  119. $("#maker").html(options); // populate select box with array
  120.  
  121. var options = '' ;
  122. for (var i=0; i < cars[0].models.length; i++) {
  123. var opt = cars[0].models[0].name ;
  124. if (i==0){options += '<option selected value="' + opt + '">' + opt + '</option>';}
  125. else {options += '<option value="' + opt + '">' + opt + '</option>';}
  126. }
  127. $("#model").html(options); // populate select box with array
  128.  
  129. var options = '' ;
  130. for (var i=0; i < cars[0].models[0].features.length; i++) {
  131. var opt = cars[0].models[0].features[i] ;
  132. if (i==0){options += '<option selected value="' + opt + '">' + opt + '</option>';}
  133. else {options += '<option value="' + opt + '">' + opt + '</option>';}
  134. }
  135. $("#feature").html(options); // populate select box with array
  136.  
  137. $("#maker").bind("click",
  138. function() {
  139. $("#model").children().remove() ; // clear select box
  140. for(var i=0; i<cars.length; i++) {
  141. if (cars[i].name == this.value) {
  142. var options = '' ;
  143. for (var j=0; j < cars[i].models.length; j++) {
  144. var opt= cars[i].models[j].name ;
  145. if (j==0) {options += '<option selected value="' + opt + '">' + opt + '</option>';}
  146. else {options += '<option value="' + opt + '">' + opt + '</option>';}
  147. }
  148. break;
  149. }
  150. }
  151. $("#model").html(options); // populate select box with array
  152.  
  153. $("#feature").children().remove() ; // clear select box
  154. for(var i=0; i<cars.length; i++) {
  155. for(var j=0; j<cars[i].models.length; j++) {
  156. if(cars[i].models[j].name == $("#model").val()) {
  157. var options = '' ;
  158. for (var k=0; k < cars[i].models[j].features.length; k++) {
  159. var opt = cars[i].models[j].features[k] ;
  160. if (k==0){options += '<option selected value="' + opt + '">' + opt + '</option>';}
  161. else {options += '<option value="' + opt + '">' + opt + '</option>';}
  162. }
  163. break;
  164. }
  165. }
  166. }
  167. $("#feature").html(options); // populate select box with array
  168. });
  169.  
  170. $("#model").bind("click",
  171. function() {
  172. $("#feature").children().remove() ; // clear select box
  173. for(var i=0; i<cars.length; i++) {
  174. for(var j=0; j<cars[i].models.length; j++) {
  175. if(cars[i].models[j].name == this.value) {
  176. var options = '' ;
  177. for (var k=0; k < cars[i].models[j].features.length; k++) {
  178. var opt = cars[i].models[j].features[k] ;
  179. if (k==0){options += '<option selected value="' + opt + '">' + opt + '</option>';}
  180. else {options += '<option value="' + opt + '">' + opt + '</option>';}
  181. }
  182. break ;
  183. }
  184. }
  185. }
  186. $("#feature").html(options); // populate select box with array
  187. });
  188. });
  189. </script>
  190. </head> <body>
  191. <div id="selection">
  192. <select id="maker"size="10" style="{width=75px}"></select>
  193. <select id="model" size="10" style="{width=75px}"></select>
  194. <select id="feature" size="10"style="{width=75px}"></select>
  195. </div></body></html>
  196.  
  197. <html lang="en">
  198. <head>
  199. <title>Populate a select dropdown list with jQuery - WebDev Ingredients</title>
  200. <script type="text/javascript" src="js/jquery-1.4.2.js"></script>
  201. <script type="text/javascript">
  202. var types = [
  203. { typeID: 1, name: 'Domestic'},
  204. { typeID: 2, name: 'Import'},
  205. { typeID: 3, name: 'Boat'}
  206. ]
  207. var makes = [
  208. { typeID: 1, makeID: 1, name: 'Chevy'},
  209. { typeID: 1, makeID: 2, name: 'Ford'},
  210. { typeID: 1, makeID: 3, name: 'Delorean'},
  211. { typeID: 2, makeID: 4, name: 'Honda'},
  212. { typeID: 2, makeID: 5, name: 'Toyota'},
  213. { typeID: 2, makeID: 6, name: 'Saab'}
  214. ]
  215. var model = [
  216. { makeID: 1, modelID: 1, name: 'Camaro'},
  217. { makeID: 1, modelID: 2, name: 'Chevelle'},
  218. { makeID: 1, modelID: 3, name: 'Nova'},
  219. { makeID: 2, modelID: 4, name: 'Focus'},
  220. { makeID: 2, modelID: 5, name: 'Galaxie'},
  221. { makeID: 2, modelID: 6, name: 'Mustang'},
  222. { makeID: 4, modelID: 7, name: 'Accord'},
  223. { makeID: 4, modelID: 8, name: 'Civic'},
  224. { makeID: 4, modelID: 9, name: 'Odyssey'},
  225. { makeID: 5, modelID: 10, name: 'Camry'},
  226. { makeID: 5, modelID: 11, name: 'Corolla'}
  227. ]
  228. //
  229. // Put this in a stand alone .js file
  230. //
  231. // returns array of elements whose 'prop' property is 'value'
  232. function filterByProperty(arr, prop, value) {
  233. return $.grep(arr, function (item) { return item[prop] == value });
  234. }
  235. // populates select list from array of items given as objects: { name: 'text', value: 'value' }
  236. function populateSelect(el, items) {
  237. el.options.length = 0;
  238. if (items.length > 0)
  239. el.options[0] = new Option('please select', '');
  240. $.each(items, function () {
  241. el.options[el.options.length] = new Option(this.name, this.value);
  242. });
  243. }
  244. // initialization
  245. $(document).ready(function () {
  246. // populating 1st select list
  247. populateSelect($('#sType').get(0), $.map(types, function(type) { return { name: type.name, value: type.typeID} }));
  248. // populating 2nd select list
  249. $('#sType').bind('change', function() {
  250. var theModels = filterByProperty(makes, 'typeID', this.value);
  251. populateSelect($('#sMake').get(0), $.map(theModels, function(make) { return { name: make.name, value: make.makeID} }));
  252. $('#sMake').trigger('change');
  253. });
  254. // populating 3nd select list
  255. $('#sMake').bind('change', function() {
  256. var theSeries = filterByProperty(model, 'makeID', this.value);
  257. populateSelect($('#sModel').get(0), $.map(theSeries, function(model) { return { name: model.name, value: model.modelID} }));
  258. });
  259. });
  260. </script>
  261. </head>
  262. <body>
  263. Enter values, click submit, and look at the post parameters
  264. <form method="get" action="index.php">
  265. <div id="selection">
  266. <select id="sType" name="type_id" style="{width=75px}"></select>
  267. <select id="sMake" name="make_id" style="{width=75px}"></select>
  268. <select id="sModel" name="model_id" style="{width=75px}"></select>
  269. </div>
  270. <input type="submit">
  271. </form>
  272. </body>
  273. </html>
Add Comment
Please, Sign In to add comment