jQuery multiple select with nested options
npm install tree-multiselect
This plugin allows you to add a sweet treeview frontend to a node.
The underlying node can be used as it was before. This means you can still use $("select").val() or selectElement.value to get the value, as if there was no plugin. If you want to add options dynamically, please continue reading, there are some more steps you need to take.
* Make sure you've got in your or some of the symbols may look strange.
* Requires jQuery v1.8+
multiple="multiple" attribute on your nodes$.treeMultiselect(params) with whatever params you want has the multiple attribute set.The children can have the following attributes.
#### Option Attributes
Option Attribute name | Description
----------------------------- | ---------------------------------selected | Have the option pre-selected. This is actually part of the HTML spec. For specified ordering of these, use data-indexreadonly | User cannot modify the value of the option. Option can be selected (ex.
All of the above are optional.
Your data-section can have multiple section names, separated by the sectionDelimiter option. If you don't have a data-section on an option, the option will be on the top level (no section).
Ex. data-section="top/middle/inner" will show up as
- top
- middle
- inner
- your option
$.treeMultiselect(params) nodes. params is optional.``javascript`
$("select").treeMultiselect();`javascript`
let params = {searchable: true};
$("select").treeMultiselect(params);`javascript
function treeOnChange(allSelectedItems, addedItems, removedItems) {
console.log("something changed!");
}
$("select").treeMultiselect({
allowBatchSelection: false,
onChange: treeOnChange,
startCollapsed: true
});
`
##### Params
Name | Default | Description
----------------------- | -------------- | ---------------
allowBatchSelection | true | Sections have checkboxes which when checked, check everything within themcollapsible | true | Adds collapsibility to sectionsenableSelectAll | false | Enables selection of all or no optionsselectAllText | Select All | Only used if enableSelectAll is activeunselectAllText | Unselect All | Only used if enableSelectAll is activefreeze | false | Disables selection/deselection of options; aka display-onlyhideSidePanel | false | Hide the right panel showing all the selected itemsmaxSelections | 0 | A number that sets the maximum number of options that can be selected. Any positive integer is valid; anything else (such as 0 or -1) means no limitonChange | null | Callback for when select is changed. Called with (allSelectedItems, addedItems, removedItems), each of which is an array of objects with the properties text, value, initialIndex, and sectiononlyBatchSelection | false | Only sections can be checked, not individual itemssortable | false | Selected options can be sorted by dragging (requires jQuery UI)searchable | false | Allows searching of optionssearchParams | ['value', 'text', 'description', 'section'] | Set items to be searched. Array must contain 'value', 'text', or 'description', and/or 'section'sectionDelimiter | / | Separator between sections in the select option data-section attributeshowSectionOnSelected | true | Show section name on the selected itemsstartCollapsed | false | Activated only if collapsible is true; sections are collapsed initially
#### Examples
#### .remove()
Removes the tree from the DOM. Leaves the original
#### .reload()
Reinitializes the tree. You can add
`javascript
let trees = $("select").treeMultiselect();
let firstTree = trees[0];
// add an option
$("select#id").append("");
firstTree.reload();
`
on to your web page. The css file is optional (but recommended).You can also use bower -
bower install tree-multiselect$3
You need to have grunt-cli installed so you can run the grunt command.
- Run tests: grunt or grunt test
- Build dist JavaScript file: grunt build
- Build Sass: grunt sass
- Build everything: grunt release$3
Help! The first element is selected when I create the tree. How do I make the first element not selected?
You didn't set the multiple attribute on your