SearchAreaControl is a complete jQuery plugin that let's you display, search and select multiple items of a tree data structure.
28 | 29 |In order to user SearchAreaControl plugin you have to include jQuery before the plugin's script. Then, you can initialize the plugin providing the appropriate data and any custom options.
36 |The plugin accepts a specific format of data. It actually accepts an array of nested objects (every object can hold an array of children objects) that produce a group of tree data structures.
37 | 38 |46 |
var myData = [ 47 | { 48 | "code": null, 49 | "group": null, 50 | "name": "Economy car", 51 | "attributes": { 52 | "data-id": "1" 53 | }, 54 | "children": [ 55 | { 56 | "code": null, 57 | "group": null, 58 | "name": "Microcar", 59 | "attributes": { 60 | "data-id": "11" 61 | }, 62 | "children": null 63 | }, 64 | { 65 | "code": null, 66 | "group": null, 67 | "name": "Hatchbacks", 68 | "attributes": { 69 | "data-id": "12" 70 | }, 71 | "children": [ 72 | { 73 | "code": null, 74 | "group": null, 75 | "name": "Ultracompact car", 76 | "attributes": { 77 | "data-id": "121" 78 | }, 79 | "children": null 80 | }, 81 | { 82 | "code": null, 83 | "group": null, 84 | "name": "City car", 85 | "attributes": { 86 | "data-id": "122" 87 | }, 88 | "children": null 89 | }, 90 | { 91 | "code": null, 92 | "group": null, 93 | "name": "Supermini/subcompact car", 94 | "attributes": { 95 | "data-id": "123" 96 | }, 97 | "children": null 98 | } 99 | ] 100 | } 101 | ] 102 | }, 103 | { 104 | "code": null, 105 | "group": null, 106 | "name": "Family car", 107 | "attributes": { 108 | "data-id": "2" 109 | }, 110 | "children": [ 111 | { 112 | "code": null, 113 | "group": null, 114 | "name": "Small family car/compact car", 115 | "attributes": { 116 | "data-id": "21" 117 | }, 118 | "children": null 119 | }, 120 | { 121 | "code": null, 122 | "group": null, 123 | "name": " Large family / mid-size", 124 | "attributes": { 125 | "data-id": "22" 126 | }, 127 | "children": null 128 | } 129 | ] 130 | }, 131 | { 132 | "code": null, 133 | "group": null, 134 | "name": "Saloons / sedans", 135 | "attributes": { 136 | "data-id": "3" 137 | }, 138 | "children": [ 139 | { 140 | "code": null, 141 | "group": null, 142 | "name": "Large family / mid-size", 143 | "attributes": { 144 | "data-id": "31" 145 | }, 146 | "children": null 147 | }, 148 | { 149 | "code": null, 150 | "group": null, 151 | "name": "Full size / large", 152 | "attributes": { 153 | "data-id": "32" 154 | }, 155 | "children": null 156 | }, 157 | { 158 | "code": null, 159 | "group": null, 160 | "name": "Crossover SUV", 161 | "attributes": { 162 | "data-id": "33" 163 | }, 164 | "children": null 165 | }, 166 | { 167 | "code": null, 168 | "group": null, 169 | "name": "Minivans / MPVs", 170 | "attributes": { 171 | "data-id": "34" 172 | }, 173 | "children": null 174 | } 175 | ] 176 | }, 177 | { 178 | "code": null, 179 | "group": null, 180 | "name": "Luxury vehicle", 181 | "attributes": { 182 | "data-id": "4" 183 | }, 184 | "children": [ 185 | { 186 | "code": null, 187 | "group": null, 188 | "name": "Compact executive", 189 | "attributes": { 190 | "data-id": "41" 191 | }, 192 | "children": null 193 | }, 194 | { 195 | "code": null, 196 | "group": null, 197 | "name": "Executive / mid-luxury", 198 | "attributes": { 199 | "data-id": "42" 200 | }, 201 | "children": null 202 | }, 203 | { 204 | "code": null, 205 | "group": null, 206 | "name": "Full-size luxury / Grand saloon", 207 | "attributes": { 208 | "data-id": "43" 209 | }, 210 | "children": null 211 | }, 212 | { 213 | "code": null, 214 | "group": null, 215 | "name": "Estate cars / station wagons", 216 | "attributes": { 217 | "data-id": "44" 218 | }, 219 | "children": null 220 | } 221 | ] 222 | }, 223 | { 224 | "code": null, 225 | "group": null, 226 | "name": "Sports cars", 227 | "attributes": { 228 | "data-id": "5" 229 | }, 230 | "children": [ 231 | { 232 | "code": null, 233 | "group": null, 234 | "name": "Hot hatch", 235 | "attributes": { 236 | "data-id": "51" 237 | }, 238 | "children": null 239 | }, 240 | { 241 | "code": null, 242 | "group": null, 243 | "name": "Sports saloon / sports sedan", 244 | "attributes": { 245 | "data-id": "52" 246 | }, 247 | "children": null 248 | }, 249 | { 250 | "code": null, 251 | "group": null, 252 | "name": "Sports car", 253 | "attributes": { 254 | "data-id": "53" 255 | }, 256 | "children": null 257 | }, 258 | { 259 | "code": null, 260 | "group": null, 261 | "name": "Grand tourer", 262 | "attributes": { 263 | "data-id": "54" 264 | }, 265 | "children": null 266 | }, 267 | { 268 | "code": null, 269 | "group": null, 270 | "name": "Supercar", 271 | "attributes": { 272 | "data-id": "55" 273 | }, 274 | "children": null 275 | }, 276 | { 277 | "code": null, 278 | "group": null, 279 | "name": "Muscle car", 280 | "attributes": { 281 | "data-id": "56" 282 | }, 283 | "children": null 284 | }, 285 | { 286 | "code": null, 287 | "group": null, 288 | "name": "Pony car", 289 | "attributes": { 290 | "data-id": "57" 291 | }, 292 | "children": null 293 | }, 294 | { 295 | "code": null, 296 | "group": null, 297 | "name": "Convertible", 298 | "attributes": { 299 | "data-id": "58" 300 | }, 301 | "children": null 302 | } 303 | ] 304 | }, 305 | { 306 | "code": null, 307 | "group": null, 308 | "name": "Off-roaders", 309 | "attributes": { 310 | "data-id": "6" 311 | }, 312 | "children": [ 313 | { 314 | "code": null, 315 | "group": null, 316 | "name": "Sport utility vehicle", 317 | "attributes": { 318 | "data-id": "61" 319 | }, 320 | "children": null 321 | } 322 | ] 323 | }, 324 | { 325 | "code": null, 326 | "group": null, 327 | "name": "Commercial vehicle", 328 | "attributes": { 329 | "data-id": "7" 330 | }, 331 | "children": [ 332 | { 333 | "code": null, 334 | "group": null, 335 | "name": "Van", 336 | "attributes": { 337 | "data-id": "71" 338 | }, 339 | "children": null 340 | } 341 | ] 342 | } 343 | ];344 | 345 |
Source: https://en.wikipedia.org/wiki/Car_classification#Microcar
346 |355 |
$('#myButton').searchAreaControl({ 356 | data: myData, 357 | mainButton: { 358 | defaultText: 'Cars' 359 | } 360 | });361 | 362 | 363 |
Now, let's use setSelectedNodes
method to initialize the plugin with a selected node. We actually initialize the plugin first, and then call the appropriate method.
372 |
var btn = $('#myButton'); 373 | 374 | // Initialize plugin 375 | btn.searchAreaControl({ 376 | data: myData, 377 | mainButton: { 378 | defaultText: 'Cars' 379 | } 380 | }); 381 | 382 | // Call "setSelectedNodes" method to select one node by "data-id" attribute 383 | btn.searchAreaControl('setSelectedNodes', false, [55]);384 | 385 |
386 | 387 |
388 |There are several methods available to use in order to perform various tasks.
392 | 393 |setSelectedNodes()
Select one or more nodes.
402 | 403 |$(document).on('click', '#setSelectedNodes_button', function() { 404 | $('#myButton').searchAreaControl('setSelectedNodes',false,[57]); 405 | });406 | 407 |
getSelectedByAttribute()
You can use getSelectedByAttribute()
method to get the selected nodes by specific attribute. Here, we can use the data-id
attribute.
$(document).on('click', '#getSelectedByAttribute_button', function() { 417 | var selected = $('#myButton').searchAreaControl('getSelectedByAttribute','data-id'); 418 | });419 | 420 |
421 |
setDisabled()
You can use setDisabled()
to enable or disable the button.
$(document).on('click', '#setDisabled_button', function() { 435 | $('#myButton').searchAreaControl('setDisabled',true); 436 | });437 |
438 | 439 | 440 |
441 |There is a number of options you can provide in order to customize the appearance and behaviour of the plugin.
448 |The following table demonstate some of them. For a complete list of all the available options view the README.
449 | 450 |Name | 456 |Type | 457 |Default value | 458 |Description | 459 |
---|---|---|---|
data | 464 |object |
465 | [] |
466 | Provides the data that are going to be displayed | 467 |
multiSelect | 470 |boolean |
471 | true |
472 | Set to false if you want to be able to select only one item at time | 473 |
columns | 476 |number |
477 | 2 |
478 | Set the number of columns that the data will be rendered | 479 |
selectionByAttribute | 482 |string |
483 | 'data-id' |
484 | Set the attribute that you want to select upon. The plugin will search for this attribute to match with the provided search string to filter all the items of the data array | 485 |
modallHeader.text | 488 |string |
489 | 'Search' |
490 | The text that is going to be displayed in the modal header | 491 |
mainButton.defaultText | 494 |string |
495 | 'Items' |
496 | Set the default button text | 497 |
We have already seen some of the available methods in action.
503 |The following table demonstate some of them. For a complete list of all the available methods view the README.
504 | 505 |Name | 511 |Parameters | 512 |Return | 513 |Description | 514 |
---|---|---|---|
getData() | 519 |no | 520 |array |
521 | Get the control's datasource object (array) | 522 |
clearSelection() | 525 |no | 526 |void |
527 | Clear selected items | 528 |
getSelectedNodes() | 531 |no | 532 |object |
533 | Get an object with two properties, selectedAll (boolean: All items are selected) and selectedNodes (array: Array of selected objects) |
534 |
updateDatasource(data) | 537 |data [object] |
538 | void |
539 | Update the datasource | 540 |
destroy() | 543 |no | 544 |void |
545 | Destroy the plugin | 546 |
Test page
27 | 28 |29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 |
37 | 38 |39 | 40 | 41 | 42 | 43 | 44 |
45 | 46 |47 | 48 |
49 | 50 | 51 |