fnagel/jquery-ui — Issue #199: enhancement (with source): for 'format', make option node available.
Author: matthucke
Created: 2011-12-07 21:57:26+00:00
State: open

Selectmenu presently provides a 'format' callback in the options object, allowing the user to transform the text from the initial HTML option into something different for the selectmenu's 'li' element.

In my uses of selectmenu, I have often found the need for more information in the transformed 'li' element than was in the original html option. For example, consider a colour picker for an online shopping cart. The original option should just display the colour name, but the selectmenu equivalent would also show an icon, whether that colour is in stock, pricing data, etc., none of which are easily obtainable from the colour name.

SUGGESTED ENHANCEMENT:

Pass a second parameter to the 'format' callback - this would be the jquery object representing the original option node whose text is being rewritten. Because the first parameter is still simply the text, this is backwards compatible.

DIFF:

281c281
< text: self._formatText(opt.text()),
---
> text: self._formatText(opt),
628,629c628,629
< _formatText: function(text) {
< return (this.options.format ? this.options.format(text) : text);
---
> _formatText: function(opt) {
> return (this.options.format ? this.options.format(opt.text(), opt) : opt.text());

EXAMPLE USAGE:

$('.foo').selectmenu({
format: function (text, opt) {
// various functions involving opt.attr('id'), opt.attr('class'), etc.
}
});