我正在学习“一周内的 Flex”教程,我已经达到了他们在 Flex 中实现 MVC 模型的地步。在那之前一切都很好,但在视图中,他们使用 dropDownList 连接到 dataProvider 并保留 selectedIndex 未声明(默认为 -1)。我(想聪明点!)尝试设置 selectedIndex = 0 或 1 或任何其他数字。但是什么都没有发生,我怎么能这样做?
课程详情在这里:
http://www.adobe.com/devnet/flex/videotraining/exercises/ex2_06.html
在我看来,代码的重要部分是:
主应用:
创建数组:
[Bindable]
private var employees:ArrayCollection = new ArrayCollection();
这个数组填充了来自 httpservice 的日期:
for each (var emp:Object in employeeData)
{
employee = new Employee();
employee.firstName = emp.firstName;
employee.lastName = emp.lastName;
employee.id = emp.id;
employee.title = emp.title;
employee.email = emp.email;
employee.managerID = emp.managerID;
employee.department = emp.department;
employee.location = emp.location;
employee.deskLocation = emp.deskLocation;
employee.city = emp.city;
employee.state = emp.state;
employee.countryCode = emp.countryCode;
employee.postalCode = emp.postalCode;
employee.directDial = emp.directDial;
employee.hireDate = emp.hireDate;
employee.evaluation = emp.evaluation;
employee.phone = emp.phone;
employees.addItem(employee);
}
以数组形式传递给自定义组件:
<components:VehicleRequestForm employees="{employees}"/>
这个自定义表单捕获它并放入一个 dropDownList,但 selectedIndex 似乎不起作用,它仍然默认为 -1。我怎样才能解决这个问题?(赞赏所有理想):
<s:FormItem label="Employee:">
<s:DropDownList id="dropDownList"
dataProvider="{employees}"
labelField="lastName"
selectedIndex="1"/>
</s:FormItem>
<s:FormItem label="Office Phone:">
<s:TextInput id="phone"
text="{dropDownList.selectedItem.phone}"/>
ex2_08_solutions.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="850"
creationComplete="employeeService.send()"
xmlns:components="components.*">
<!-- Exercise 2.08: Creating an ArrayCollection of value objects -->
<!-- Styles ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<fx:Style source="Styles.css"/>
<!-- Script ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import valueObjects.Employee;
// variable declarations ------------------------------------
[Bindable]
private var employees:ArrayCollection = new ArrayCollection();
// getter/setters -------------------------------------------
// helper methods -------------------------------------------
// event handlers -------------------------------------------
protected function employeeService_resultHandler(event:ResultEvent):void
{
var employeeData:ArrayCollection = event.result.employees.employee;
var employee:Employee;
for each (var emp:Object in employeeData)
{
employee = new Employee();
employee.firstName = emp.firstName;
employee.lastName = emp.lastName;
employee.id = emp.id;
employee.title = emp.title;
employee.email = emp.email;
employee.managerID = emp.managerID;
employee.department = emp.department;
employee.location = emp.location;
employee.deskLocation = emp.deskLocation;
employee.city = emp.city;
employee.state = emp.state;
employee.countryCode = emp.countryCode;
employee.postalCode = emp.postalCode;
employee.directDial = emp.directDial;
employee.hireDate = emp.hireDate;
employee.evaluation = emp.evaluation;
employee.phone = emp.phone;
employees.addItem(employee);
}
}
protected function employeeService_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.faultString,"Fault Information");
}
]]>
</fx:Script>
<!-- Declarations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<fx:Declarations>
<s:HTTPService id="employeeService"
url="http://adobetes.com/f45iaw100/remoteData/employees.xml"
result="employeeService_resultHandler(event)"
fault="employeeService_faultHandler(event)"/>
</fx:Declarations>
<!-- UI components ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<s:Label x="10" y="34"
width="690" height="40"
text="Employee Portal: Vehicle Request Form"
styleName="titleHeader"/>
<components:VehicleRequestForm employees="{employees}"/>
</s:Application>
VehicleRequestForm.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()">
<!-- Styles ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- Script ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<fx:Script>
<![CDATA[
// import statements ----------------------------------------
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CalendarLayoutChangeEvent;
// variable declarations ------------------------------------
[Bindable]
public var employees:ArrayCollection;
// getter/setters -------------------------------------------
// helper methods -------------------------------------------
// event handlers -------------------------------------------
private function dateChangeHandler(event:CalendarLayoutChangeEvent):void
{
Alert.show('You have selected ' + event.target.selectedDate.toDateString());
if ((event.target.id == "returnDate") && (pickupDate.selectedDate > returnDate.selectedDate))
{
Alert.show("Pickup date must be scheduled before return date.");
}
if ((event.target.id == "pickupDate") && (pickupDate.selectedDate > returnDate.selectedDate) && (returnDate.selectedDate != null))
{
Alert.show("Pickup date must be scheduled before return date.")
}
}
private function init():void
{
pickupDate.addEventListener(CalendarLayoutChangeEvent.CHANGE, dateChangeHandler);
returnDate.addEventListener(CalendarLayoutChangeEvent.CHANGE, dateChangeHandler);
}
]]>
</fx:Script>
<!-- Declarations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<!-- UI components ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<s:Form x="10" y="70">
<s:FormItem label="Employee:">
<s:DropDownList id="dropDownList"
dataProvider="{employees}"
labelField="lastName"
selectedIndex="1"/>
</s:FormItem>
<s:FormItem label="Office Phone:">
<s:TextInput id="phone"
text="{dropDownList.selectedItem.phone}"/>
</s:FormItem>
<s:FormItem label="Mobile Phone:">
<s:TextInput id="mobilePhone"/>
</s:FormItem>
<s:FormHeading label="Dates Requested"/>
<s:FormItem label="Pickup Date:">
<mx:DateChooser id="pickupDate"/>
</s:FormItem>
<s:FormItem label="Return Date:">
<mx:DateChooser id="returnDate"/>
</s:FormItem>
<s:FormItem>
<s:Button id="submitButton"
label="Submit Request"/>
</s:FormItem>
</s:Form>
</s:Group>