Select2: Hide certain options dynamically

前端 未结 10 776
情深已故
情深已故 2020-12-03 06:56

Basically what I\'m looking for is the ability to hide options from the dropdown of select items. So, technically they would still be options, but you just wouldn\'t be able

10条回答
  •  Happy的楠姐
    2020-12-03 07:17

    If you want to achieve it , maybe you can modify the select2.js code,

    First i hidden the second option , originally it will not work when you use

    select2 plugin ,

    
    

    Second i will modify the select2.js code: line 926

    i add extra condition statement && element.css('display') != 'none' here

     process = function (element, collection) {
         var group;
         if (element.is("option") && element.css('display') != 'none') {
             if (query.matcher(term, element.text(), element)) {
                  collection.push(self.optionToData(element));
                  }
         } else if (element.is("optgroup")) {
                  group = self.optionToData(element);
                  element.children().each(function (i, elm) { 
                           process(elm, group.children); 
                       });
                  if (group.children.length > 0) {
                           collection.push(group);
                  }
          }
         };
    

    JSBIN http://jsbin.com/qusimi/1/edit

提交回复
热议问题