Flash CS3 Documentation |
|||
| ActionScript 2.0 Components Language Reference > DataSet component > DataSet.addSort() | |||
Flash Player 7.
Flash MX Professional 2004.
dataSetInstance.addSort(name,fieldList,sortOptions)
name A string that specifies the name of the sort.
fieldList An array of strings that specify the field names to sort on.
sortOptions One or more of the following integer (constant) values, which indicate what options are used for this sort. Separate multiple values using the bitwise OR operator (|). Specify one or more of the following values:
DataSetIterator.Ascending Sorts items in ascending order. This is the default sort option, if none is specified.DataSetIterator.Descending Sorts items in descending order based on item properties specified.DataSetIterator.Unique Prevents the sort if any fields have like values. DataSetIterator.CaseInsensitive Ignores case when comparing two strings during the sort operation. By default, sorts are case-sensitive when the property being sorted on is a string.A DataSetError exception is thrown when DataSetIterator.Unique is specified as a sort option and the data being sorted is not unique, when the specified sort name has already been added, or when a property specified in the fieldList array does not exist in this data set.
Nothing.
Method; creates a new ascending or descending sort for the current iterator based on the properties specified by the fieldList parameter. Flash automatically assigns the new sort to the current iterator after it is created, and then stores it in the sorting collection for later retrieval.
The following code creates a new sort named "nameSort" that performs a descending, case-insensitive sort on the DataSet object's "name" field.
import mx.data.components.datasetclasses.DataSetIterator;
my_ds.addItem({name:"Milton", years:3});
my_ds.addItem({name:"mark", years:3});
my_ds.addItem({name:"Sarah", years:1});
my_ds.addItem({name:"michael", years:2});
my_ds.addItem({name:"Frank", years:2});
my_ds.addSort("nameSort", ["name"], DataSetIterator.Descending | DataSetIterator.Unique | DataSetIterator.CaseInsensitive);
In the following example, you can dynamically add data to a DataSet component by entering first and last names into TextInput component instances on the Stage and clicking the Submit button. After you add items to the DataSet component, you can clear the data set by clicking the Clear button on the Stage. Drag a DataGrid component to the Stage, and give it an instance name of my_dg. Drag two Button components to the Stage, and give them instance names of submit_button and clear_button. Drag a DataSet component to the Stage, and give it an instance name of my_ds. Drag two TextInput components to the Stage, and give them instance names of firstName_ti and lastName_ti. Drag an Alert component into the current document's library. Add two component properties, firstName and lastName, to the DataSet component's schema by using the Schema tab of the Component inspector. Next, add a data binding from the dataProvider property of the DataSet component to the dataProvider property of the DataGrid component by using the Binding tab in the Component inspector. Finally, paste the following code in the first frame of the main timeline:
import mx.controls.Alert;
my_ds.addSort("lastFirst", ["lastName", "firstName"]);
my_dg.enabled = false;
clear_button.enabled = false;
submit_button.label = "Submit";
clear_button.label = "Clear";
my_ds.addEventListener("addItem", addItemListener);
my_ds.addEventListener("modelChanged", modelChangedListener);
submit_button.addEventListener("click", submitListener);
clear_button.addEventListener("click", clearListener);
function modelChangedListener(evt_obj:Object):Void {
my_dg.enabled = (evt_obj.target.length > 0);
clear_button.enabled = my_dg.enabled;
}
function submitListener(evt_obj:Object):Void {
my_ds.addItem({firstName:firstName_ti.text, lastName:lastName_ti.text});
}
function addItemListener(evt_obj:Object):Void {
if ((evt_obj.item.firstName.length == 0) || (evt_obj.item.lastName.length == 0)) {
Alert.show("Error, first name or last name cannot be blank.", "Error", Alert.OK, _level0);
evt_obj.result = false;
} else {
firstName_ti.text = "";
lastName_ti.text = "";
}
}
function clearListener(evt_obj:Object):Void {
Alert.show("Are you sure you want to clear the data?", "Warning", Alert.OK | Alert.CANCEL, _level0, clearConfirmListener);
}
function clearConfirmListener(evt_obj:Object):Void {
switch (evt_obj.detail) {
case Alert.OK:
my_ds.clear();
break;
case Alert.CANCEL:
break;
}
}
Select Control > Test Movie to test the document in the authoring environment. Enter some text into both of the TextInput instances and then click the Submit button. A a new item should be added to the DataGrid instance. Clicking the Clear button should display an Alert component instance with a confirmation message asking if you want to clear the contents of the DataGrid. Clicking OK clears the dataProvider property of the DataSet component (and then the dataProvider property of the DataGrid component, because of the binding). Clicking Cancel dismisses the Alert component instance.
Flash CS3