我的 mxml 文件中有一个 datagrid 控件:
现在在我的 AS 文件中,在从 DB 获取数据时的结果函数中,我可以动态创建列。假设我创建了 1 列(客户名称):
private function GetDebtors_Result(event:ResultEvent):void
{
var arrayCol:Array = new Array();
var xmlSrc:XML = new XML("<main></main>");
var xmlTmp:XML;
var colClientname:DataGridColumn;
//Build an XML from DB data received (could as well use "event.result" directly to act as dataprovider for the datagrid, but I needed to break it down here)
for each(var o:Object in event.result)
{
xmlTmp = <row>
<CLIENTNAME>{o.CLIENTNAME}</CLIENTNAME>
</row>;
xmlSrc.appendChild(xmlTmp);
}
//Create the column CLIENTNAME
colClientname = new DataGridColumn("CLIENTNAME");
colClientname.headerText = "Client Name";
//Add the newly created column in the "Column" array.
arrayCol.push(colClientname);
//Use the "Column" array to set the columns of the datagrid.
dgSearch.columns = arrayCol;
//Populate the datagrid with the XML data.
dgSearch.dataProvider = xmlSrc.row;
}
这很好用。
现在问题来了:我需要添加包含复选框的第二列。将根据数据库中的数据选择或取消选择它们。我将通过更新与上面相同的“GetDebtors_Result”函数来展示我是如何做到的(添加的行被注释为“// ADDED ”):
private function GetDebtors_Result(event:ResultEvent):void
{
var arrayCol:Array = new Array();
var xmlSrc:XML = new XML("<main></main>");
var xmlTmp:XML;
var colClientname:DataGridColumn;
var colSel:DataGridColumn; // **ADDED**
//Build an XML from DB data received (could as well use "event.result" directly to act as dataprovider for the datagrid, but I needed to break it down here)
for each(var o:Object in event.result)
{
xmlTmp = <row>
<CLIENTNAME>{o.CLIENTNAME}</CLIENTNAME>
<SELECTED>{(o.SELECTED == 1)?true:false}</SELECTED> //**ADDED**
</row>;
xmlSrc.appendChild(xmlTmp);
}
//Create the column CLIENTNAME
colClientname = new DataGridColumn("CLIENTNAME");
colClientname.headerText = "Client Name";
//Create the column SELECTED
colSel = new DataGridColumn("SELECTED"); // **ADDED**
colSel.headerText = ""; // **ADDED**
colSel.itemRenderer = new ClassFactory(mx.controls.CheckBox); // **ADDED**
colSel.dataField = "SELECTED"; // **ADDED**
//Add the newly created column in the "Column" array.
arrayCol.push(colClientname);
//Add the "selection" column in the "Column" array.
arrayCol.push(colSel); // **ADDED**
//Use the "Column" array to set the columns of the datagrid.
dgSearch.columns = arrayCol;
//Populate the datagrid with the XML data.
dgSearch.dataProvider = xmlSrc.row;
}
问题 #1:出现复选框列,我可以选中和取消选中复选框,但在加载时,它们并未针对 DB 数据选中/取消选中。
问题#2:如何将一个函数与复选框相关联,例如一个将更新 XML 以便我可以将新数据保存到数据库的函数?
有人得到解决方案吗?先感谢您。
最佳答案
似乎是我今天看到的一个非常古老的问题。
希望你现在已经找到了解决方案,以防万一有人遇到同样的问题:
在向列添加复选框时 - 只需首先实例化它:
var chkTempCheck: Checkbox = new CheckBox();
然后设置所需的所有属性:
chkTempCheck.selected = o.dBColumnToDecideCheckUnCheck
这里的 'o' 是你在 event.result 中使用的对象。
这肯定会奏效!
关于apache-flex - 弹性 3 : dynamically created checkbox column in datagrid - data population issues and event listener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14279576/