Flash CS3 Documentation |
|||
| ActionScript 2.0 Components Language Reference > DataSet component > DataSet.addItem() | |||
Flash Player 7.
Flash MX Professional 2004.
dataSetInstance.addItem([obj])
obj An object to add to this collection. This parameter is optional.
A Boolean value: true if the item was added to the collection, false if it was not.
Method; adds the specified record (transfer object) to the collection for management. The newly added item becomes the current item of the data set. If no obj parameter is specified, a new object is created automatically by means of DataSet.createItem().
The location of the new item in the collection depends on whether a sort has been specified for the current iterator. If no sort is in use, the item is added to the end of the collection. If a sort is in use, the item is added to the collection according to its position in the current sort.
For more information on initialization and construction of the transfer object, see DataSet.createItem().
The following example uses DataSet.addItem() to create a new item and add it to the data set:
my_ds.addEventListener("addItem", addItemListener);
my_ds.addItem({name:"Bobo", occupation:"clown"});
function addItemListener(evt_obj:Object):Void {
trace("adding item");
}
The following example demonstrates how you can accept or reject an item's insertion into the DataSet by setting the result to true or false within the handler for the addItem event. Drag a DataSet component to the Stage, and assign it an instance name of my_ds. Drag a DataGrid component to the Stage, and give it an instance name of my_dg. Drag a CheckBox component to the Stage, and give it an instance name of my_ch. Drag a Button component to the Stage, and give it an instance name of submit_button. Add two properties, name and occupation, to the DataSet component by using the Schema tab of the Component inspector. Create a binding between the my_ds.dataProvider property and the my_dg.dataProvider property by using the Bindings panel of the Component inspector. Add the following ActionScript to Frame 1 of the main timeline:
my_ds.addEventListener("addItem", addItemListener);
submit_button.addEventListener("click", submitListener);
function userHasAdminPrivs():Boolean {
return my_ch.selected;
}
function addItemListener(evt_obj:Object):Void {
if (userHasAdminPrivs()) {
// Allow the item addition.
evt_obj.result = true;
trace("Item added");
} else {
// Don't allow the item addition; user doesn't have admin privileges.
evt_obj.result = false;
trace("Error, insufficient permissions");
}
}
function submitListener(evt_obj:Object):Void {
my_ds.addItem({name:"bobo", occupation:"clown"});
}
Flash CS3