แบบฟอร์ม 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" : "City"},
{"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">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 อธิบาย
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">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
<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>
เพิ่มคอลัมน์ที่คลิกได้ลงในตาราง
ในบทที่แล้ว คุณได้สร้างแอปพลิเคชันสำหรับแสดงรายการบันทึกจากฐานข้อมูล
ตอนนี้เพิ่มคอลัมน์ใหม่ลงในตาราง:
ที่มา HTML
<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>
เหตุการณ์ onclick (ในคอลัมน์ใหม่) เรียกให้เรียกใช้แอปพลิเคชัน AppML ที่อยู่ในองค์ประกอบ HTML ที่มี id="Form01":
- 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>