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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:56:18+00:00 2026-05-24T07:56:18+00:00

I have a dijit.Tree backed by a ForestStoreModel that uses a custom data store

  • 0

I have a dijit.Tree backed by a ForestStoreModel that uses a custom data store to provide the data. It works well, but I want to provide the ability for the user to rearrange the top-level items (and only the top level) items via the dojo drag-and-drop facility.

The problem is the the ForestStoreModel::pasteItem function checks to see if item is a child of the root and then passes a null to the TreeStoreModel::pasteItem.

pasteItem: function(/*Item*/ childItem, /*Item*/ oldParentItem, /*Item*/ newParentItem, /*Boolean*/ bCopy, /*int?*/ insertIndex){
        // summary:
        //      Move or copy an item from one parent item to another.
        //      Used in drag & drop
        if(oldParentItem === this.root){
            if(!bCopy){
                // It's onLeaveRoot()'s responsibility to modify the item so it no longer matches
                // this.query... thus triggering an onChildrenChange() event to notify the Tree
                // that this element is no longer a child of the root node
                this.onLeaveRoot(childItem);
            }
        }
        dijit.tree.TreeStoreModel.prototype.pasteItem.call(this, childItem,
            oldParentItem === this.root ? null : oldParentItem,
            newParentItem === this.root ? null : newParentItem,
            bCopy,
            insertIndex
        );
        if(newParentItem === this.root){
            // It's onAddToRoot()'s responsibility to modify the item so it matches
            // this.query... thus triggering an onChildrenChange() event to notify the Tree
            // that this element is now a child of the root node
            this.onAddToRoot(childItem);
        }
    }

The TreeStoreModel does not update the underlying data store if the passed parent items are null and the onLeaveRoot and onAddToRoot events do not pass in the insertIndex, so I cannot use them to update my data store (which seems like it would be a bit backwards, anyway).

At this point I think the only viable option is to extend the ForestStoreModel in order to allow me to set the synthetic $root$ item to a compatible data store object and allow the ForestStoreModel to pass it, unaltered, through to the TreeStoreModel.

Are there other way of approaching this problem?

Update

The eventual solution turned out to be even simpler than suggested. My ForestStoreModel is already a custom class because I am actually using the dojo 1.6 ObjectStore as a data source, so I can pass the desired index in the options argument to the object store put method. The fix was just a one-liner as I let the parent class take care of calling onLeaveRoot and onAddRoot:

pasteItem: function(/*Item*/ childItem, /*Item*/ oldParentItem, /*Item*/ newParentItem, /*Boolean*/ bCopy, /*int?*/ insertIndex){
    // Handle drag & drop at the root level
    if (oldParentItem === this.root && newParentItem === this.root){
        this.store.put(childItem, { index: insertIndex });
    } 
    this.inherited(arguments);
}
  • 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-24T07:56:19+00:00Added an answer on May 24, 2026 at 7:56 am

    You will need to subclass ForestTreeModel, you can’t escape that. But, you’ll only need to override pasteItem. You can’t pass the synthetic root to the TreeStoreModel because it doesn’t know anything about it.

    If you need to modify the underlying data store, you’re better off calling this.store.setValues() directly. That should trigger the onSetItem event and in turn call _requeryTop() for you, which will fetch the roots from the underlying store in whatever order you arrange them, so be sure to reflect that in your modification.

    dojo.declare('MyForestTreeModel', [ dijit.tree.ForestTreeModel ], {
      pasteItem: function(childItem, oldParentItem, newParentItem, bCopy, insertIndex) {
        if (oldParentItem == this.root && newParentItem == this.root) {
          if (!bCopy) { this.onLeaveRoot(childItem); }
          // modify the underlying store somehow so the call to _requeryTop() fetches
          // the items in the correct order.
          // (you decide what's 'order' and what new_order() returns)
          this.store.setValues(childItem, 'order', new_order(insertIndex));
          this.onAddRoot(childItem);
        } else {
          // call super
          this.inherited(arguments);
        }
      }
    });
    

    The other, easier way, is to manipulate this.root.children yourself and then emit the event onChildrenChange to notify the views. Beware, that method won’t persist the order.

    dojo.declare('MyForestTreeModel', [ dijit.tree.ForestTreeModel ], {
      pasteItem: function(childItem, oldParentItem, newParentItem, bCopy, insertIndex) {
        if (oldParentItem == this.root && newParentItem == this.root) {
          if (!bCopy) { this.onLeaveRoot(childItem); }
          // manipulate this.root.children to reorder childItem
          // remove child from the current position
          var children = dojo.filter(this.root.children, function(x) {
            return x != childItem;
          });
          // and insert it into the new index
          children.splice(insertIndex, 0, childItem);
          this.root.children = children;
          // notify views
          this.onChildrenChanged(this.root, children);
          this.onAddRoot(childItem);
        } else {
          // call super
          this.inherited(arguments);
        }
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dijit.Tree that's populated by a ItemFileReadStore using JSON. Because of our
I have some problems getting the custom formated JSON data into a dijit.form.FilteringSelect. It
I have a dijit.form.FilteringSelect component and I want to change the options dynamically. But
I have a page that uses several dijit form widgets. I also have an
I have a grid that will edit small chunks of data based on a
I have a dojo (dijit) select dropdown that calls a js function onChange. I
Hey guys! I have a dijit.form.ComboBox that needs have value checking applied against it.
Is there a good way to expand/close all the expandable nodes in a dijit.Tree
I have an text outline tree where i display a lot of information There
The problem: I have a trie and I want to return the information stored

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.