gpt4 book ai didi

apache-flex - 弹性 : Updating a Tree control

转载 作者:行者123 更新时间:2023-12-04 02:41:33 24 4
gpt4 key购买 nike

我有一个带有复选框的树控件,它使用来自 http://www.sephiroth.it/file_detail.php?id=151# 的控件

不知何故,当我更改 dataProvider(即通过单击复选框)时,我无法更新控件,我可以让它更新的唯一方法是使用滚动条。如何强制更新?我已经尝试了所有我能想到的方法? (见下方更新)

另外如何重置树(折叠所有节点,滚动到大树的顶部)?

package offerta.monkeywrench.components
{

import offerta.monkeywrench.components.componentClasses.TreeCheckBoxItemRenderer;

import mx.collections.ArrayCollection;

import mx.events.TreeEvent;

public class WatchTree extends TreeCheckBox
{

public var idProperty:String;

public var watchFactory:Function;

private var _wSet:Boolean = false;

/* clientId: */

private var _clientId:String;

[Bindable]
public function get clientId():String
{
return _clientId;
}

public function set clientId(value:String):void
{
this._clientId = value;
}

/* //clientId */

/* watching: */

private var _watching:ArrayCollection;

[Bindable]
public function set watching(value:ArrayCollection):void
{
this._watching = value;
}

public function get watching():ArrayCollection
{
return this._watching;
}

/* //watching */

override public function initialize() :void
{
super.initialize();
addEventListener("itemCheck", onItemCheck, false, 0, true);
}

private function isWatching(id:String):Boolean
{
for each(var w:Object in this._watching)
{
if(w[this.idProperty]==id) return true;
}
return false;
}

private function onItemCheck(event:TreeEvent):void
{
var item:Object = event.item as Object;
var currentValue:uint = (event.itemRenderer as TreeCheckBoxItemRenderer).checkBox.checkState;
if(item.children==null)
{
currentValue==2 ? addWatch(item.Id) : removeWatch(item.Id);
}
else
{
for each(var x:Object in item.children)
{
currentValue==2 ? addWatch(x.Id) : removeWatch(x.Id);
}
}
updateParents(item, currentValue);
updateChilds(item, currentValue);
this.dataProvider.refresh();
super.invalidateProperties();
super.invalidateDisplayList();
super.updateDisplayList(this.unscaledWidth, this.unscaledHeight);
}

private function updateParents(item:Object, value:uint):void
{
var checkValue:String = (value == ( 1 << 1 | 2 << 1 ) ? "2" : value == ( 1 << 1 ) ? "1" : "0");
var parentNode:Object = item.parent;
if(parentNode)
{
for each(var x:Object in parentNode.children)
{
if(x.checked != checkValue)
{
checkValue = "2"
}
}
parentNode.checked = checkValue;
updateParents(parentNode, value);
}
}

private function updateChilds(item:Object, value:uint):void
{
var middle:Boolean = (value&2<<1)==(2<<1);

if(item.children!=null && item.children.length>0&&!middle)
{
for each(var x:Object in item.children)
{
x.checked = value == (1<<1|2<<1) ? "2" : value==(1<<1) ? "1" : "0";
updateChilds(x, value);
}
}
}

private function addWatch(id:String):void
{
if(isWatching(id)) return;
this._watching.addItem(this.watchFactory(id, this.clientId));
}

private function removeWatch(id:String):void
{
for(var i:int=0, n:int=this._watching.length; i<n; ++i)
{
if(this._watching[i][this.idProperty]==id)
{
this._watching.removeItemAt(i);
return;
}
}
}

public function update(__watching:ArrayCollection, __clientId:String):void
{
clientId = __clientId;
watching = __watching;
if(this.dataProvider!=null)
{
var ws:ArrayCollection = ArrayCollection(this.dataProvider);
for each(var group:Object in ws)
{
var count:int = 0;
for each(var child:Object in group.children)
{
if(isWatching(child.Id))
{
child.checked = "1";
count++;
}
}
group.checked = (count==0 ? "0" : (count==group.children.length ? "1" : "2"));
}
this._wSet = false;

var dp:ArrayCollection = ArrayCollection(this.dataProvider);
dp.refresh();
super.invalidateProperties();
super.invalidateDisplayList();
super.updateDisplayList(this.unscaledWidth, this.unscaledHeight);

//scroll up the list???

//collapse? (doesn't work)
this.expandItem(null, false);
}
}

}

}

最佳答案

我发现 Flex 中的 Tree 控件有点敏感。我最终强制重绘的方式是断开 dataProvider 并重新连接它,然后强制验证,有点像这样:

private function forceRedraw(tree:Tree, dataProvider:Object):void
{
var scrollPosition:Number = tree.verticalScrollPosition;
var openItems:Object = tree.openItems;
tree.dataProvider = dataProvider;
tree.openItems = openItems;
tree.validateNow();
tree.verticalScrollPosition = scrollPosition;
}

我想这顺便回答了您问题的第二部分,因为您所要做的就是将 openItems 集合清零并将 verticalScrollPosition 设置为 0。

关于apache-flex - 弹性 : Updating a Tree control,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/416139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com