AppML 表單
本章演示如何針對資料庫構建輸入表單。
此頁面上的示例使用本地 SQL 資料庫。
本地 SQL 資料庫在 IE 或 Firefox 中無法正常工作。請使用 Chrome 或 Safari。
建立表單模型
model_customersform.js
{
"database" : {
"connection" : "localmysql",
"maintable" : "Customers",
"keyfield" : "CustomerID",
"sql" : "SELECT * FROM Customers"},
"updateItems" : [
{"item" : "CustomerName"},
{"item" : "Address"},
{"item" : "PostalCode"},
{"item" : "城市"},
{"item" : "Country"}]
}
建立 HTML 表單
在上一章中,您建立了一個用於列出資料庫記錄的應用程式。
現在,將表單應用程式新增到頁面
HTML 表單
<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">客戶:</label>
<input id="customername" class="w3-input w3-border">
</p>
<p>
<label for="address">地址:</label>
<input id="address" class="w3-input w3-border">
</p>
<p>
<label for="city">城市:</label>
<input id="city" class="w3-input w3-border">
</p>
<p>
<label for="postalcode">郵政編碼:</label>
<input id="postalcode" class="w3-input w3-border">
</p>
<p>
<label for="country">國家:</label>
<input id="country" class="w3-input w3-border">
</p>
</div>
自己試試 »
HTML 表單說明
appml-data="local?model=model_customersform" 定義了表單的 AppML 應用程式。
建立 HTML 表單命令
使用您喜歡的樣式表(我們使用 bootstrap),並建立您想要的表單命令
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">新建</button>
<button onclick="appml('Form01').saveRecord();" class="w3-btn w3-green">儲存</button>
<button onclick="appml('Form01').deleteRecord();" class="w3-btn">刪除</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>
包含表單命令
在您的表單中包含表單命令
HTML 表單
<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">客戶:</label>
<input id="customername" class="w3-input w3-border">
</p>
<p>
<label for="address">地址:</label>
<input id="address" class="w3-input w3-border">
</p>
<p>
<label for="city">城市:</label>
<input id="city" class="w3-input w3-border">
</p>
<p>
<label for="postalcode">郵政編碼:</label>
<input id="postalcode" class="w3-input w3-border">
</p>
<p>
<label for="country">國家:</label>
<input id="country" class="w3-input w3-border">
</p>
</div>
自己試試 »
為表格新增可點選的列
在上一章中,您建立了一個用於列出資料庫記錄的應用程式。
現在為表格新增新的一列
HTML 原始碼
<div appml-data="local?model=model_customerslist">
<h1>客戶</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>客戶</th>
<th>城市</th>
<th>國家</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>
自己試試 »
onclick 事件(在新列中)會觸發一個呼叫,該呼叫執行位於 ID 為 "Form01" 的 HTML 元素中的 AppML 應用程式
- appml('Form01') 返回 AppML 應用程式
- run({{CustomerID}}) 以 CustomerID 作為引數執行應用程式。
最後,隱藏表單
為表單新增樣式,使其不可見
HTML
<div id="Form01" appml-data="local?model=model_customersform"
appml-controller="myFormController"
class="jumbotron" style="display:none">
為表單新增控制器,以便在表單載入並準備顯示資料時才顯示它
控制器
<script>
function myFormController($appml) {
if ($appml.message == "ready") {return -1;}
if ($appml.message == "loaded") {
document.getElementById("Form01").style.display="";
}
}
</script>
自己試試 »