This chapter demonstrates how to construct an input form against a database.
The examples on this page use a local SQL database.
Local SQL databases does not work in IE or Firefox. Use Chrome or Safari.
Create a Form Model
model_customersform.js
{
"database" : {
"connection" : "localmysql",
"maintable" : "Customers",
"keyfield" : "CustomerID",
"sql" : "SELECT * FROM Customers"},
"updateItems" : [
{"item" : "CustomerName"},
{"item" : "Address"},
{"item" : "PostalCode"},
{"item" : "City"},
{"item" : "Country"}]
}
Create an HTML Form
In the previous chapter, you created an application for listing records from a database.
Now add a form application to the page:
HTML Form
<div id="Form01" appml-data="local?model=model_customersform"
class="jumbotron">
<div class="form-group">
<label for="customername">Customer:</label>
<input id="customername" class="form-control">
</div>
<div class="form-group">
<label
for="address">Address:</label>
<input id="address" class="form-control">
</div>
<div class="form-group">
<label for="city">City:</label>
<input id="city" class="form-control">
</div>
<div
class="form-group">
<label for="postalcode">Postal Code:</label>
<input id="postalcode"
class="form-control">
</div>
<div class="form-group">
<label
for="country">Country:</label>
<input id="country" class="form-control">
</div>
</div>
Try It Yourself »
HTML Form Explained
appml-data="local?model=model_customersform" defines the AppML application for the form.
Create HTML Form Commands
Use your favorite style sheet (we use bootstrap), and create your wanted form commands:
inc_formcommands.htm
<button type="button" class="close"
onclick="document.getElementById('Form01').style.display='none';">X</button>
<button type="button" class="close">X</button>
<div class="btn-toolbar" style="margin-bottom:20px;">
<div class="btn-group">
<button type="button" class="btn btn-default" onclick="appml('Form01').newRecord();">
<span class="glyphicon glyphicon-new-window"></span> New</button>
<button type="button" class="btn btn-primary" onclick="appml('Form01').saveRecord();">
<span class="glyphicon glyphicon-floppy-disk"></span> Save</button>
<button type="button" class="btn btn-default" onclick="appml('Form01').deleteRecord();">
<span class="glyphicon glyphicon-trash"></span> Delete</button>
</div>
</div>
<div id="appmlmessage" class="alert alert-warning" style="display:none;">
<button type="button" class="close"
onclick="this.parentNode.style.display='none';">X</button>
<div id="message"></div>
</div>
Include the Form Commands
Include the form commands in in your form:.
HTML Form
<div id="Form01" appml-data="local?model=model_customersform"
class="jumbotron">
<div appml-include-html="inc_formcommands.htm"></div>
<div class="form-group">
<label for="customername">Customer:</label>
<input id="customername" class="form-control">
</div>
<label
for="address">Address:</label>
<input id="address" class="form-control">
</div>
<div class="form-group">
<label for="city">City:</label>
<input id="city" class="form-control">
</div>
<div
class="form-group">
<label for="postalcode">Postal Code:</label>
<input id="postalcode"
class="form-control">
</div>
<div class="form-group">
<label
for="country">Country:</label>
<input id="country" class="form-control">
</div>
</div>
Try It Yourself »
Add a Clickable Column to the Table
In the previous chapter, you created an application for listing records from a database.
Now add a new column to the table:
HTML Source
<div appml-data="local?model=model_customerslist">
<h1>Customers</h1>
<div appml-include-html="inc_listcommands.htm"></div>
<div appml-include-html="inc_filter.htm"></div>
<table class="table table-striped table-bordered">
<tr>
<th></th>
<th>Customer</th>
<th>City</th>
<th>Country</th>
</tr>
<tr appml-repeat="records">
<td
style="cursor:pointer;width:34px;"
onclick="appml('Form01').run({{CustomerID}})">
<span class="glyphicon glyphicon-edit"></span></td>
<td>{{CustomerName}}</td>
<td>{{City}}</td>
<td>{{Country}}</td>
</tr>
</table>
</div>
Try It Yourself »
The onclick event (in the new column) triggers a call to run an AppML application located in the HTML element with id="Form01":
- appml('Form01') returns the AppML application
- run({{CustomerID}}) runs the applications with CustomerID as parameter.
Finally, Hide the Form
Add a style to the form to make it invisible:
HTML
<div id="Form01" appml-data="local?model=model_customersform"
appml-controller="myFormController"
class="jumbotron" style="display:none">
Add a controller to the form, to display the form only when it is loaded and ready to display data:
Controller
<script>
function myFormController($appml) {
if ($appml.message
== "ready") {return -1;}
if ($appml.message ==
"loaded") {
document.getElementById("Form01").style.display="";
}
}
</script>
Try It Yourself »