'select all' and 'remove all' with chosen.js

前端 未结 7 1686
深忆病人
深忆病人 2020-12-24 12:29

For the select menu plugin chosen.js, is there an established way to add \'select all items in list\' or \'remove all items in list\' feature to a multiple select input? In

相关标签:
7条回答
  • 2020-12-24 13:16

    I missed the similar feature. This adds the two links All and None (texts customizable through options uncheck_all_text and select_all_text) on the top of the popup list. You may need to edit this if you use grouping.

    $("select").on("chosen:showing_dropdown", function(evnt, params) {
        var chosen = params.chosen,
            $dropdown = $(chosen.dropdown),
            $field = $(chosen.form_field);
        if( !chosen.__customButtonsInitilized ) {
            chosen.__customButtonsInitilized = true;
            var contained = function( el ) {
                var container = document.createElement("div");
                container.appendChild(el);
                return container;
            }
            var width = $dropdown.width();
            var opts = chosen.options || {},
                showBtnsTresshold = opts.disable_select_all_none_buttons_tresshold || 0;
                optionsCount = $field.children().length,
                selectAllText = opts.select_all_text || 'All',
                selectNoneText = opts.uncheck_all_text || 'None';
            if( chosen.is_multiple && optionsCount >= showBtnsTresshold ) {
                var selectAllEl = document.createElement("a"),
                    selectAllElContainer = contained(selectAllEl),
                    selectNoneEl = document.createElement("a"),
                    selectNoneElContainer = contained(selectNoneEl);
                selectAllEl.appendChild( document.createTextNode( selectAllText ) );
                selectNoneEl.appendChild( document.createTextNode( selectNoneText ) );
                $dropdown.prepend("<div class='ui-chosen-spcialbuttons-foot' style='clear:both;border-bottom: 1px solid black;'></div>");
                $dropdown.prepend(selectNoneElContainer);
                $dropdown.prepend(selectAllElContainer);
                var $selectAllEl = $(selectAllEl),
                    $selectAllElContainer = $(selectAllElContainer),
                    $selectNoneEl = $(selectNoneEl),
                    $selectNoneElContainer = $(selectNoneElContainer);
                var reservedSpacePerComp = (width - 25) / 2;
                $selectNoneElContainer.addClass("ui-chosen-selectNoneBtnContainer")
                    .css("float", "right").css("padding", "5px 8px 5px 0px")
                    .css("max-width", reservedSpacePerComp+"px")
                    .css("max-height", "15px").css("overflow", "hidden");
                $selectAllElContainer.addClass("ui-chosen-selectAllBtnContainer")
                    .css("float", "left").css("padding", "5px 5px 5px 7px")
                    .css("max-width", reservedSpacePerComp+"px")
                    .css("max-height", "15px").css("overflow", "hidden");
                $selectAllEl.on("click", function(e) {
                    e.preventDefault();
                    $field.children().prop('selected', true);
                    $field.trigger('chosen:updated');
                    return false;
                });
                $selectNoneEl.on("click", function(e) {
                    e.preventDefault();
                    $field.children().prop('selected', false);
                    $field.trigger('chosen:updated');
                    return false;
                });
            }
        }
    });
    

    I am also using a CSS to limit the height of the selected choises in case there are say tens of them:

        .chosen-choices {
            max-height: 150px;
        }
    
    0 讨论(0)
提交回复
热议问题