我正在通过一个datadic.xml文件来填充actionscript 3.0中的组合框,但我想在前11个属性的每个部分之间添加一个中断或标题然后中断,然后是最后26个属性。这是我的xml文件的一部分:
<codeSet>
<title>Census: population estimates for Northern Ireland </title>
<subtitle>By country, region and local authority</subtitle>
<txtSource>2011 Census</txtSource>
<txtSource2>2001Census</txtSource2>
<dateStart>2001</dateStart>
<dateEnd>2011</dateEnd>
<code id='94NI'>NORTHERN IRELAND</code>
<code id='N09000001'>Antrim and Newtownabbey</code>
然后actionscript加载xml并创建一个名为setLists的函数,这是一个填充组合框的简单循环,但现在我想要休息,所以应该是北爱尔兰(休息)Antrim和Newtownabbey接着是10然后(休息)等等。 ActionScript代码:
function setLists():void
{
var menuLength:int = xmlCodes.code.length();
for (var i:int=0; i<menuLength; i++)
{
myVarList1.addItem({label:xmlCodes.code[i].text(), data:xmlCodes.code[i].@id});
myVarList2.addItem({label:xmlCodes.code[i].text(), data:xmlCodes.code[i].@id});
if (
}
答案 0 :(得分:0)
如果标题下方的元素总数相同,请尝试以下方法:
function setLists():void
{
var menuLength:int = xmlCodes.code.length();
// I'm basing the "breaks" variable on this flow: title, break, 11 items, break, title, break, 26 items
//
var breaks:int = 3;
var breakCount:int = 0;
// Note we've added "breaks" to our menuLength
//
for (var i:int=0; i<menuLength + breaks; i++)
{
// 1, 12, and 14 are where we add breaks based on the flow mentioned above
//
if (i == 1 || i == 12 || i == 14) {
myVarList1.addItem({label:' ', data:null);
myVarList2.addItem({label:' ', data:null);
breakCount++;
} else {
myVarList1.addItem({label:xmlCodes.code[i-breakCount].text(), data:xmlCodes.code[i].@id});
myVarList2.addItem({label:xmlCodes.code[i-breakCount].text(), data:xmlCodes.code[i].@id});
}
}
答案 1 :(得分:0)
我认为“类别”和“孩子”之间的id
格式存在差异?此外,XML提供了具有以下模式的排序列表?
- CATEGORY1
- child1
- 的child2
- [...]
- child3
- CATEGORY2
- child1
- 的child2
- [...]
- child3
如果是这样,您可以尝试找出“类别”与其“孩子”之间ID的区别。一种方法是创建regular expression,只匹配属于类别的ID,并在获得匹配时采取不同的行动。
示例:
var categoryIdPattern:RegExp = /^\d{2,3}\w{2,3}$/; // Matches only strings consisting of 2-3 numbers followed by 2-3 word characters
var menuLength:int = xmlCodes.code.length();
for (var i:int = 0; i < menuLength; i++) {
myVarList1.addItem({label:xmlCodes.code[i].text(), data:xmlCodes.code[i].@id});
if (categoryIdPattern.exec(xmlCodes.code[i].@id as String) != null) {
//This is a category, add an empty row beneath the newly added value in the combobox
myVarList1.addItem({label:"", data:null});
}
}
编辑:我应该补充一点,上面的代码是未经测试的,只是说明了这个概念。
答案 2 :(得分:0)
我最后创建了一个新类来覆盖数据设置器,因此我可以在组合框中将值设置为'disabled:true'
var myVarList1:ComboBox = new ComboBox();
var myVarList2:ComboBox = new ComboBox();
myVarList1.dropdown.setStyle("cellRenderer", MyCustomCellRenderer);
myVarList2.dropdown.setStyle("cellRenderer", MyCustomCellRenderer);
然后调用我的setlists函数
function setLists():void
{
var menuLength:int = xmlCodes.code.length();
var breaks:int = 3;
var breaksCount:int = 0;
for (var i:int=0; i<menuLength; i++)
{
if(i == 1){
myVarList1.addItem({label: " ", disabled:true});
myVarList2.addItem({label: " ", disabled:true});
breaksCount++;
}
else if (i == 11){
myVarList1.addItem({label: " ", disabled:true});
myVarList2.addItem({label: " ", disabled:true});
breaksCount++;
}
else {
myVarList1.addItem({label:xmlCodes.code[i].text(), data:xmlCodes.code[i].@id});
myVarList2.addItem({label:xmlCodes.code[i].text(), data:xmlCodes.code[i].@id});
}
}
这是自定义功能,允许您将值设置为禁用。
package {
import fl.controls.listClasses.CellRenderer;
public class MyCustomCellRenderer extends CellRenderer
{
// override the data setter to set the enabled flag
override public function set data(value:Object):void
{
super.data = value;
this.enabled = !(value['disabled']);
}
}
}