AppML Forms
本章演示如何针对数据库创建输入表单。
本页上的示例使用本地 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" : "City"},{"item" : "Country"}]}
创建一个 HTML 表单
在上一章中,您创建了一个应用程序,用于列出数据库中的记录。
现在向页面添加一个表单应用程序:
HTML Form
<!DOCTYPE html><html><link rel="stylesheet" href="https://cankaoshouce.com/css/w3.css"><script src="https://cankaoshouce.com/js/appml/appml.js"></script><script src="/js/appml/appml_sql.js"></script><body><div class="w3-container"><div id="Form01" class="w3-container w3-light-grey w3-padding-large w3-margin-bottom" appml-data="local?model=/example/appml/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><div appml-data="local?model=/example/appml/model_customerslist"><h2>Customers</h2><div appml-include-html="/example/appml/inc_listcommands.html"></div><div appml-include-html="/example/appml/inc_filter.html"></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></div></body></html>
HTML 表单解释
appml-data="local?model=model_customersform" 定义表单的 AppML 应用程序。
创建 HTML 表单命令
使用你最喜欢的样式表(我们使用 bootstrap),创建你想要的表单命令:
inc_formcommands.html
<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>
包含表单命令
在您的表单中包含表单命令:
HTML 表单
<!DOCTYPE html><html><link rel="stylesheet" href="https://cankaoshouce.com/css/w3.css"><script src="https://cankaoshouce.com/js/appml/appml.js"></script><script src="/js/appml/appml_sql.js"></script><body><div class="w3-container"><div id="Form01" class="w3-container w3-light-grey w3-padding-large w3-margin-bottom" appml-data="local?model=/example/appml/model_customersform"><div appml-include-html="/example/appml/inc_formcommands.html"></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><div appml-data="local?model=/example/appml/model_customerslist"><h2>Customers</h2><div appml-include-html="/example/appml/inc_listcommands.html"></div><div appml-include-html="/example/appml/inc_filter.html"></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></div></body></html>
向表中添加一个可单击的列
在上一章中,您创建了一个应用程序,用于列出数据库中的记录。
现在向表中添加一个新列:
HTML 源码
<!DOCTYPE html><html><link rel="stylesheet" href="https://cankaoshouce.com/css/w3.css"><script src="https://cankaoshouce.com/js/appml/appml.js"></script><script src="/js/appml/appml_sql.js"></script><body><div class="w3-container"><div id="Form01" class="w3-container w3-light-grey w3-padding-large w3-margin-bottom" appml-data="local?model=/example/appml/model_customersform"><div appml-include-html="/example/appml/inc_formcommands.html"></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><div appml-data="local?model=/example/appml/model_customerslist"><h1>Customers</h1><div appml-include-html="/example/appml/inc_listcommands.html"></div><div appml-include-html="/example/appml/inc_filter.html"></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></div></body></html>
onclick 事件(在新列中)触发一个调用,以运行 id="Form01" 的 HTML 元素中的 AppML 应用程序:
appml('Form01') 返回 appml 应用程序
run({CustomerID}}) 以 CustomerID 作为参数运行应用程序。
最后隐藏表单
添加一个样式隐藏 form 表单:
HTML
<div id="Form01" appml-data="local?model=/example/appml/model_customersform"appml-controller="myFormController"class="jumbotron" style="display:none">
将控制器添加到表单,以便仅在加载表单并准备好显示数据时显示表单
控制器
<!DOCTYPE html><html><link rel="stylesheet" href="https://cankaoshouce.com/css/w3.css"><script src="https://cankaoshouce.com/js/appml/appml.js"></script><script src="/js/appml/appml_sql.js"></script><body><div class="w3-container"><div id="Form01" class="w3-container w3-light-grey w3-padding-large w3-margin-bottom" appml-data="local?model=/example/appml/model_customersform" appml-controller="myFormController" style="display:none;"><div appml-include-html="/example/appml/inc_formcommands.html"></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><div appml-data="local?model=/example/appml/model_customerslist"><h1>Customers</h1><div appml-include-html="/example/appml/inc_listcommands.html"></div><div appml-include-html="/example/appml/inc_filter.html"></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></div><script>function myFormController($appml) {if ($appml.message == "ready") {return -1;}if ($appml.message == "loaded") {document.getElementById("Form01").style.display="";}}</script></body></html>