国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發 > 正文

5.9.為List的項編輯器添加格式化和驗證數據

2024-04-27 13:51:57
字體:
來源:轉載
供稿:網友
5.9.1.問題
在提交輸入值到列表之前驗證用戶在一個條目編輯器中輸入的所有數據.
5.9.2.解決方法
在itemEditEnd 事件上,使用ListBase 類的itemEditorInstance 屬性從條目編輯器中重新獲得文本內容并解析其結果。
5.9.3討論
當用戶開始和結束編輯一個列表中的條目時,為了驗證和格式化所有輸入數據,必須偵聽由List 發出的條目編輯事件。當條目編輯編輯完以后List 控件發出三個事件

itemEditBegin
當editedItemPosition 屬性已設置且條目可編輯后調度。當事件被觸發,List 組件使用createItemEditor 方法創建一個itemEditor 對象并從條目拷貝數據屬性到編輯器。默認情況下,itemEditor 對象是一個TextInput 控件的實例.使用List 控件的itemEditor 屬性來指定一個定制itemEditor 類并最終設置itemEditorInstance,來停止創建編輯器,可以將preventDefault 作為默認事件偵聽器的一部分來調用.

itemEditBeginning
當用戶準備好編輯項目(例如,在項目上釋放鼠標按鍵)后調度。跳到List 上面或其內部,或以任何方式試圖編輯列表,該事件的默認偵聽器設置List.editedItemPosition 屬性到有焦點的條目來啟動條目編輯會話,為該事件寫一個你自己的事件偵聽器來拒絕編輯特別的條目和條目組。調用你自己事件偵聽器中的preventDefault 方法來防止默認的偵聽器執行。

itemEditEnd
當項目編輯會話因任何原因而結束時調度。List 組件有一個針對該事件的默認處理過程,它從itemEditor 拷貝數據到List 控件的數據提供者,默認情況下,List 使用List 控件中的editorDataField 屬性來決定itemEditor 的屬性包含新數據并使用該新數據更新數據提供者條目,由于默認的itemEditor 是TextInput 控件,editorDataField 屬性的默認值是text,為TextInput 包含新的條目數據指定text屬性然后調用destroyItemEditor,清除itemEditor.在事件偵聽方法內,你可以通過編輯器修改返回數據到List 組件。在你的事件偵聽器中,檢查itemEditor 中輸入的數據,若數據不正確,你可以調用preventDefault 來讓flex 停止傳遞新的數據到List 組件和關閉編輯器.

下面的例子中,若基于正則表達式,事件的preventDefault 方法用來防止編緝器把關閉和保存值給dataProvider ,若被傳遞給itemEditor 的值是正確的,則將該字符串格式化為適當的姓和名并保存該結果.
+展開
-XML
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxmlwidth="400height="300">
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
import mx.controls.TextInput;
import mx.events.ListEvent;
import mx.collections.ArrayCollection;
[Bindable]
private var dp:ArrayCollection = new ArrayCollection([{name:"John Smith" , positionType:1}
, {name:"Ellen Smith" , positionType:2}
, {name:"James Smith" , positionType:3}
, {name:"Jane Smith" , positionType:4}]);
//下面定義的這個itemEditEnd 事件的事件偵聽器,注意事件是一個包含reason 屬性的ListEvent 對象,該屬性將被用作判斷用戶是否修改了itemEditor 的值。
public function formatData(event:ListEvent):void {
// Check the reason for the event.
if(event.reason == "cancelled") {
// Do not update cell.
return;
}
// Get the new data value from the editor.
var newData:String = TextInput(event.currentTarget.itemEditorInstance).text;
// Determine if the new value is an empty String.
var reg:RegExp = //d/; 
if(newData == ""|| reg.test(newData)) {
// Prevent the user from removing focus,
// and leave the cell editor open.
event.preventDefault();
// Use the errorString to inform the user that something is wrong.
TextInput(listInstance.itemEditorInstance).setStyle("borderColor",0xff0000);
TextInput(listInstance.itemEditorInstance).errorString = "Enter a valid string." ;
return void ;
}
//test for FirstName LastName format
reg = //w+./s./w+/
if (!reg.test(newData)) {
event.preventDefault();
TextInput( listInstance.itemEditorInstance).setStyle( "borderColor", 0xff0000);
TextInput(listInstance.itemEditorInstance).errorString = "Enter first name and last name";
else{
//make sure the name is properly formatted
var firstName:String = newData.substring(0, newData.indexOf(" " ));
var lastName:String = newData.substring(newData.indexOf(" " )+1);
firstName = firstName.charAt(0).toUpperCase() + firstName.substr(1);
lastName = lastName.charAt(0).toUpperCase() + lastName.substr(1);
TextInput(listInstance.itemEditorInstance).text = firstName+" " +lastName;
newData = newData.charAt(0).toLocaleUpperCase()+ newData.substring(1, newData.indexOf(" "
))+ newData.charAt(newData.indexOf(" " )+1)+ newData.substring(newData.indexOf(" " )+2);
}
}

]]>
</mx:Script>
<mx:List id="listInstancedataProvider="{dp}editable="truelabelField="nameitemEditEnd="formatData(event)width="300"/>
</mx:Canvas>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平度市| 平阳县| 长兴县| 邹平县| 浠水县| 漳浦县| 循化| 台中县| 花莲县| 松溪县| 通江县| 马关县| 吴桥县| 巩义市| 胶南市| 邹平县| 视频| 澳门| 若羌县| 华蓥市| 林口县| 武川县| 元谋县| 乌拉特前旗| 云南省| 炎陵县| 衡水市| 洛川县| 吉首市| 榆树市| 涟源市| 乳源| 德保县| 宽甸| 敦化市| 格尔木市| 隆化县| 大洼县| 白朗县| 楚雄市| 息烽县|