如何将 Flex 复选框数组发送到 mysql 服务器?

发布于 2024-11-26 13:27:52 字数 1671 浏览 3 评论 0原文

我正在使用 FB for PHP 4.5、ZendAMF,并且我读到我不需要使用 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 组件。

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

I am using FB for PHP 4.5, ZendAMF, and I read that I do not need to use HTTPService for what I want to do.

Table structure:

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

I have a slightly complex flex script, People.mxml. In the PersonAdd state, I have a form which will be used to send data to the mysql server. In that form , I have a repeater that will create a checkbox for every department in my database. When I click the Add button, I want to insert data into the people and people_departments tables.

Repeater code:

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

In my peopleService.php file, I will have the function createPerson(People $item,$deptArray) and inside that function, I will execute two SQL queries. One query will insert data into the People table and the other one will insert data into people_departments, with people_departments.pID = ID of newly inserted person.

As you can see, in the Repeater code above, the checkbox as the attribute label. However, when I send the dept_Array (of type ArrayCollection) to the server, the former needs to contain deptIDs. How can I do so?

This is how I create the dept_Array in People.mxml to send to the server:

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.
  }
 }
}

[Edit] I am using an s:Checkbox and I do not have the data property. mx:Checkbox does but I cannot use the mx:Checkbox component in my project.

I would appreciate any help. Also, is there is a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

川水往事 2024-12-03 13:27:52

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

}
}

try to bind the selected property with your getDepartmentsResult to save the selected state :

<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>

After that iterate on your collection to get the selected department:

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.
  }

}
}

许你一世情深 2024-12-03 13:27:52

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

请注意,spark 复选框不存在 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 函数。

I solved the problem by using the attribute automationName to send the ID related to the itemName displayed as the checkbox label.

Note that the data attribute does not exist for the spark checkbox. If you use the mx:Checkbox, you have data, and you can use it to send the ID linked to your item in question.

<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>

I know that this may not be the best solution, but I am apparently the first person to do this commonplace task, and this worked for me.

And then, the send the array of the IDs of the selected checkboxes, I use the following code, defined in my submitBtn_clickHandler function:

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);
    }
  }

I then pass items_array as an additional parameter to my ItemService.createItem function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文