Lesson 5: Add data manipulation and navigation

This lesson describes how to fill out your application with data manipulation and navigation logic.

Initialize the table

  1. Initialize the CustomerTable representing the Customer table in your database by adding the following code to the end of the Connect function in tutorial.js:

    try {
      CustomerTable = Connection.getTable( "customer", null );
      CustomerTable.open();
    } catch( ex3 ) {
      alert("Error: " + ex3.getMessage() );
    }
  2. Add variables to move data between the database and the web form.

    Add the following variable declarations to the top of tutorial.js for the customer data:

    var GivenName = "";
    var Surname = "";
    var Street = "";
    var City = "";
    var Phone = "";
    var ID = "";
    
  3. Create functions to fetch and display customer data.

    Add the following function to tutorial.js. This function fetches the current row of the customer data and also ensures that NULL columns are displayed as empty strings:

    function Fetch() 
    {
        if( Table.getRowCount() == 0 ) {
    	GivenName = "";
    	Surname = "";
    	Street = "";
    	City = "";
    	Phone = "";
    	ID = "";
    	return;
        } 
        ID = Table.getString( Table.schema.getColumnID( "ID" ) );
        GivenName = Table.getString( Table.schema.getColumnID( "GivenName" ) );
        Surname = Table.getString( Table.schema.getColumnID( "Surname" ) );
        Street = Table.getString( Table.schema.getColumnID( "Street" ) );
        if( Table.isNull( Table.schema.getColumnID( "City" ) ) ) {
    	City = "";
        } else {
    	City = Table.getString( Table.schema.getColumnID( "City" ) );
        }
        if( Table.isNull( Table.schema.getColumnID( "Phone" ) ) ) {
    	Phone = "";
        } else {
    	Phone = Table.getString( Table.schema.getColumnID( "Phone" ) );
        }
    }
    

    Add the following to main.htm immediately after the <script> tag. DisplayRow takes the values from the database and displays them in the web form. FetchForm takes the current values in the web form and makes them available to the database code.

    <script>
    function DisplayRow() {
        Fetch();
        document.form.ID.value = ID;
        document.form.GivenName.value = GivenName;
        document.form.Surname.value = Surname;
        document.form.Street.value = Street;
        document.form.City.value = City;
        document.form.Phone.value = Phone;
    }
    
    function FetchForm() {
        GivenName = document.form.GivenName.value;
        Surname = document.form.Surname.value;
        Street = document.form.Street.value;
        City = document.form.City.value;
        Phone = document.form.Phone.value;
    }
    </script>
  4. Call DisplayCurrentRow to display the current row when the application is loaded. Modify the body tag at the top of main.htm as follows:

    <body onload="Connect(); DisplayCurrentRow();">

Although there is no data in the tutorial database up to this point, this is a good place to synchronize the channel to ensure that the application is running properly.

Add code to insert rows

  • Create functions to insert customer data.

    In the following procedure, the call to InsertBegin puts the application into insert mode and sets all values in the current row to their defaults. For example, the ID column receives the next autoincrement value. The column values are set and the new row is inserted.

    Add the following function to tutorial.js:

    function Insert()
    {
        try {
    	Table.insertBegin();
    	Table.setString( Table.schema.getColumnID( "GivenName" ), GivenName );
    	Table.setString( Table.schema.getColumnID( "Surname" ), Surname );
    	Table.setString( Table.schema.getColumnID( "Street" ), Street );
    	if( City.length > 0 ) {
    	    Table.setString( Table.schema.getColumnID( "City" ), City );
    	}
    	if( Phone.length > 0 ) {
    	    Table.setString( Table.schema.getColumnID( "Phone" ), Phone );
    	}
    	Table.insert();
    	Table.moveLast();
        } catch( ex ) {
    	alert( "Error: cannot insert row: " + ex.getMessage() );
        }
    }

    Add the following function to main.htm:

    function ClickInsert()
    {
        FetchForm();
        Insert();
        DisplayRow();
    }