Can't find what you're looking for?

Contact us or post in our forums and we'll help you find an answer.

Change Data Source of TreeView in JavaScript

Background:

You may need to allow users to load different sets of data into a TreeView. This sample will cover how to change the data source of a TreeView through using a drop down control.

Steps to Complete:

  1. Create a drop down control containing the list of data sets that users can select to display and tie it to an event

  2. Inside of the event, assign the TreeView’s itemsSource property to the selected data set

Getting Started:

Create a drop down control containing the list of data sets that users can select to display and tie it to an event

First, we’ll need a control that will allow users to select which data set they’ll display. For this, we’ll use Wijmo’s AutoComplete control, supply it with a list of data set names and tie an event to the control.

var combobox = new wijmo.input.ComboBox('#theCombo', {
  textChanged: function(s, e) {},
  itemsSource: getCatagories()
});

The textChanged event will get fired whenever a user changes the selection in the AutoComplete control.

Inside of the event, assign the TreeView’s itemsSource property to the selected data set

Inside of the textChanged event, we’re going to then change the itemsSource of the TreeView based on the selected value of the AutoComplete control. In the sample we have 3 sets of data (cars, countries and electronics). We’ll compare the strings of the selected value to determine which of our 3 data sets we push onto the TreeView.

textChanged: function(s, e) {
  if(s.text == 'Cars') {
    tree.itemsSource = getCars();
  } else if(s.text == 'Electronics') {
    tree.itemsSource = getElectronics();
  } else {
    tree.itemsSource = getCountries();
  }
}

The TreeView control will automatically update when the itemsSource is changed, so there is no need to refresh the control through code.

You can find a live sample of this project at this location: https://jsfiddle.net/JParksGC/c98rewgu/

Get the Latest Version of Wijmo
Download the Free Trial Now