2015-04-17 81 views
3

我正在使用David Stutz的bootstrap多选插件,我无法显示'all selected'。使用allSelectedText方法可以工作,但在使用'allSelectedText'回调时它会被覆盖。Bootstrap Multiselect Plugin显示'all selected'

使用前一个问题的答案,我试图抓住孩子的数量和检查,如果他们都选择(见注释)

注意 - 这是工作,如果我删除了“numberofOptions”

使用buttonText回调/ allSelectedText方法,这里有一个链接到文档 - http://davidstutz.github.io/bootstrap-multiselect/

您的输入表示赞赏。

JS

$(document).ready(function() { 
     $('#multiselect').multiselect({ 
      //Following line is being overridden by the numberOfOptions 
      allSelectedText: 'All Selected', 
      buttonText: function(options, select) { 
      //grab the number of childeren to later return 'all selected' 
      var numberOfOptions = $(this).children('option').length; 
       if (options.length === 0) { 
        return 'Select'; 
       } 
       else if (options.length > 1) { 
        return '1+ Selected'; 
       } 
       //This line is calculating number of options, 
       //displaying 'AllSelected in the label' 
       else if (options.length === numberOfOptions) { 
        return 'AllSelected'; 
       } 
       else { 
        var labels = []; 
        options.each(function() { 
         if ($(this).attr('label') !== undefined) { 
          labels.push($(this).attr('label')); 
         } 
         else { 
          labels.push($(this).html()); 
         } 
        }); 
        return labels.join(', ') + ''; 
       } 
      }, 

      buttonWidth: '100%', 
      includeSelectAllOption: true, 
     }); 
    }); 

HTML

<select id="multiselect" multiple="multiple"> 
     <option value="x">XYZ</option> 
     <option value="x">XYZ</option> 
     <option value="x">XYZ</option> 
     <option value="x">XYZ</option> 
     <option value="x">XYZ</option>    
</select> 
+1

你能提供你的HTML代码SELECT标签的摘录? –

+0

对不起,编辑。查看帖子。 – Mintberry

回答

2

我通过使用以下文件解决了这个问题。

numberDisplayed:1

$(document).ready(function() { 
     $('#multiselect').multiselect({ 
      allSelectedText: 'All', 
      numberDisplayed: 1, 
      buttonWidth: '100%', 
      includeSelectAllOption: true, 
     }); 
    }); 
0

你的代码永远不会达到

else if (options.length === numberOfOptions) 

因为

else if (options.length > 1) 

已经涵盖了几乎所有可能的剩余案例(options.length === 1除外)。

另外,allSelectedText被默认使用buttonText回调。如果您覆盖它并且不在代码中使用allSelectedText文本,则该文本不会显示。

+0

谢谢你的输入,我决定用内置的'numberDisplayed' – Mintberry

相关问题