AppML-vorms
Hierdie hoofstuk demonstreer hoe om 'n invoervorm teen 'n databasis saam te stel.
Die voorbeelde op hierdie bladsy gebruik 'n plaaslike SQL-databasis.
Plaaslike SQL-databasisse werk nie in IE of Firefox nie. Gebruik Chrome of Safari.
Skep 'n vormmodel
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"}]
}
Skep 'n HTML-vorm
In die vorige hoofstuk het jy 'n toepassing geskep om rekords vanaf 'n databasis te lys.
Voeg nou 'n vormaansoek by die bladsy:
HTML-vorm
<div id="Form01" class="w3-container w3-light-grey w3-padding-large
w3-margin-bottom" appml-data="local?model=model_customersform">
<p>
<label for="customername">Customer:</label>
<input id="customername"
class="w3-input w3-border">
</p>
<p>
<label
for="address">Address:</label>
<input id="address" class="w3-input
w3-border">
</p>
<p>
<label for="city">City:</label>
<input
id="city" class="w3-input w3-border">
</p>
<p>
<label
for="postalcode">Postal Code:</label>
<input id="postalcode"
class="w3-input w3-border">
</p>
<p>
<label for="country">Country:</label>
<input id="country"
class="w3-input w3-border">
</p>
</div>
HTML-vorm verduidelik
appml-data="local?model=model_customersform" definieer die AppML-toepassing vir die vorm.
Skep HTML-vormopdragte
Gebruik jou gunsteling stylblad (ons gebruik bootstrap), en skep jou gewenste vormopdragte:
inc_formcommands.htm
<span onclick="document.getElementById('Form01').style.display='none'"
class="w3-button w3-xlarge w3-right">×</span>
<div
class="w3-bar w3-border w3-white">
<button
onclick="appml('Form01').newRecord();" class="w3-btn">New</button>
<button onclick="appml('Form01').saveRecord();" class="w3-btn
w3-green">Save</button>
<button onclick="appml('Form01').deleteRecord();"
class="w3-btn">Delete</button>
</div>
<div id="appmlmessage"
class="w3-container w3-pale-yellow w3-padding" style="display:none;">
<span onclick="this.parentNode.style.display='none';" class="w3-button
w3-xlarge w3-right">×</span>
<div id="message"></div>
</div>
Sluit die vormbevele in
Sluit die vormopdragte in by jou vorm in:
HTML-vorm
<div id="Form01" class="w3-container w3-light-grey w3-padding-large
w3-margin-bottom" appml-data="local?model=model_customersform">
<div appml-include-html="inc_formcommands.htm"></div>
<p>
<label for="customername">Customer:</label>
<input
id="customername" class="w3-input w3-border">
</p>
<p>
<label
for="address">Address:</label>
<input id="address" class="w3-input
w3-border">
</p>
<p>
<label for="city">City:</label>
<input
id="city" class="w3-input w3-border">
</p>
<p>
<label
for="postalcode">Postal Code:</label>
<input id="postalcode"
class="w3-input w3-border">
</p>
<p>
<label for="country">Country:</label>
<input id="country"
class="w3-input w3-border">
</p>
</div>
Voeg 'n klikbare kolom by die tabel
In die vorige hoofstuk het jy 'n toepassing geskep om rekords vanaf 'n databasis te lys.
Voeg nou 'n nuwe kolom by die tabel:
HTML Bron
<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="w3-table-all">
<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}})">✎</td>
<td>{{CustomerName}}</td>
<td>{{City}}</td>
<td>{{Country}}</td>
</tr>
</table>
</div>
Die onclick-gebeurtenis (in die nuwe kolom) veroorsaak 'n oproep om 'n AppML-toepassing te laat loop wat in die HTML-element geleë is met id="Form01":
- appml('Form01') gee die AppML-toepassing terug
- run({{CustomerID}}) loop die toepassings met CustomerID as parameter.
Ten slotte, versteek die vorm
Voeg 'n styl by die vorm om dit onsigbaar te maak:
HTML
<div id="Form01" appml-data="local?model=model_customersform"
appml-controller="myFormController"
class="jumbotron" style="display:none">
Voeg 'n kontroleerder by die vorm, om die vorm slegs te vertoon wanneer dit gelaai is en gereed is om data te vertoon:
Beheerder
<script>
function myFormController($appml) {
if ($appml.message ==
"ready") {return -1;}
if ($appml.message == "loaded") {
document.getElementById("Form01").style.display="";
}
}
</script>