Using BoxSelect

BoxSelect extends the ComboBox control to provide a more multiselect, friendly ComboBox control. The examples included in this chapter show the differences between the default ComboBox control and this extension, and provide general information about the advanced usage of BoxSelect.

Basic configuration

BoxSelect should support all configuration values as the ComboBox supports. There are some changes for the default values for this extension:

  • The multiSelect option is set to true by default.
  • The forceSelection option is set to true by default.
  • In most cases, the multiple selections are made from a preformed list, but we can also configure the BoxSelect extension to add new records with an autosuggestion list.
  • The ComboBox component doesn't support typeAhead when the multiSelect option is set to true, but even though the value of typeAhead is set to false by default for the BoxSelect extension, support for this feature has been added for multiSelect when set to true.
  • The value option can be used to initialize the multiSelect values. The same format of values is accepted for the setValue method.

Now, let us start using the BoxSelect extension with the following configuration:

{
  "value": [
    "TX",
    "CA"
  ],
  "fieldLabel": "Select multiple states",
  "displayField": "name",
  "valueField": "abbr",
  "width": 500,
  "labelWidth": 130,
  "emptyText": "Pick a state, any state",
  "store": "States",
  "queryMode": "local"
}

And the screenshot should be as follows:

Basic configuration

In the preceding screenshot, we can see how easily we can select multiple values within the BoxSelect combobox extension.

Templates

We can easily configure the display of the selected values and the drop-down list items through templates:

  • labelTpl: It is the template configuration option which controls the display of the selected values within the input field.
  • listConfig: It is the template configuration option which controls the display of the drop-down list items. This option is available within the default ComboBox field and also supported by BoxSelect.

Now, let us see how we can set the configuration for customizing the labelTpl and the listConfig options:

{
  "delimiter": ", ",
  "value": "AZ, CA, NC",
  "labelTpl": "<img src="{flagUrl}"style="height: 25px;vertical-align: middle;margin: 2px;" /> {name} ({abbr})",
  "listConfig": {
    "tpl": [
      "<ul><tpl for=".">",
      "<li role="option"class="x-boundlist-item"style="background-image:url({flagUrl});background-repeat: no-repeat;background-size: 25px;padding-left: 30px;">{name}: {slogan}</li>",
         "</tpl></ul>"
    ]
  },
  "fieldLabel": "Select multiple states",
  "displayField": "name",
  "valueField": "abbr",
  "width": 500,
  "labelWidth": 130,

  "store": "States",
  "queryMode": "local"
}

Following is the screenshot of the BoxSelect extension using the preceding configuration for labelTpl and listConfig:

Templates

In the preceding screenshot, we can see that the BoxSelect extension is working fine and is showing the selected items with the configured labelTpl and listConfig comboboxes.

Single value selection

The BoxSelect extension is targeted for multiple selections, but it also supports single selection by setting the multiSelect option to false. If we need the single selection option by default, we can add the following line of code before the BoxSelect extension is created:

Ext.ux.form.field.BoxSelect.prototype.multiSelect = false;

Now, let us configure the BoxSelect extension for a single selection:

{
  "fieldLabel": "Select a state",
  "multiSelect": false,
  "filterPickList": true,
  "displayField": "name",
  "valueField": "abbr",
  "width": 500,
  "labelWidth": 130,
  "emptyText": "Pick a state, any state",
  "store": "States",
  "queryMode": "local"
}

And the output should be as follows:

Single value selection

In the preceding screenshot, we can now select only a single value within the combobox when the multiSelect option is set to false.

Remote query with unknown values

When we set the queryMode option to remote and the forceSelection option to true, and we pass a value to the BoxSelect extension that is not in the store, a query will be sent to the store's configured proxy "x" with the name of the valueField option and a set of unknown values separated by the configured delimiter as the parameters. For example, if the valueField option is abbr, the delimiter value is |, and unknown values 'NC', 'VA', and, 'ZZ' are set, the following parameters will be passed to the store's configured proxy:

{ abbr: 'NC|VA|ZZ' }

This attempt to load the unknown values will be performed only once per initValue/setValue call. The records which are still unknown after this request will be removed from the field's value, but all known values will be retained. In the preceding example, the 'ZZ' entry was discarded.

Now, let us configure the BoxSelect extension for remote stores:

{
  "fieldLabel": "With Remote Store",
  "store": "RemoteStates",
  "pageSize": 25,
  "queryMode": "remote",
  "delimiter": "|",
  "value": "NC|VA|ZZ",
  "triggerOnClick": false,
  "labelTpl": "{name} ({abbr})",
  "listConfig": {
    "tpl": [
      "<ul><tpl for=".">",
        "<li role="option"class="x-boundlist-item">{name}: {slogan}</li>",
      "</tpl></ul>"
    ]
  },
  "displayField": "name",
  "valueField": "abbr",
  "width": 500,
  "labelWidth": 130
}

Following is the screenshot where we have used this configuration for the BoxSelect extension:

Remote query with unknown values

In the preceding screenshot we can see that our configured BoxSelect is working fine for the remote store, and the value for 'NC' and 'VA' is retrieved where the value for 'ZZ' is discarded.

Adding new records with autosuggestion

In this example we will show the use of forceSelection, when set to false, to enable the entry of new values with autosuggestion provided from the attached store. The new records will be created using the user input for both the configured displayField and valueField. These new records are not added to the ComboBox store automatically.

New entries can be created by any of the following four ways:

  • When we type the configured delimiter which is default to ',', the value that we entered before the delimiter will be used to create a new record.
  • When we paste texts in to the field, the value will be split according to the configured delimiter, which is default to ',' and any values entered will be parsed in to new/existing records.
  • The createNewOnEnter option is set to false by default. If set to true, a new entry will be created when we press the Enter key. This configuration option only applies if the forceSelection option is set to false.
  • The createNewOnBlur option is set to false by default. If set to true, a new entry will be created when the focus leaves the input field. This configuration option only applies if forceSelection is set to false, and is superseded by autoSelect and selectOnTab.

Now, let us configure the BoxSelect extension for autosuggestion:

{
  "fieldLabel": "Enter multiple email addresses",
  "width": 500,
  "growMin": 75,
  "growMax": 120,
  "labelWidth": 130,
  "store": [
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]"
  ],
  "queryMode": "local",
  "forceSelection": false,
  "createNewOnEnter": true,
  "createNewOnBlur": true,
  "filterPickList": true,
  "displayField": "name",
  "valueField": "abbr"
}

Using this configuration we will get the BoxSelect component as follows:

Adding new records with autosuggestion

In the preceding screenshot, we can see how the BoxSelect component is offering an autosuggestion list where we can select those list items or create new records.

BoxSelect specific configurations

The following configuration options are specific to the BoxSelect extension:

  • The createNewOnEnter option is set to false by default. If this option is set to true and the forceSelection option is set to false, a new entry will be created as soon as the user presses the Enter key.
  • The createNewOnBlur option is set to false by default. If this option is set to true and the forceSelection option is set to false, a new entry will be created when the focus leaves the input field. This configuration option is superseded by autoSelect and selectOnTab.
  • The stacked option is set to false by default. If this option is set to true, the labeled items will fill the available width of the list instead of being only as wide as the displayed value.
  • The pinList option is set to true by default. If this option is set to false, the pick list will automatically collapse after a selection is made, when multiSelect is true. This mimics the default behavior when multiSelect is false.
  • The triggerOnClick option is set to true by default. When the option is set to true, the pick list will emulate a trigger when clicking in the field just like when a ComboBox component is set with the editable option to false.
  • The grow option is set to true by default. If this option is set to false, the list of selections will scroll when necessary, and the height of the field will not change. This setting has no effect if a fixed height is set for the field, either directly (for example, through a height configuration), or by the containing layout.
  • The growMin option is set to false by default. If this option is set to true, any numeric value will be used for the field's minimum height.
  • The growMax option is set to false by default. If this option is set to true, any numeric value will be used for the field's maximum height and the list of selections will scroll when necessary.
  • The filterPickList option is set to false by default. If this option is set to true, the currently selected values will be hidden from the expanded pick list.

Now, let us configure the BoxSelect component by changing some of the default values to see the effect:

{
  "fieldLabel": "Select multiple states",
  "displayField": "name",
  "width": 500,
  "labelWidth": 130,
  "store": "States",
  "queryMode": "local",
  "valueField": "abbr",
  "value": "WA, TX",
  "stacked": true,
  "pinList": false,
  "filterPickList": true
}

Following is the screenshot of the BoxSelect component where we've used this configuration:

BoxSelect specific configurations

In the preceding screenshot, we can see that the labeled items are filling the full width available as we set the stacked option to true. The pick list is automatically collapsing as soon as a selection is made as we set the pinList option to false, and the current selected values are hidden from the expanded pick list as we set the filterPickList option to true.

Value handling and events

The following methods are available within BoxSelect, which helps to work with the value of the combobox:

  • addValue(mixedValue): Adds a value or values to the current value of the field.
  • removeValue(mixedValue): Removes a value or values from the current value of the field.
  • getValueRecords(): Returns the records for the field's current value.
  • getSubmitData(): Allows submitting the field as a JSON encoded array.

Also the BoxSelect component provides the following two events for managing the selected items:

  • valueSelectionChange
  • valueFocusChange
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset