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 8245455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:10:57+00:00 2026-06-07T22:10:57+00:00

Trying to build a tree structure with knockout and jquery mobile. using a listview

  • 0

Trying to build a tree structure with knockout and jquery mobile. using a listview and insert items, my object have 2 arrays where 1 is n deep, and the otherone holding n objects that should be listet after the first array on each level.

Have made 2 fiddles to easier explain what I’m trying to do.

What it trying to achieve: http://jsfiddle.net/Qwark/gDTtd/

What I have: http://jsfiddle.net/Qwark/NJZrN/13/

EDIT: adding source here. Just in case something happens to the fiddles.

What it trying to achieve:

<div id="myPage" data-role="page" data-theme="b">
  <div data-role="header">The header</div>
  <div data-role="content">        
    <div id="orgjson">
        <ul data-role="listview">
            <li>
                <span>CompanyRoot</span>
                <ul data-role="listview">
                    <li><span>Department1</span>
                        <ul data-role="listview">
                            <li>
                                <span>Department1.1</span>
                                <ul data-role="listview">
                                    <li><span>Dep1.1 Employee1</span></li>
                                    <li><span>Dep1.1 Employee2</span></li>
                                </ul>
                            </li>
                            <li>
                                <span>Department1.2</span>
                                <ul data-role="listview">
                                    <li><span>Dep1.2 Employee1</span></li>
                                    <li><span>Dep1.2 Employee2</span></li>
                                    <li><span>Dep1.3 Employee1</span></li>
                                </ul>
                            </li>                                
                            <li><span>Dep1 Employee1</span></li>
                            <li><span>Dep1 Employee2</span></li>
                            <li><span>Dep1 Employee3</span></li>
                        </ul>  
                    </li>
                    <li><span>Department2</span>
                        <ul data-role="listview">
                            <li>
                                <span>Department2.1</span>
                                <ul data-role="listview">
                                    <li><span>Dep2.1 Employee1</span></li>
                                    <li><span>Dep2.1 Employee2</span></li>
                                </ul>
                            </li>
                            <li>
                                <span>Department2.2</span>
                                <ul data-role="listview">
                                    <li><span>Dep2.2 Employee1</span></li>
                                    <li><span>Dep2.2 Employee2</span></li>
                                </ul>
                            </li>                                
                            <li><span>Dep2 Employee1</span></li>
                            <li><span>Dep2 Employee2</span></li>
                        </ul>  
                    </li>

                    <li><span>Root Employee1</span></li>
                    <li><span>Root Employee2</span></li>
                </ul>                                        
            </li>                                
        </ul>          

    </div>
</div>    

What I have html:

<div id="myPage" data-role="page" data-theme="b">
   <div data-role="header">The header</div>
   <div data-role="content">        
      <ul data-bind="listview: true, template: {name: 'depTmpl', foreach: DepTree         }"></ul>   
    </div>    
</div>

<script id="depTmpl" type="text/html">
  <li>
    <span data-bind="text: depname"></span>
    <ul data-bind="listview: true, template: {name: 'depTmpl', foreach: departments}">
      </ul>
   </li>

    <!-- I do not know what should go here or if this is the right place at all -->
    <!-- I want to list employes after all Departments -->    
</script>

What I have js:

    function Department(depid, depname) {
    this.depid = depid;
    this.depname = depname;
    this.departments = new Array();
    this.employees = new Array();

    this.addDepartment = function (newdep) {
        this.departments.push(newdep);
    };
    this.addEmployee = function (newEmp) {
        this.employees.push(newEmp);
    };
};

function Employee(empid, empname) {
    this.empid = empid;
    this.empname = empname;
};

ko.bindingHandlers.listview = {
    update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        setTimeout(function () {
            $(element).attr('data-role', 'listview');
            $(element).listview();
        }, 0);
    }
};   

DepViewModel = function () {
    this.DepTree = SetupDepTree();
};

SetupDepTree = function () {//testdata, indented for easy reading
    var CompanyRoot= new Department("D0", "Root");
    var dep1 = new Department("D1", "Department1");        
        var dep11 = new Department("D1.1", "Department1.1");    
            var REmp111 = new Employee("E1.1.1", "Employee111");
            var REmp112 = new Employee("E1.1.2", "Employee112");                
            dep11.addEmployee(REmp111);
            dep11.addEmployee(REmp112);
        var dep12 = new Department("D1.2", "Department1.2");
            var REmp121 = new Employee("E1.2.1", "Employee121");            
            var REmp122 = new Employee("E1.2.2", "Employee122");
            var REmp123 = new Employee("E1.2.3", "Employee123");
            dep12.addEmployee(REmp121);
            dep12.addEmployee(REmp122);
            dep12.addEmployee(REmp123);            
        var REmp11 = new Employee("E1.1", "Employee11");
        var REmp12 = new Employee("E1.2", "Employee12");        
        dep1.addDepartment(dep11);
        dep1.addDepartment(dep12);
        dep1.addEmployee(REmp11);
        dep1.addEmployee(REmp12);        
    var dep2 = new Department("D2", "Department2");
        var dep21 = new Department("D2.1", "Department2.1");    
            var REmp211 = new Employee("E2.1.1", "Employee211");
            var REmp212 = new Employee("E2.1.2", "Employee212");                
            dep21.addEmployee(REmp211);
            dep21.addEmployee(REmp212);
        var dep22 = new Department("D2.2", "Department2.2");
            var REmp221 = new Employee("E2.2.1", "Employee221");            
            var REmp222 = new Employee("E2.2.2", "Employee222");
            var REmp223 = new Employee("E2.2.3", "Employee223");
            dep22.addEmployee(REmp221);
            dep22.addEmployee(REmp222);
            dep22.addEmployee(REmp223);            
        var REmp21 = new Employee("E2.1", "Employee21");
        var REmp22 = new Employee("E2.2", "Employee22");        
        dep2.addDepartment(dep21);
        dep2.addDepartment(dep22);
        dep2.addEmployee(REmp21);
        dep2.addEmployee(REmp22);
    var REmp1 = new Employee("E0.1", "Employee01");
    var REmp2 = new Employee("E0.2", "Employee02");
    CompanyRoot.addDepartment(dep1);
    CompanyRoot.addDepartment(dep2);
    CompanyRoot.addEmployee(REmp1);
    CompanyRoot.addEmployee(REmp2);    
    return CompanyRoot;
};

$(document).ready(function () {
        var vm = new DepViewModel ();
        ko.applyBindings(vm);
});
  • 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-06-07T22:10:59+00:00Added an answer on June 7, 2026 at 10:10 pm

    Try this example: http://jsfiddle.net/NJZrN/17/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a Quadtree data structure(or let's just say a tree)
Problem: I am trying to build a recursive tree using a function and data
I am trying to build an expression tree programmatically. I have in my input,
I'm trying to build a tree of LI/UL elements from an interal object I
I am trying to build a tree structure that represents a parsed configuration file
I am trying to build a tree from an xml file using jstree. I
I am trying to build the KD tree(independent) for image features. I have extracted
I'm trying to build a tree with data in the nodes as well as
I am trying to build an SWT Tree that has icons at the top
I'm trying to build an expression tree ( still ) but getting further! I

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.