Advertisement
icharge

html picklist sample

Oct 4th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.79 KB | None | 0 0
  1. <select name="somethinglist[]" id="somethingList" size="10" multiple style="width:200px;height:300px">
  2.     <option value="1" selected>item 1</option>
  3.     <option value="2">item 2</option>
  4. </select>
  5.  
  6.  
  7. <script>
  8. jQuery.fn.filterByText = function(textbox, ListBox, selectSingleMatch) {
  9.     return this.each(function() {
  10.         var select = this;
  11.         var options = [];
  12.         $(select).find('option').each(function() {
  13.             options.push({
  14.                 value: $(this).val(),
  15.                 text: $(this).text(),
  16.                 className: $(this).attr('class')
  17.             });
  18.         });
  19.         $(select).data('options', options);
  20.  
  21.         $(textbox).bind('change keyup', function() {
  22.             //var options = $(select).empty().scrollTop(0).data('options');
  23.             var options = [];
  24.             $(select).find('option').each(function() {
  25.                 if ($(this).attr('selected') === undefined)
  26.                 {
  27.                     options.push({
  28.                         value: $(this).val(),
  29.                         text: $(this).text(),
  30.                         className: $(this).attr('class')
  31.                     });
  32.                 }
  33.             });
  34.  
  35.             var search = $(this).val().trim();
  36.             var regex = new RegExp(search, "gi");
  37.  
  38.             $(ListBox).pickList('removeAll');
  39.             var items = [];
  40.             $.each(options, function(i) {
  41.                 var option = options[i];
  42.                 if (option.text.match(regex) !== null) {
  43.                     //$(select).append(
  44.                     //  $('<option>').text(option.text).val(option.value).addClass(option.className)
  45.                     //);
  46.                     items.push({
  47.                         value: option.value,
  48.                         label: option.text,
  49.                         selected: false
  50.                     });
  51.                 }
  52.             });
  53.             $(ListBox).pickList('showItems', items);
  54.             if (selectSingleMatch === true && $(select).children().length === 1) {
  55.                 $(select).children().get(0).selected = true;
  56.             }
  57.         });
  58.     });
  59. };
  60.  
  61. $(function() {
  62.  
  63.  
  64.     $('#somethingList').pickList({
  65.         sourceListLabel: '<i class="fa fa-search"></i><input id="listsearch1" class="searchbox" type="text" autocomplete="off" />',
  66.         targetListLabel: 'รายการ',
  67.  
  68.         mainClass: 'pickList col-sm-12',
  69.         listContainerClass: 'panel panel-primary',
  70.         listLabelClass: 'panel-heading',
  71.         listClass: 'pickList_list list-group',
  72.         listItemClass: 'pickList_listItem list-group-item',
  73.         addAllClass: 'btn btn-default center-block',
  74.         addClass: 'btn btn-default center-block',
  75.         removeAllClass: 'btn btn-default center-block',
  76.         removeClass: 'btn btn-default center-block',
  77.         addLabel: '<i class="glyphicon glyphicon-chevron-right"></i>',
  78.         addAllLabel: '<i class="glyphicon glyphicon-chevron-right"></i><i class="glyphicon glyphicon-chevron-right"></i>',
  79.         removeLabel: '<i class="glyphicon glyphicon-chevron-left"></i>',
  80.         removeAllLabel: '<i class="glyphicon glyphicon-chevron-left"></i><i class="glyphicon glyphicon-chevron-left"></i>'
  81.     });
  82.  
  83.     $('#somethingList').filterByText($('#listsearch1'), $('#somethingList') );
  84.  
  85.     $('.searchbox').keydown(function(e) {
  86.         if (e.which == 13)
  87.         {
  88.             e.preventDefault();
  89.         }
  90.     });
  91. });
  92. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement