I'm trying to build a tree (Flex 2) that keeps its nodes sorted, even when
you edit them.  I keep running into problems, though.  Here's the basic app,
without the auto-sorting:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; layout="vertical"
creationComplete="creationComplete()">
  <mx:Script>
    <![CDATA[
      import mx.collections.SortField;
      import mx.collections.Sort;
      import mx.collections.ArrayCollection;

      [Bindable] private var treedata:ArrayCollection =
        new ArrayCollection([
          {label:"node0"},
          {label:"node1"},
          {label:"node2"},
          {label:"node3"},
          {label:"node4"}
        ]);

      private function creationComplete():void
      {
        var sort:Sort = new Sort;
        sort.fields = [new SortField("label", true)];
        treedata.sort = sort;
      }
    ]]>
  </mx:Script>
  <mx:Tree
    id="tree"
    dataProvider="{treedata}"
    width="200"
    editable="true"
    />
</mx:Application>


Okay, so that lets you click on the nodes and edit them.  What should be
done to get the nodes to re-sort after editing?  I've tried:

    itemEditEnd="treedata.refresh()"

Doesn't work, because when itemEditEnd has been called, the dataprovider
hasn't actually been updated.

    itemEditEnd="callLater(treedata.refresh, [])"

Sorta works, until renamed "node2" to "node" and then it just disappears.
If you rename the last node to something that would make it the first node,
you get an error.

      private function reload():void
      {
        treedata.refresh();
        tree.dataProvider = treedata;
      }

    itemEditEnd="callLater(reload)"

This works ok, until you have a node with children like:
    {label:"node0", children:new ArrayCollection([{label:"child-0-1"},
{label:"child-0-2"}])},
And you expand it before renaming "node4" to "node".  Then you get an error.

I've also tried this without using itemEditEnd and instead having the node
be a custom object that throws events.  I run into the same issues.  What's
the "right" way to do something that should be a pretty simple task?

-- 
Jason

Reply via email to