Skip to content

Determining the best width #727

@SteveTheTechie

Description

@SteveTheTechie

Reference issue #717, comments 2 and 8.

Here is the essence of how I automatically determine a "best width" that will accommodate all the select options... Essentially a hidden clone of the menu is produced that is used for measuring. The clone is set up specifically to allow the container div to assume the smallest width needed to accommodate the widest option.

Determining a single option height allows us to just multiply the number of options by the native select's size attribute to determine a menu height.

This is not perfect-there is room for improvement. In particular, whether or not options can be wrapped should be set via a new option. Wrapping affects the width and the appearance.

Also, the measuring can be a bit slow. If we had a reliable way to quickly determine the widest option then we could just make a dummy menu clone with that single option for measuring which would be faster. This is an approach that I not yet explored.

	// Added to allow automatic menu sizing,  8/17/2014, Steve James
	// Reference: http://stackoverflow.com/questions/1472303/jquery-get-width-of-element-when-not-visible-display-none
	// 		http://stackoverflow.com/questions/5903112/is-it-really-impossible-to-make-a-div-fit-its-size-to-its-content
	
	_measureMenu: function() {			
		// width: auto needed to reset width when dialogs 
                //    containing multi-select are re-opened.  
		// Display: inline-block needed to force width to best fit.
		
		// Make the hidden clone for measuring...
		var $clone = this.$menu.clone()
                                 .appendTo('body')
                                 .css({visibility:'hidden',width:'auto',display:'inline-block'})
                                 .find('ul')
                                 .css({display:'inline-block'})
                                 .end()
                                 .show();
				
		// Find outer height of a hidden menu item before we display menu.																											
		this.optionHeight = $clone.find('label').outerHeight();																																	
		
		// Find best width of hidden menu before we display it; 20 is for scroll bar		
		this.menuWidth = Math.max($clone.find('.ui-multiselect-header').width(),
                                         $clone.find('.ui-multiselect-checkboxes').width()) - 20;
		$clone.remove(); 
	},

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions