Example Number 5.16

Example Name: ProductsAdminGrid.fla

Language: ActionScript 1.0

Version: 1.0.1

Code:

#include "NetServices.as" 
#include "DataGlue.as" 

// Set up the combo boxes to be able to pick a value 
FComboBoxClass.prototype.pickValue = function (value) { 
  for (var i=0; i<this.getLength( ); i++) { 
    if (this.getItemAt(i).data == value) { 
      this.setSelectedIndex(i); 
      break; 
    } 
  } 
}; 

// General error handler for authoring environment 
function errorHandler (error) { 
 trace(error.description); 
}

// Responder objects 
var SearchResult = new Object( ); 
SearchResult.onResult = function (result_rs) { 
  // Put the contents of the recordset into the DataGrid 
  allProducts_dg.setDataProvider(result_rs); 
  // Don't allow editing of the ProductID primary key 
  allProducts_dg.getColumnAt(0).setEditable(false); 
}; 

SearchResult.onStatus = errorHandler; 

// Set up a responder object to handle recordsets for ComboBoxes 
function ComboBoxResponder (cbName) { 
  this.cbName = cbName; 
}

// The responder assumes that data is coming in with 
// ID column in [0] position and description column 
// in the [1] position 
ComboBoxResponder.prototype.onResult = function (result_rs) { 
  var fields = result_rs.getColumnNames( ); 
  var idField = '#' + fields[0] + '#'; 
  var descField = '#' + fields[1] + '#'; 
  DataGlue.bindFormatStrings(this.cbName, result_rs, descField,idField); 
}

ComboBoxResponder.prototype.onStatus = errorHandler; 

// Main responder for the Update, Insert, and Delete functions. 
// Display is to the Output window only. 
function MainServiceResponder ( ) { 
}

MainServiceResponder.prototype.onResult = function (result) { 
  trace(result); 
}; 

MainServiceResponder.prototype.onStatus = errorHandler; 

// Initialization code 
if (connected == null) { 
  connected = true; 
  NetServices.setDefaultGatewayUrl("http://localhost/flashservices/gateway"); 
  var my_conn = NetServices.createGatewayConnection( ); 
  my_conn.onStatus = errorHandler; 
  my_conn.setCredentials("admin", "1234"); // hardcoded username and password 
  var myService = my_conn.getService("com.oreilly.frdg.admin.ProductsAdmin"); 
}

// Set up the two combo boxes 
myService.getCategories(new ComboBoxResponder(categories_cb)); 
myService.getSuppliers(new ComboBoxResponder(suppliers_cb)); 

// Set up change handlers for combo boxes 
categories_cb.setChangeHandler("setCategory"); 
suppliers_cb.setChangeHandler("setSupplier"); 

// Set up the DataGrid 
allProducts_dg.setEditable(true); 
allProducts_dg.setSelectMultiple(true); 

// Each time a row is edited, flag it for update 
allProducts_dg.setEditHandler("flagForUpdate"); 

// Create an array to hold flagged product records 
allProducts_toUpdate = new Array( ); // Records marked for update 

// When the user selects a row, set the combo boxes to match the data 
allProducts_dg.setChangeHandler("setCombos"); 

// Get the Product list 
function getRecordset ( ) { 
  myService.getSearchResult(SearchResult, ''); 
}

getRecordset( ); 

// Set up event handlers for buttons 
insert_pb.setClickHandler("insertRecord"); 
update_pb.setClickHandler("updateRecords"); 
delete_pb.setClickHandler("deleteRecords"); 

// Event handlers for buttons 
// Update a batch of records stored in the allProducts_toUpdate array 
function updateRecords ( ) { 
  myService.updateProducts(new MainServiceResponder( ), allProducts_toUpdate); 
  getProductList( ); 
}

function insertRecord ( ) { 
  if (insert_pb.getLabel( ) == "Add New Product") { 
    allProducts_dg.addItem(getNewRecord( )); 
    allProducts_dg.setSelectedCell(allProducts_dg.getLength( )-1,"ProductName"); 
    insert_pb.setLabel("Insert To Database"); 
    insert_txt.text = "Click again to insert to database"; 
  } else { 
    insert_pb.setLabel("Add New Product"); 
    myService.addProduct(new MainServiceResponder( ), 
    allProducts_dg.getSelectedItem( )); 
    getRecordset( ); 
    insert_txt.text = ""; 
  } 
}

// Delete all selected records -- pass the ProductID numbers as a list 
function deleteRecords ( ) { 
  var deletedIndices = allProducts_dg.getSelectedIndices( ); 
  var deletedItems = new Array( ); 
  for (var i=0; i < deletedIndices.length; i++) { 
    deletedItems.push(allProducts_dg.getItemAt(deletedIndices[i]).ProductID); 
    allProducts_dg.removeItemAt(deletedIndices[i]); 
  } 
  myService.deleteProducts(new MainServiceResponder( ), deletedItems.join( )); 
}

// Get a blank record 
function getNewRecord ( ) { 
  var theRecord = { ProductID:'' 
    ,ProductName:'' 
    ,UnitPrice:'' 
    ,QuantityPerUnit:'' 
    ,CategoryID:'' 
    ,SupplierID:'' 
  }; 
  return theRecord; 
}

function flagForUpdate (grid_dg) { 
  // This row has been modified; save it for update 
  allProducts_toUpdate.push(grid_dg.getSelectedItem( )); 
}

function setCombos ( ) { 
  categories_cb.pickValue(allProducts_dg.getSelectedItem( ).CategoryID); 
  suppliers_cb.pickValue(allProducts_dg.getSelectedItem( ).SupplierID); 
}

// Utility function to set the current CategoryID to the value in the combo 
function setCategory (combo) { 
  allProducts_dg.setCellData(allProducts_dg.getSelectedIndex( ), 
  "CategoryID", combo.getValue( )); 
}

// Utility function to set the current SupplierID to the value in combo 
function setSupplier (combo) { 
  allProducts_dg.setCellData(allProducts_dg.getSelectedIndex( ), "SupplierID", combo.getValue( )); 
}

Download code text

Download chapter example files