@@ -273,6 +273,90 @@ <h6>Code</h6>
273
273
274
274
< br />
275
275
276
+ < h4 > Option Group</ h4 >
277
+ < p > This example demonstrates the plugin's capability to combine both standard and grouped (option group) dropdowns.</ p >
278
+
279
+ < div id ="example5 " class ="bs-docs-example ">
280
+ < h4 > Phone finder</ h4 >
281
+
282
+ < select class ="step1 " name ="screen ">
283
+ < option value =""> Screen size</ option >
284
+ < option value ="4 " selected ="selected "> 4.0"</ option >
285
+ < option value ="4.3 "> 4.3"</ option >
286
+ < option value ="4.7 "> 4.7"</ option >
287
+ < option value ="5 "> 5.0"</ option >
288
+ </ select >
289
+ < select class ="step2 " name ="resolution ">
290
+ < option value =""> Screen resolution</ option >
291
+ </ select >
292
+ < select class ="step3 " name ="storage ">
293
+ < option value =""> Storage size</ option >
294
+ </ select >
295
+
296
+ < h4 > Matches < img src ="res/ajax-loader.gif " data-bind ="visible: loading " /> </ h4 >
297
+ < ul data-bind ="foreach: phones, visible: phones().length > 0 ">
298
+ < li >
299
+ < span data-bind ="text: maker "> </ span >
300
+ < span data-bind ="text: model "> </ span >
301
+ </ li >
302
+ </ ul >
303
+ < p data-bind ="visible: phones().length == 0 "> No matches</ p >
304
+ </ div >
305
+
306
+ < h6 > Code</ h6 >
307
+ < div >
308
+ < pre class ="brush: js "> $('#example5').cascadingDropdown({
309
+ selectBoxes: [
310
+ {
311
+ selector: '.step1'
312
+ },
313
+ {
314
+ selector: '.step2',
315
+ source: function(request, response) {
316
+ $.getJSON('/api/resolutionsGrouped', request, function(data) {
317
+ var newData = {};
318
+ $.each(data, function(key, value) {
319
+ newData[key] = $.map(value, function(item, index) {
320
+ return {
321
+ label: item + 'p',
322
+ value: item
323
+ };
324
+ });
325
+ });
326
+
327
+ response(newData);
328
+ });
329
+ }
330
+ },
331
+ {
332
+ selector: '.step3',
333
+ requires: ['.step1', '.step2'],
334
+ requireAll: true,
335
+ source: function(request, response) {
336
+ $.getJSON('/api/storages', request, function(data) {
337
+ response($.map(data, function(item, index) {
338
+ return {
339
+ label: item + ' GB',
340
+ value: item,
341
+ selected: index == 0 // set to true to mark it as the selected item
342
+ };
343
+ }));
344
+ });
345
+ }
346
+ }
347
+ ],
348
+ onChange: function(event, dropdownData) {
349
+ // do stuff
350
+ // dropdownData is an object with values from all the dropdowns in this group
351
+ },
352
+ onReady: function(event, dropdownData) {
353
+ // do stuff
354
+ }
355
+ });</ pre >
356
+ </ div >
357
+
358
+ < br />
359
+
276
360
< h4 > Multiple select</ h4 >
277
361
< p > You can enable multiple select by including the < code > multiple</ code > attribute. The value for the dropdown will then be an array of strings instead of a string. However, you will need to ensure that your backend service supports this type of data.</ p >
278
362
@@ -310,7 +394,7 @@ <h4>Matches <img src="res/ajax-loader.gif" data-bind="visible: loading" /></h4>
310
394
< script type ="text/javascript " src ="https://cdnjs.cloudflare.com/ajax/libs/knockout/2.3.0/knockout-min.js "> </ script >
311
395
< script type ="text/javascript " src ="https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.1/jquery.mockjax.min.js "> </ script >
312
396
< script type ="text/javascript " src ="res/ajax-mocks.js "> </ script >
313
- < script type ="text/javascript " src ="dist/jquery.cascadingdropdown.min. js "> </ script >
397
+ < script type ="text/javascript " src ="dist/jquery.cascadingdropdown.js "> </ script >
314
398
< script type ="text/javascript ">
315
399
function viewmodel ( ) {
316
400
this . phones = ko . observableArray ( [ ] ) ;
@@ -321,11 +405,13 @@ <h4>Matches <img src="res/ajax-loader.gif" data-bind="visible: loading" /></h4>
321
405
example2 = new viewmodel ( ) ,
322
406
example3 = new viewmodel ( ) ,
323
407
example4 = new viewmodel ( ) ;
408
+ example5 = new viewmodel ( ) ;
324
409
325
410
ko . applyBindings ( example1 , document . getElementById ( 'example1' ) ) ;
326
411
ko . applyBindings ( example2 , document . getElementById ( 'example2' ) ) ;
327
412
ko . applyBindings ( example3 , document . getElementById ( 'example3' ) ) ;
328
413
ko . applyBindings ( example4 , document . getElementById ( 'example4' ) ) ;
414
+ ko . applyBindings ( example5 , document . getElementById ( 'example5' ) ) ;
329
415
330
416
// Example 1
331
417
$ ( '#example1' ) . cascadingDropdown ( {
@@ -356,7 +442,7 @@ <h4>Matches <img src="res/ajax-loader.gif" data-bind="visible: loading" /></h4>
356
442
}
357
443
]
358
444
} ) ;
359
-
445
+
360
446
// Example 2
361
447
$ ( '#example2' ) . cascadingDropdown ( {
362
448
selectBoxes : [
@@ -410,7 +496,7 @@ <h4>Matches <img src="res/ajax-loader.gif" data-bind="visible: loading" /></h4>
410
496
}
411
497
]
412
498
} ) ;
413
-
499
+
414
500
// Example 3
415
501
$ ( '#example3' ) . cascadingDropdown ( {
416
502
selectBoxes : [
@@ -516,11 +602,68 @@ <h4>Matches <img src="res/ajax-loader.gif" data-bind="visible: loading" /></h4>
516
602
} ) ;
517
603
}
518
604
} ) ;
605
+
606
+ // Example 5
607
+ $ ( '#example5' ) . cascadingDropdown ( {
608
+ selectBoxes : [
609
+ {
610
+ selector : '.step1'
611
+ } ,
612
+ {
613
+ selector : '.step2' ,
614
+ source : function ( request , response ) {
615
+ $ . getJSON ( '/api/resolutionsGrouped' , request , function ( data ) {
616
+ var newData = { } ;
617
+ $ . each ( data , function ( key , value ) {
618
+ newData [ key ] = $ . map ( value , function ( item , index ) {
619
+ return {
620
+ label : item + 'p' ,
621
+ value : item
622
+ } ;
623
+ } ) ;
624
+ } ) ;
625
+
626
+ response ( newData ) ;
627
+ } ) ;
628
+ }
629
+ } ,
630
+ {
631
+ selector : '.step3' ,
632
+ requires : [ '.step1' , '.step2' ] ,
633
+ requireAll : true ,
634
+ source : function ( request , response ) {
635
+ $ . getJSON ( '/api/storages' , request , function ( data ) {
636
+ response ( $ . map ( data , function ( item , index ) {
637
+ return {
638
+ label : item + ' GB' ,
639
+ value : item ,
640
+ selected : index == 0 // set to true to mark it as the selected item
641
+ } ;
642
+ } ) ) ;
643
+ } ) ;
644
+ }
645
+ }
646
+ ] ,
647
+ onChange : function ( event , dropdownData ) {
648
+ example5 . loading ( true ) ;
649
+ $ . getJSON ( '/api/phones' , dropdownData , function ( data ) {
650
+ example5 . phones ( data ) ;
651
+ example5 . loading ( false ) ;
652
+ } ) ;
653
+ } ,
654
+ onReady : function ( event , dropdownData ) {
655
+ example5 . loading ( true ) ;
656
+ $ . getJSON ( '/api/phones' , dropdownData , function ( data ) {
657
+ example5 . phones ( data ) ;
658
+ example5 . loading ( false ) ;
659
+ } ) ;
660
+ }
661
+ } ) ;
519
662
</ script >
520
663
< script type ="text/javascript " src ="http://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shCore.js "> </ script >
521
664
< script type ="text/javascript " src ="http://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shBrushJScript.js "> </ script >
522
665
< script type ="text/javascript ">
523
666
SyntaxHighlighter . all ( ) ;
524
667
</ script >
525
668
</ body >
526
- </ html >
669
+ </ html >
0 commit comments