Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7005301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:20:01+00:00 2026-05-27T21:20:01+00:00

I am using KnockoutJS on my page and trying to populate the DOM for

  • 0

I am using KnockoutJS on my page and trying to populate the DOM for a JsTree, but the JsTree is not identifying the leaves correctly and applying any changes to the code of the leaves(or folders) that KnockoutJs populates. Is there a way to tell JsTree that there is new data and should re-evaluate everything?
Script at beginning of page

$(document).ready(function() {

        function treeNode(data) {
            var self = this;
            self.id = ko.observable(data.ID);
            self.name = ko.observable(data.Name);
        }

        function partsViewModel() {
            var self = this;
            self.partTypes = ko.observableArray([]);
            self.selectedPartType = ko.observable();
            self.selectedPartType = ko.observable();

            $.getJSON("/PartKO/PartTypes", function(allData) {
                var mappedPartTypes = $.map(allData, function(item) { return new treeNode(item); });
                self.partTypes(mappedPartTypes);
            });

            $("#navigation").jstree({
                "themes": { "theme": "classic" },
                "plugins": ["themes", "html_data"]
            });
        }

        ko.applyBindings(new partsViewModel());

    });

HTML

ul id="navigation" class="treeview" data-bind="foreach: partTypes">
        <li>
            <a href="#" data-bind="text:name"></a>
        </li>
    </ul>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:20:02+00:00Added an answer on May 27, 2026 at 9:20 pm

    You need to call .jstree after the list is populated, so move the call to the end of the $.getJSON callback.

    Also the navigation should be a container containing the <ul> element and not the list itself.

    Change partsViewModel function to:

    function partsViewModel() {
        var self = this;
        self.partTypes = ko.observableArray([]);
        self.selectedPartType = ko.observable();
        self.selectedPartType = ko.observable();
    
        $.getJSON("/PartKO/PartTypes", function(allData) {
          var mappedPartTypes = $.map(allData, function(item) { return new treeNode(item); });
          self.partTypes(mappedPartTypes);
    
          $("#navigation").jstree({
            "themes": { "theme": "classic" },
            "plugins": ["themes", "html_data"]
          });
        });
    }
    

    And your HTML to:

    <div id="navigation">
      <ul class="treeview" data-bind="foreach: partTypes">
        <li>
            <a href="#" data-bind="text:name"></a>
        </li>
      </ul>
    </div>
    

    HERE is a simplified example.

    Update:

    If you want to update the tree everytime the <ul> is changed you can use Knockout’s .afterRender callback. This will be called after the DOM is updated. You cannot use .subscribe() because that is called before the DOM changes.

    Insert this into partsViewModel and remove the .jstree part from .getJSON callback:

        self.redrawTree = function() {
            $("#navigation").jstree({
                "themes": {
                    "theme": "classic"
                },
                "plugins": ["themes", "html_data"]
            });
        };
    

    And change HTML to:

    <div id="navigation">
      <ul class="treeview" data-bind="foreach: { data: partTypes, afterRender: redrawTree }">
        <li>
          <a href="#" data-bind="text:name"></a>
        </li>
      </ul>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to update a KnockOutJS page using an Ajax call. The Update
I'm using KnockoutJS and MVVM in a page and most if it works, but
I Am trying to fill jquery mobile listivew using knockoutjs library, but my problem
I am trying to figure out the best ways of using KnockoutJS and I
I am trying to create a web page with a knockoutjs model, that allows
I'm creating an app using the very slick KnockoutJS library, but I've run into
I understand how the tutorial works on the page, but I'm trying to set
I am trying to learn to use Knockoutjs but I am facing a problem
I have a page using KnockoutJS, with a somewhat large count of observables (about
I've been building out a page using ASP.NET MVC 2 using KnockoutJS, KnockoutJS Mapping

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.