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

  • Home
  • SEARCH
  • 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 9137093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:03:00+00:00 2026-06-17T09:03:00+00:00

I have a TreeView rendered inside a dialog box. With the dialog box I

  • 0

I have a TreeView rendered inside a dialog box. With the dialog box I have a Clear Selections button, My Question is how do I restore the treeview to the default un-expanded state.

Currently If I expand the nodes within the treeview and hit the clear button and open the dialog box again, the tree view is still expanded.

On document.ready for my Dailog box Javascript file, I have the following to initialize the treeview. Should I put this inside a function and upon clear button in dialog box, call this function again to reset?

JS File

//This load the tree view on document ready

$("#parts ul").treeview({
  persist: "location",
  collapsed: true,
  prerendered: false
});

Clear: function () {
  $('#partTreeView :checked').removeAttr('checked');
  $("#PartDisplay").text('');
  $("#inputReps").text('');
  $("#parts").attr("style", "display:inline;");
  $("#inputParts").attr("style", "display:none;");
  PartDialog.dialog("destroy");
  //Toggle/Un-expand tree view here if already expanded here
}

View HTML

<!-- This div opens the dialog box -->
<div class="bodyContent"> 
  <span class="leftContent">
    @Html.Label("Select Parts")
  </span>
  <span class="rightContent">
    <span id="PartChildDialogLink" class="treeViewLink">Click here to Select Parts</span>
    <br />
    <span id="PartDisplay"></span>
    @Html.HiddenFor(model => model.PartDescription)
    <input id="ServiceEntryID" type="hidden" value="0" />
  </span>
</div>

<!-- This div contains the dialog box and the tree viwe rendered inside of it -->
<div id="partTreeView" title="Dialog Title" style="font-size: 10px; font-weight: normal; overflow: scroll; width: 800px; height: 450px; display: none;">
  <div id="parts">
    <!-- This line renders the treeview just fine -->
    @Html.RenderTree(CacheHelper.Parts(), ec => ec.Name, ec => ec.Children.ToList(), ec => (ec.ID).ToString(), null, "part")
  </div>
  <div id="inputParts" style="display: none;"></div>
  <div id="inputPartTemp" style="display: none;"></div>
</div>
  • 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-17T09:03:01+00:00Added an answer on June 17, 2026 at 9:03 am

    A treeview can have a tree view control. That control is bound by the tree view script to 3 actions: collapse tree, expand tree and toggle tree.

    The tree view control has to be an element containing 3 anchor links, one for each action.

    To add a tree view control add the following HTML below or above your treeview:

    <div id="myTreeviewcontrol">
      <a href="#"> collapse </a><a href="#"> expand </a><a href="#"> toggle </a>
    </div>
    

    You can delete the text between the anchor tags to have an “invisible” control if you don’t want the users to be able to collapse/expand/toggle and only plan to collapse the tree programmatically.

    To let the tree view script know about the control so it can bind the 3 actions to it, add the following to your treeview:

    $("#parts ul").treeview({
        persist: "location",
        collapsed: true,
        prerendered: false,
        control: "#myTreeviewcontrol"
    });
    

    To programmatically collapse the tree now you simply trigger the first link in your control, like this:

    $('#myTreeviewcontrol a:eq(0)').click();
    

    DEMO – Programmatically collapsing the tree


    In the above DEMO I left the text in the anchors so you can use them
    too if you like. I added a button at the bottom of the tree that
    when clicked, will collapse the tree by programmatically triggering
    the control’s collapse link


    Applying those changes


    To apply those changes to your code would involve a change to your HTMLview and the script.

    Your view changes:

    <div id="partTreeView" title="Dialog Title" style="font-size: 10px; font-weight: normal;
                overflow: scroll; width: 800px; height: 450px; display: none;">
    
      <!-- You could insert Tree View Controller here -->
      <div id="myTreeviewcontrol">
        <a href="#"></a><a href="#"></a><a href="#"></a>
      </div>
    
      <div id="parts">
        //This line renders the treeview just fine 
        @Html.RenderTree(CacheHelper.Parts(), ec => ec.Name, ec => ec.Children.ToList(), ec => (ec.ID).ToString(), null, "part")
      </div>
      <div id="inputParts" style="display: none;"></div>
      <div id="inputPartTemp" style="display: none;"></div>
    </div>
    

    Your script changes:

    // Let the treeview know "who" your controller is
    $("#parts ul").treeview({
      persist: "location",
      collapsed: true,
      prerendered: false,
      control: "#myTreeviewcontrol" // <-- Assign id of controller to control option
    });
    

    Now your treeview and the treeview controller are bound and the controller knows which treeview to collaps when the collaps link is clicked.

    // Tell your controller to collapse the tree
    Clear: function () {
      $('#partTreeView :checked').removeAttr('checked');
      $("#PartDisplay").text('');
      $("#inputReps").text('');
      $("#parts").attr("style", "display:inline;");
      $("#inputParts").attr("style", "display:none;");
      PartDialog.dialog("destroy");
      //Toggle/Un-expand tree view here if already expanded here
      $('#myTreeviewcontrol a:eq(0)').click(); //<-- Tell the treeview controller to collaps the treeview it is bound to
    }
    

    Adding the last line triggers the click event of the first link in the treeview controller, causing the controller to collaps the treeview it has been associated with.


    Additional Notes


    The tree view script will always expect 3 links inside the element you assign as the control and always bind the actions in the exact same order as mentioned at the start.

    • 1st link = collapse tree
    • 2nd link = expand tree
    • 3rd link = toggle tree

    Looking at the jQuery TreeView source there is the following code showing how it works (at the bottom of the function you see how it triggers the links):

    // factory for treecontroller
    function treeController(tree, control) {
      // factory for click handlers
      function handler(filter) {
        return function () {
          // reuse toggle event handler, applying the elements to toggle
          // start searching for all hitareas
          toggler.apply($("div." + CLASSES.hitarea, tree).filter(function () {
            // for plain toggle, no filter is provided, otherwise we need to check the parent element
            return filter ? $(this).parent("." + filter).length : true;
          }));
          return false;
        };
      }
      // click on first element to collapse tree
      $("a:eq(0)", control).click(handler(CLASSES.collapsable));
      // click on second to expand tree
      $("a:eq(1)", control).click(handler(CLASSES.expandable));
      // click on third to toggle tree
      $("a:eq(2)", control).click(handler());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple win32 application (not dialog box), and treeview in this application. All
I have a TreeView <asp:TreeView ID=tvCategories runat=server ShowLines=false ShowExpandCollapse=true ExpandDepth=0 /> by default the
I have a treeview and a button. I want to disable the button (IsEnabled=false)
I have a TreeView with ContextMenu, and inside that menu I want to bind
I have a TreeView (the only control) inside a Grid , and I only
I have a treeview-widget inside a ScrolledWindow , which is populated during runtime. I
I have a treeview with an editable CellRendererText: self.renderer = gtk.CellRendererText() self.renderer.set_property('editable', True) But
I have a treeview which contains, per node, a key and text. However, there
I have a treeview that contains icons. The icons are ugly. and by ugly
I have a TreeView that launches a new window when each of its TreeViewItems

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.