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