【问题标题】:How to send an array of Flex checkboxes to a mysql server?如何将一组 Flex 复选框发送到 mysql 服务器?
【发布时间】:2011-07-28 13:18:09
【问题描述】:

我正在为 PHP 4.5、ZendAMF 使用 FB,并且我读到我不需要使用 HTTPService 来完成我想做的事情。

表结构:

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

我有一个稍微复杂的 flex 脚本,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:Checkbox,但我没有 data 属性。 mx:Checkbox 可以,但我不能在我的项目中使用 mx:Checkbox 组件。

我将不胜感激。另外,有没有更好的方法来做到这一点?

【问题讨论】:

    标签: php mysql apache-flex checkbox repeater


    【解决方案1】:

    尝试将所选属性与您的 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.
      }
    

    } }

    【讨论】:

    • 那行不通,因为首先,我的表中没有deptSelected 字段,其次,selected 是一个布尔属性。我最终做的是像往常一样启动文档,并仔细检查整个列表。我发现了一个可以接受字符串的属性,我作弊并使用automationName 属性来做我想做的事情。
    • &lt;s:CheckBox label="{checkBoxRepeater.currentItem.myItemName}" id="checkbox" automationName="{String(checkBoxRepeater.currentItem.myItemID)}" change="checkbox_changeHandler(event,event.currentTarget.repeaterIndex)"/&gt;
    【解决方案2】:

    我通过使用属性automationName 发送与显示为复选框标签的itemName 相关的ID 解决了这个问题。

    请注意,火花复选框不存在 data 属性。如果您使用 mx:Checkbox,则您有数据,您可以使用它来发送与您的相关项目相关联的 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 函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多