Different display value for selecte text using select2.js

后端 未结 5 2137
闹比i
闹比i 2021-01-18 15:24

Trying to implement a custom select dropdown using select2 plugin Is it possible to have the selected value to display only the actual option \'value\' instead of the text,

5条回答
  •  礼貌的吻别
    2021-01-18 15:54

    The version 4 of Select 2 uses templateSelection instead of formatSelection:
    https://select2.github.io/announcements-4.0.html#changed-templating
    https://select2.github.io/options.html#templateSelection

    $element.select2({
        /**
         * @param {Object} item
         * @param {Boolean} item.disabled
         * @param {HTMLOptionElement} item.element
         * @param {String} item.id
         * @param {Boolean} item.selected
         * @param {String} item.text
         * @returns {String}
         */
        templateSelection: function(item) {
            /** @type {jQuery} HTMLOptionElement */
            var $option = $(item.element);
            var $optGroup = $option.parent();
            return $optGroup.attr('label') + ' (' + item.text + ')';
        }
    });
    

提交回复
热议问题