All Projects → clivezhg → select2-to-tree

clivezhg / select2-to-tree

Licence: MIT License
Select2-to-Tree extends Select2 to support arbitrary level of nesting...

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects
HTML
75241 projects

Projects that are alternatives of or similar to select2-to-tree

Vuejs Tree
A highly customizable and blazing fast Vue tree component ⚡🌲
Stars: ✭ 211 (+197.18%)
Mutual labels:  tree, treeview
laravel-simple-select
Laravel Simple Select inputs component for Blade and Livewire.
Stars: ✭ 59 (-16.9%)
Mutual labels:  select, select2
vuejs-tree
A highly customizable and blazing fast Vue tree component ⚡🌲
Stars: ✭ 310 (+336.62%)
Mutual labels:  tree, treeview
El Tree Select
基于element-ui2.x扩展下拉树
Stars: ✭ 159 (+123.94%)
Mutual labels:  tree, select
tree-tree
No description or website provided.
Stars: ✭ 15 (-78.87%)
Mutual labels:  tree, treeview
Vue Treeselect
A multi-select component with nested options support for Vue.js
Stars: ✭ 2,347 (+3205.63%)
Mutual labels:  tree, select
android-thinkmap-treeview
Tree View; Mind map; Think map; tree map; custom view; 自定义;关系图;树状图;思维导图;组织机构图;层次图
Stars: ✭ 314 (+342.25%)
Mutual labels:  tree, treeview
Vue Finder
📁 A Vue.js component to display hierarchical data (like the MacOS X finder)
Stars: ✭ 87 (+22.54%)
Mutual labels:  tree, treeview
react-tree
Hierarchical tree component for React in Typescript
Stars: ✭ 174 (+145.07%)
Mutual labels:  tree, treeview
comment tree
Render comment tree like facebook comment - reply
Stars: ✭ 37 (-47.89%)
Mutual labels:  tree, treeview
Vue Ui For Pc
基于Vue2.x的一套PC端UI组件,包括了Carousel 跑马灯、Cascader 级联、Checkbox 多选框、Collapse 折叠面板、DatePicker 日期选择、Dialog 对话框、Form 表单、Input 输入框、InputNumber 数字输入框、Layer 弹窗层、Loading 加载、Menu 菜单、Page 分页、Progress 进度条、Radio 单选框、SelectDropDown 仿select、Switch 开关、Table 表格、Tabs 标签页、Textarea 文本框、Tooltip 文字提示、BackTop 返回顶部、steps 步骤条、Transfer 穿梭框、Tree 树形、Upload 文件上传、Lazy 图片懒加载、Loading 加载、Pagination 分页等等
Stars: ✭ 156 (+119.72%)
Mutual labels:  tree, select
react-treefold
A renderless tree component for your hierarchical React views
Stars: ✭ 37 (-47.89%)
Mutual labels:  tree, treeview
Graphview
Flutter GraphView is used to display data in graph structures. It can display Tree layout, Directed and Layered graph. Useful for Family Tree, Hierarchy View.
Stars: ✭ 152 (+114.08%)
Mutual labels:  tree, treeview
Heyui
🎉UI Toolkit for Web, Vue2.0 http://www.heyui.top
Stars: ✭ 2,373 (+3242.25%)
Mutual labels:  tree, select
Wmztreeview
类似前端elementUI的树形控件,可自定义节点内容,支持无限极节点,可拖拽增删节点等等,非递归实现
Stars: ✭ 118 (+66.2%)
Mutual labels:  tree, treeview
vue-virtualised
Blazing fast scrolling and updating for any amount of list and hierarchical data.
Stars: ✭ 18 (-74.65%)
Mutual labels:  tree, treeview
Angular Tree Component
A simple yet powerful tree component for Angular (>=2)
Stars: ✭ 1,031 (+1352.11%)
Mutual labels:  tree, treeview
Bootstraptable Treeview
bootstrapTable extension of treeView
Stars: ✭ 57 (-19.72%)
Mutual labels:  tree, treeview
craft-select2
Filter / search a <select> using the popular Select2 fieldtype for Craft CMS
Stars: ✭ 18 (-74.65%)
Mutual labels:  select, select2
Algorithms
Java implementation for Introduction to Algorithms book.
Stars: ✭ 58 (-18.31%)
Mutual labels:  tree, select

Select2-to-Tree

Select2-to-Tree is an extension to Select2, a popular select boxes library: https://github.com/select2/select2.

Though Select2 is very versatile, it only supports a single level of nesting. See https://select2.github.io/options.html#how-many-levels-of-nesting-are-allowed:

Because Select2 falls back to an <optgroup> when creating nested options, only a single level of nesting is supported. Any additional levels of nesting is not guarenteed to be displayed properly across all browsers and devices.

Select2-to-Tree extends Select2 to support arbitrary level of nesting.

Select2 compatibility

  • Select2 4+

Browser compatibility

  • IE 8+
  • Chrome 8+
  • Firefox 10+
  • Safari 3+
  • Opera 10.6+

Usage

Firstly, you need to know the usage of Select2: https://github.com/select2/select2

Then, in your HTML document, you add the Select2 library (the *.js file & *.css file, currently the version should be 4.0+), and the Select2-to-Tree library (the *.js file & *.css file in the "src" folder). jQuery is also needed.

There are 2 ways to use Select2-to-Tree:

1. Use data, and empty <select> element(see "Example 1" in "example/example.html"):

Suppose your HTML is like this:

<select id="sel_1" style="width:16em" multiple>
</select>

And your data:

var mydata = [
   {id:1, text:"USA", inc:[
      {text:"west", inc:[
         {id:111, text:"California", inc:[
            {id:1111, text:"Los Angeles", inc:[
               {id:11111, text:"Hollywood"}
            ]},
            {id:1112, text:"San Diego", selected:"true"}
         ]},
         {id:112, text:"Oregon"}
      ]}
   ]},
   {id:2, text:"India"},
   {id:3, text:"中国"}
];

And you call Select2-to-Tree like the following:

$("#sel_1").select2ToTree({treeData: {dataArr:mydata}, maximumSelectionLength: 3});

"{treeData: {dataArr:mydata}" is for Select2-to-Tree, "maximumSelectionLength: 3" is for Select2 (and you can set the other Select2 arguments if needed)

About the data structure: "id" will be used as option value, "text" will be used as option label, and "inc" will be used to specify sub-level options. If your data structure is not like this, you can set arguments in "treeData" to change the default behavior, e.g., treeData: {dataArr: mydata, valFld: "value", labelFld: "name", incFld: "sub"}:

  • dataArr, an array containing the data.
  • valFld, the option value field, it's "id" by default. (if the value is empty, the corresponding option will be unselectable, see the "west" option in the example)
  • selFld, the selected value field, it's "selected" by default.
  • labelFld, the option label field, it's "text" by default.
  • incFld, the sub options field, it's "inc" by default.
  • dftVal, the default value.

For valFld and labelFld, you can give a object path (eg: item.label. see "Example 4" in "example/example.html").

The above are all the parameters supported by Select2-to-Tree.

2. directly create the <select> element(see "Example 2" in "example/example.html"):

If it's hard to create the required data structure, you can directly create the <select> element. It's like the following:

<select id="sel_2" style="width:8em">
   <option value="1" class="l1 non-leaf">opt_1</option>
   <option value="11" data-pup="1" class="l2 non-leaf">opt_11</option>
   <option value="111" data-pup="11" class="l3">opt_111</option>
   <option value="12" data-pup="2" class="l2">opt_12</option>
   <option value="2" class="l1">opt_2</option>
   <option value="3" class="l1">opt_3</option>
</select>
  • the classes l1,l2,l3,l4,l5..., setting the nesting level.
  • the attribute data-pup, setting the value of the parent level option.
  • the class non-leaf, setting whether the option has children or not.

Then, you call Select2-to-Tree (the "treeData" argument of Select-to-Tree is not needed here, but you can set arguments for Select2):

$("#sel_2").select2ToTree();

Styling

Select-to-Tree uses CSS rules(in the select2totree.css file) to control the indent & size of each level, e.g.:

.s2-to-tree .select2-results__option.l8 {
	margin-left: 6.0em;
	font-size: 0.75em;
}

By default, Select-to-Tree defines 8 levels, if you need more than 8 levels, you can add your own CSS rules. You can also change or override the pre-defined CSS rules to match your requirements.

Constraints

  • AJAX data source is not supported.
  • It is a little slower than plain Select2, because there are extra operations to do. Anyway, according to my test (you can check "Example 3" in "example/example.html", click the "India -> north"), 1500 options is basically acceptable, which is enough in most of the real world cases.

Illustration

See "Example 3" in "example/example.html":

Copyright and license

The license is available within the repository in the [LICENSE][license] file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].