User Guide

DataSet.addItem() 341
The following example demonstrates how you can accept or reject an items 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"});
}
See also
DataSet.createItem()