control.ListBox

Extends ICollection, IControl, ICustomizable.

Class for creating a control in the form of a drop-down list. The standard drop-down list layout changes its appearance depending on the size of the map. If the map is wide enough, text can be displayed in the header of a drop-down list. If the map is small in size, only an icon is displayed. If a button has no icon, then only text will be displayed in all states, and vice versa.

Constructor | Fields | Events | Methods

Constructor

control.ListBox([parameters])

Parameters:

Parameter

Default value

Description

parameters

Type: Object

Drop-down list parameters.

parameters.data

Type: Object

Data.

parameters.data.content

Type: String

List title.

parameters.data.image

Type: String

URL of the button icon. The standard layout of a button is designed for the icon size 16x16 pixels.

parameters.data.title

Type: String

Text of the popup hint that appears when the mouse cursor hovers over the list.

parameters.items

Type: IControl[]

Array of child elements of the list.

parameters.options

Type: Object

Control options.

parameters.options.adjustMapMargin

false

Type: Boolean

Whether the control registers its size in the map margins manager map.margin.Manager.

parameters.options.collapseOnBlur

true

Type: Boolean

This flag enables to collapse the list when the button loses focus, such as when a user clicks on the document.

parameters.options.expandOnClick

true

Type: Boolean

Flag that allows automatically expanding/collapsing the list when clicked.

parameters.options.float

"right"

Type: String

The side to which you want to align the control. Can take three values: "left", "right" or "none". If set to "left" or "right", the controls are arranged one by one, starting from the left or right edge of the map, respectively. If set to "none", the controls are positioned according to the values of the left, right, bottom and top options, relative to the boundaries of the map. See also the description of the position option.

parameters.options.floatIndex

0

Type: Number

The priority of the control positioning. The element with highest priority is positioned closer to the map boundary that is specified in the float property. Does not work with float = "none".

parameters.options.layout

Type: Function|String

Constructor of the control layout which implements the ISelectableControlLayout and IGroupControlLayout interfaces or the layout key in the layout.storage. The layout constructor is passed an object containing the fields:

  • control - Reference to the control.
  • options - Control options manager control.ListBox.options.
  • data - Control data manager control.ListBox.data.
  • state - Control state manager control.ListBox.state.
    The layout's outward appearance changes based on the control's data, state and options. The control, in turn, reacts to layout interface events and changes the values of fields for control.ListBox.state depending on the commands received.

parameters.options.maxWidth

90

Type: Number|Number[]

The maximum width of the listbox in different states. If a number is specified, it is assumed that the button has the same maximum dimensions in all states. If an array is specified, it will be interpreted as the maximum width of the button in different states, from the lesser to the greater. The number of states is set in the instance of the class control.Manager, which is usually a field of Map.controls, via the "states" option. By default, the controls have three states ['small', 'medium', 'large'].

parameters.options.popupFloat

Type: String

The side to align the manager element's popup to. Takes two values: "left" and "right". By default, it is defined by the "float" option.

parameters.options.position

Type: Object

Object describing the position of a control. If the position option is set, the float option value is automatically treated as "none".

parameters.options.position.bottom

'auto'

Type: Number|String

Position relative to the bottom edge of the map.

parameters.options.position.left

'auto'

Type: Number|String

Position relative to the left edge of the map.

parameters.options.position.right

'auto'

Type: Number|String

Position relative to the right edge of the map.

parameters.options.position.top

'auto'

Type: Number|String

Position relative to the top edge of the map.

parameters.options.visible

true

Type: Boolean

Indicates if the control is displayed.

parameters.state

Type: Object

State of the drop-down list.

parameters.state.expanded

false

Type: Boolean

Flags if the list is expanded.

Examples:

1.

// Example 1.
// Handling a click on list items.
var cityList = new ymaps.control.ListBox({
    data: {
        content: 'Select a city'
    },
    items: [
        new ymaps.control.ListBoxItem('Moscow'),
        new ymaps.control.ListBoxItem('Novosibirsk'),
        new ymaps.control.ListBoxItem({options: {type: 'separator'}}),
        new ymaps.control.ListBoxItem('New York'),
    ]
});
cityList.get(0).events.add('click', function () {
    map.setCenter([55.752736, 37.606815]);
});
cityList.get(1).events.add('click', function () {
    map.setCenter([55.026366, 82.907803]);
});
cityList.get(3).events.add('click', function () {
    map.setCenter([40.695537, -73.97552]);
});
map.controls.add(cityList, { floatIndex: 0 });

2.

// Example 2.
// Creating a custom list.
// This example uses jQuery, downloaded from http://yandex.st/jquery/1.6.4/jquery.min.js

// By default, the drop-down list reacts to the "click" event and automatically
// changes its state to expanded or collapsed.
var MyListBoxLayout = ymaps.templateLayoutFactory.createClass(
    '<div id="my-listbox-header" >{{ data.title }}</div >' +
    // This item will serve as a container for the child list items.
    '<div id="my-list-box" style="display: {% if state.expanded %}block{% else %}none{% endif %};" >' +
    '</div >', {

        build: function() {
            MyListBoxLayout.superclass.build.call(this);
            this.childContainerElement = $('#my-list-box').get(0);
            // Each time we rebuild, we will generate an event
            // that signals that the container for child elements has changed.
            // The event format is described in the IGroupControlLayout interface.
            this.events.fire('childcontainerchange', {
                newChildContainerElement: this.childContainerElement,
                oldChildContainerElement: null
            });
        },

        // Redefining the method that requires the IGroupControlLayout interface.
        getChildContainerElement: function () {
            return this.childContainerElement;
        }
    }
);
// Creating a list and displaying the created layout via the options.
var listBox = new ymaps.control.ListBox({options: {layout: MyListBoxLayout}});

3.

// Example 3.
// In this example, the ListBox control filters objects to show
// on the map (multi-select is supported).
// ObjectManager is used here for adding objects to the map.

// Creating a drop-down list with 5 items.
var listBoxItems = ['School', 'Pharmacy', 'Store', 'Hospital', 'Bar']
    .map(function(title) {
        return new ymaps.control.ListBoxItem({
            data: {
                content: title
            },
            state: {
                selected: true
            }
        });
    });

// Now creating the drop-down list with 5 items.
var listBoxControl = new ymaps.control.ListBox({
    data: {
        content: 'Filter',
        title: 'Filter'
    },
    items: listBoxItems,
    state: {
        // Indicates that the list is expanded.
        expanded: true,
        filters: listBoxItems.reduce(function(filters, filter) {
            filters[filter.data.get('content')] = filter.isSelected();
            return filters;
        }, {})
    }
});

map.controls.add(listBoxControl);

// Adding tracking to the indicator to check if a list item is selected.
listBoxControl.events.add(['select', 'deselect'], function(e) {
    var listBoxItem = e.get('target');
    var filters = ymaps.util.extend({}, listBoxControl.state.get('filters'));
    filters[listBoxItem.data.get('content')] = listBoxItem.isSelected();
    listBoxControl.state.set('filters', filters);
});

// Tracking changes to the control.ListBox.state field.
var filterMonitor = new ymaps.Monitor(listBoxControl.state);

filterMonitor.add('filters', function(filters) {
    // Applying the filter to ObjectManager.
    objectManager.setFilter(getFilterFunction(filters));
});

function getFilterFunction(categories){
    return function(obj){
        var content = obj.properties.balloonContent;
        return categories[content]
    }
}

Fields

Name

Type

Description

data

data.Manager

Drop-down list data. Names of fields that are available via the data.Manager.get method:

  • content - Title of the drop-down list.
  • title - Text of the popup hint that appears when the mouse cursor hovers over the list.

events

IEventManager

Event manager.

Inherited from IEventEmitter.

options

IOptionManager

Options manager.

Inherited from IControl.

state

data.Manager

State of the drop-down list. Names of fields that are available via the data.Manager.get method:

  • expanded - Flag for whether the list is expanded.
  • size - The size that is currently set for the list.

Events

Name

Description

add

A child object was added.

Inherited from ICollection.

click

Clicking the list title. Instance of the Event class.

collapse

The list is closed. Instance of the Event class.

expand

The list is open. Instance of the Event class.

optionschange

Change to the object options.

Inherited from ICustomizable.

parentchange

The parent object reference changed.

Data fields:

  • oldParent - Old parent.
  • newParent - New parent.

Inherited from IChild.

press

The event indicating that the button has been pressed. Unlike the click event, it is generated only if the state isEnabled == true. Instance of the Event class.

remove

A child object was deleted.

Inherited from ICollection.

Methods

Name

Returns

Description

add(object)

ICollection

Adds a child object to the collection.

Inherited from ICollection.

collapse()

control.ListBox

Collapses the list.

expand()

control.ListBox

Expands the list.

getIterator()

IIterator

Returns iterator for the collection.

Inherited from ICollection.

getMap()

Map

Returns reference to the map.

getParent()

IControlParent|null

Returns link to the parent object, or null if the parent element was not set.

Inherited from IControl.

isExpanded()

Boolean

Returns a flag for whether the control is in the expanded state.

remove(object)

ICollection

Removes a child object from the collection.

Inherited from ICollection.

setParent(parent)

IChildOnMap

Sets the parent object. If the null value is passed, the manager element will only be deleted from the current parent object.

Inherited from IControl.

Fields details

data

{data.Manager} data

Drop-down list data. Names of fields that are available via the data.Manager.get method:

  • content - Title of the drop-down list.
  • title - Text of the popup hint that appears when the mouse cursor hovers over the list.

Example:

// Adding a drop-down list to the map and changing its hint
// depending on whether the list is collapsed or expanded.

// Creating a group of event listeners.
var listBoxListener = listBox.events.group()
    .add('expand', function () {
        listBox.data.set('title', 'List is expanded');
    })
    .add('collapse', function () {
        listBox.data.set('title', 'List is collapsed');
    });
map.controls.add(listBox, {float: 'none', top: 10, left: 10});
// ...
map.controls.remove(listBox);
// After deleting the item from the map, we delete the listeners.
listBoxListener.removeAll();

state

{data.Manager} state

State of the drop-down list. Names of fields that are available via the data.Manager.get method:

  • expanded - Flag for whether the list is expanded.
  • size - The size that is currently set for the list.

Example:

// Creating and adding a list that is initially open.
var listBox = new ymaps.control.ListBox();
listBox.state.set('expanded', true);
map.controls.add(listBox);

Events details

click

Clicking the list title. Instance of the Event class.

collapse

The list is closed. Instance of the Event class.

expand

The list is open. Instance of the Event class.

press

The event indicating that the button has been pressed. Unlike the click event, it is generated only if the state isEnabled == true. Instance of the Event class.

Methods details

collapse

{control.ListBox} collapse()

Collapses the list.

Returns self-reference.

expand

{control.ListBox} expand()

Expands the list.

Returns self-reference.

getMap

{Map} getMap()

Returns reference to the map.

isExpanded

{Boolean} isExpanded()

Returns a flag for whether the control is in the expanded state.