4

I want to make <select> replacement with custom text formatting using jQuery UI Selectmenu.

Third and fifth selects in this fiddle are good examples of what I am trying to achieve:

http://jsfiddle.net/fnagel/GXtpC/

In the fiddle, there is defined function addressFormatting(), which takes the original option text and returns html output which will be used for rendering the selectmenu. This function is passed as a callback in the selectmenu initialization:

$('select').selectmenu({
    format: addressFormatting
});

I am using jQuery UI Selectmenu 1.11.4. The problem is that the format callback option is not present in this version.

This is a portion of code from jQuery UI Selectmenu version 1.5.0pre, used in the provided fiddle:

$.widget("ui.selectmenu", {
options: {
    appendTo: "body",
    typeAhead: 1000,
    style: 'dropdown',
    positionOptions: null,
    width: null,
    menuWidth: null,
    handleWidth: 26,
    maxHeight: null,
    icons: null,
    format: null, // <<<<<<<<<<<< FORMAT OPTION IS PRESENT <<<<<<<<<<<<
    escapeHtml: false,
    bgImage: function() {}
},

And this is the portion of code from newer version that I am using:

var selectmenu = $.widget( "ui.selectmenu", {
version: "1.11.4",
defaultElement: "<select>",
options: {
    appendTo: null,
    disabled: null,
    icons: {
        button: "ui-icon-triangle-1-s"
    },
    position: {
        my: "left top",
        at: "left bottom",
        collision: "none"
    },
    width: null,

    // callbacks
    change: null,
    close: null,
    focus: null,
    open: null,
    select: null
}, 

format option is not present here, and using it in the initialization has no effect.

In the API documentation, there is _renderItem() method, whose name suggest it could be used to add custom formatting to the select, but it has private scope, so I can't use it from outside of the widget. There is also public create() method, but I am not sure if I can or if I should use it to change the structure of created selectmenu.

Lukas
  • 817
  • 8
  • 13

1 Answers1

0

As mentioned format option has been removed from the selectmenu before it was a part of the jquery-ui library. There is a way to inject custom code into the selectmenu widget and overwrite functions that handle this functionality.

// Create objects that you want inside the menu list
var RenderItem = function(item) {
    return $('<div/>')
        .addClass('ui-menu-item-wrap')
        .append(
            $('<span/>')
                .addClass('ui-menu-item-header')
                .text(item.label + ' (' + item.km + " km)")
        ).append(
            $('<span/>')
                .addClass('ui-menu-item-description')
                .text(item.desc)
        );
};

// Extend functions in the selectmenu plugin
$.widget("ui.selectmenu", $.ui.selectmenu, {

    // Input middleware to the _setText function, that will build
    // jQuery objects to render
    _renderItem: function( ul, item ){
        var li = $( "<li>" ),
            wrapper = $( "<div>", {
                title: item.element.attr( "title" )
            } );

        if ( item.disabled ) {
            this._addClass( li, null, "ui-state-disabled" );
        }
        // Insert middleware
        this._setText( wrapper, RenderItem(item));

        return li.append( wrapper ).appendTo( ul );
    },

    // Overwrite this function to add custom attribute values from the option
    _parseOption: function( option, index ) {
        var optgroup = option.parent( "optgroup" );
        return {
            element: option,
            index: index,
            value: option.val(),
            label: option.text(),
            desc: option.attr('data-desc'), // Custom <option> value saved to item
            km: option.attr('data-km'), // Custom <option> value saved to item
            optgroup: optgroup.attr( "label" ) || "",
            disabled: optgroup.prop( "disabled" ) || option.prop( "disabled" )
        };
    },

    // Overwrite this function to append a value, instead of inserting text
    // So that the jQuery element is handled correctly.
    _setText: function(element, value) {
        if (value) {
            element.append(value);
        } else {
            element.html("&#160;");
        }
    }
});
Kansuler
  • 1,539
  • 2
  • 11
  • 17