2011-07-26 85 views
0

我使用PHP的PHP 4.5,ZendAMF,我讀了我不需要使用HTTPService來執行我想要的操作。如何將Flex複選框數組發送到mysql服務器?

表結構:

people: {pID, pName} 
departments: {deptID, deptName} 
people_departments: {pID, deptName} 

我有一個稍微複雜的柔性腳本,People.mxml。在PersonAdd狀態下,我有一個將用於發送數據到mysql服務器的表單。在這種形式下,我有一個轉發器,它將爲我的數據庫中的每個部門創建一個複選框。當我點擊添加按鈕時,我想要插入數據到peoplepeople_departments表中。

直放站代碼:

<mx:HBox> 
    <mx:Repeater id="checkBoxRepeater" 
       dataProvider="{getDepartmentsResult.lastResult}"> 
    <s:CheckBox id="deptCB" 
       label="{checkBoxRepeater.currentItem.deptName}"/> 
    </mx:Repeater> 
</mx:HBox> 

在我peopleService.php文件,我將有功能createPerson(People $item,$deptArray)和函數裏,我會執行兩個SQL查詢。一個查詢會將數據插入People表中,另一個將數據插入到people_departments中,people_departments.pID =新插入的人的ID。

如您所見,在上面的Repeater代碼中,複選框作爲屬性標籤。但是,當我將dept_Array(類型ArrayCollection)發送到服務器時,前者需要包含deptID。我該怎麼做?

這是我如何創建People.mxml的dept_Array發送到服務器:

protected function button_clickHandler(event:MouseEvent):void 
{ 
var dept_array:Array=[]; 
var idx:int; 
var getDepts_array:ArrayCollection=new ArrayCollection; 
getDepts_array=getDepartmentsResult.lastResult as ArrayCollection; 
var len:int=getDepts_array.length; 
for (idx=0; idx < len; idx++) 
{ 
    if (deptCB[idx].selected) 
    { 
    dept_array.push(deptCB[idx].label); //here I need to be able to push the ID of selected department. 
    } 
} 
} 

[編輯]我使用的是S:複選框,我沒有數據屬性。 MX:複選框沒有,但我不能在我的項目中使用MX:複選框組件。

我將不勝感激任何幫助。另外,是否有更好的方法來做到這一點?

回答

0

我解決了這個問題,使用屬性automationName發送與作爲chec顯示的itemName相關的ID kbox標籤。

請注意,火花複選框不存在data屬性。如果您使用mx:複選框,則您擁有數據,您可以使用它發送鏈接到相關商品的ID。

<mx:Tile id="myTile"> 
    <mx:Repeater id="checkBoxRepeater" 
       dataProvider="{getItemsResult.lastResult}" 
       startingIndex="0"> 
    <s:CheckBox label="{checkBoxRepeater.currentItem.myItemName}" 
       id="checkbox" 
       automationName="{String(checkBoxRepeater.currentItem.myItemID)}" 
       change="checkbox_changeHandler(event,event.currentTarget.repeaterIndex)"/>^ 
    </mx:Repeater> 
</mx:Tile> 

我知道這可能不是最好的解決辦法,但我顯然做這個司空見慣任務的第一人,這爲我工作。

然後,將發送所選擇的複選框的ID的排列,我使用下面的代碼,在我submitBtn_clickHandler函數定義:

var items_array:Array=[]; 
var idx:int; 
var getitems_array:ArrayCollection=new ArrayCollection; 
getitems_array=getItemsResult.lastResult as ArrayCollection; 
var len:int=getitems_array.length; 
for (idx=0; idx < len; idx++) 
    { 
    var element:CheckBox=myTile.getElementAt(idx) as CheckBox; 
    if (element.selected) 
    { 
     items_array.push(element.automationName); 
    } 
    } 

我然後傳遞items_array作爲附加參數到我ItemService。 createItem函數。

0

嘗試將選定的屬性與getDepartmentsResult綁定到選定的狀態保存:

<mx:HBox> 
    <mx:Repeater id="checkBoxRepeater" 
       dataProvider="{getDepartmentsResult.lastResult}"> 
    <s:CheckBox id="deptCB" 
       label="{checkBoxRepeater.currentItem.deptName}" selected={checkBoxRepeater.currentItem.deptSelected}/> 
    </mx:Repeater> 
</mx:HBox> 

您的集合迭代後得到所選部門:

protected function button_clickHandler(event:MouseEvent):void 
{ 
var dept_array:Array=[]; 
var idx:int; 
var getDepts_array:ArrayCollection=new ArrayCollection; 
getDepts_array=getDepartmentsResult.lastResult as ArrayCollection; 
var len:int=getDepts_array.length; 
for each (var dept:Object in getDepts_array) { 
    // ... 
} 
    if (dept.selected) 
    { 
    dept_array.push(dept.label); //here I need to be able to push the ID of selected department. 
    } 

} }

+0

這是行不通的,因爲首先,我的表中沒有'deptSelected'字段,其次,'selected'是一個布爾屬性。 我最終所做的就是像往常一樣激活文檔,並仔細檢查整個列表。我發現一個屬性可以接受一個字符串,並且我調整了一下,並使用'automationName'屬性來完成我想要做的事情。 – shailenTJ

+0

'' – shailenTJ

相關問題