我有一个连接到数组的数据网格。如果我将数据硬编码到数组中var data1:Array = [{Name:"Joe Smith"}];
,则数据会正确显示。但是,我需要通过输入文本字段和添加按钮动态添加数据。这是我所拥有的一切:
public var grid1:DataGrid = new DataGrid();
public var i1:Number = 0;
var data1:Array = [];
public function Datagrid() {
addBtn.addEventListener(MouseEvent.CLICK, addFun);
grid1.move(20,120);
grid1.setSize(760, 660);
grid1.columns = ["Member"];
grid1.dataProvider = new DataProvider(data1);
}
public function addFun($e:MouseEvent):void{
data1[i1] = [];
//this is the line I need help with - how do I format this so that the
//datagrid can read it from the array as proper input?
data1[i1][0] = "{Member:"+addNameTxt.text"}";
i1++;
grid1.dataProvider = new DataProvider(data1);
trace(data1); // returns "{Member:Joe Smith}, but nothing appears on the datagrid
}
}
}
感谢任何帮助!
答案 0 :(得分:1)
data1
数组应该是一个维度。
而不是data1[i1][0] =
使用data1[i1] =
。
这是不正确的:
"{Member:" + addNameTxt.text + "}";
这是一个字符串。它应该是一个对象:
{Member: addNameTxt.text};
您的代码应如下所示:
function addFun($e: MouseEvent): void
{
data1[i1] = {Member:addNameTxt.text};
i1++;
grid1.dataProvider = new DataProvider(data1);
}