jsviewsjquery-ui-selectmenu

How to specify custom rendering template for options in {{selectmenu}} tag


I am looking for an example where I can specify template for rendering options, so that I can render an option as a two part: span with a background color and a value.

Simillar to this https://jqueryui.com/selectmenu/#custom_render

is this supported in the jsviews tag implementation?

Many thanks.


Solution

  • There are probably other approaches, including making your own custom tag - not using jQuery UI, or creating a derived version of the {{selectmenu}} tag control here, {{myselectmenu}} (using baseTag).

    But here is a quick suggestion of one way you can do it:

    {^{selectmenu person name="person" onBind=~onbind}}
      {^{for people}}
        <option data-style="{{:style}}" value="{{:id}}">{{:name}}</option>
      {{/for}}
    {{/selectmenu}}
    

    Data:

    people: [
      {name: "John Resig", id: "1", style: "background-image: url(...);"},
      ...
    

    Code:

    pageTmpl.link("#page", model, {
      onbind: function(val) {
        // override onBind for this tag control instance
        this.baseApply(arguments);
    
        // override _renderItem for this widget instance:
        this.widget._renderItem = function( ul, item ) {
            var li = $( "<li>" ),
              wrapper = $( "<div>", { text: item.label } );
    
            if ( item.disabled ) {
              li.addClass( "ui-state-disabled" );
            }
    
            $( "<span>", {
              style: item.element.attr( "data-style" ),
              "class": "ui-icon " + item.element.attr( "data-class" )
            })
              .appendTo( wrapper );
    
            return li.append( wrapper ).appendTo( ul );
          };
        }
    });