ต้นแบบ AppML
ในบทนี้ เราจะสร้างต้นแบบสำหรับเว็บแอปพลิเคชัน
สร้าง HTML Prototype
ขั้นแรก สร้างต้นแบบ HTML ที่เหมาะสม โดยใช้ CSS ที่คุณชื่นชอบ
เราใช้ W3.CSS ในตัวอย่างนี้:
ตัวอย่าง
<!DOCTYPE html>
<html lang="en-US">
<title>Customers</title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<div class="w3-container">
<h1>Customers</h1>
<table class="w3-table-all">
<tr>
<th>Customer</th>
<th>City</th>
<th>Country</th>
</tr>
<tr>
<td>{{CustomerName}}</td>
<td>{{City}}</td>
<td>{{Country}}</td>
</tr>
</table>
</div>
</body>
</html>
{{ ... }} เป็นตัวยึดตำแหน่งสำหรับข้อมูลในอนาคต
เพิ่ม AppML
หลังจากที่คุณสร้างต้นแบบ HTML แล้ว คุณสามารถเพิ่ม AppML:
ตัวอย่าง
<!DOCTYPE html>
<html lang="en-US">
<title>Customers</title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script src="https://www.w3schools.com/appml/2.0.3/appml.js"></script>
<script src="https://www.w3schools.com/appml/2.0.3/appml_sql.js"></script>
<body>
<div class="w3-container" appml-data="customers.js">
<h1>Customers</h1>
<table class="w3-table-all">
<tr>
<th>Customer</th>
<th>City</th>
<th>Country</th>
</tr>
<tr appml-repeat="records">
<td>{{CustomerName}}</td>
<td>{{City}}</td>
<td>{{Country}}</td>
</tr>
</table>
</div>
</body>
</html>
เพิ่ม AppML:
<script src="https://www.w3schools.com/appml/2.0.3/appml.js">
เพิ่มฐานข้อมูล WebSQL ในเครื่อง:
<script src="https://www.w3schools.com/appml/2.0.3/appml_sql.js">
กำหนดแหล่งข้อมูล:
appml-data="customers.js"
กำหนดองค์ประกอบ HTML ที่จะทำซ้ำสำหรับแต่ละระเบียนในระเบียน:
appml_repeat="บันทึก"
เพื่อให้ง่าย ให้เริ่มต้นด้วยข้อมูลในเครื่อง เช่นก่อนเชื่อมต่อกับฐานข้อมูล
สร้างแบบจำลอง AppML
เพื่อให้สามารถใช้ฐานข้อมูลได้ คุณจะต้องมีโมเดลฐานข้อมูล AppML:
proto_customers.js
{
"rowsperpage" : 10,
"database" : {
"connection"
: "localmysql",
"sql" : "Select * from Customers",
"orderby"
: "CustomerName",
}
หากคุณไม่มีฐานข้อมูลในเครื่อง คุณสามารถใช้โมเดล AppML เพื่อสร้างฐานข้อมูล Web SQL
ในการสร้างตารางด้วยระเบียนเดียว ให้ใช้แบบจำลองดังนี้:
การสร้างฐานข้อมูลท้องถิ่นไม่ทำงานใน IE หรือ Firefox ใช้ Chrome หรือ Safari
ใช้โมเดลในแอปพลิเคชันของคุณ เปลี่ยนแหล่งข้อมูลเป็นlocal?model=proto_customers_single :
ตัวอย่าง
<div class="w3-container" appml-data="local?model=proto_customers_single">
<h1>Customers</h1>
<table class="w3-table-all">
<tr>
<th>Customer</th>
<th>City</th>
<th>Country</th>
</tr>
<tr appml-repeat="records">
<td>{{CustomerName}}</td>
<td>{{City}}</td>
<td>{{Country}}</td>
</tr>
</table>
</div>
สร้างฐานข้อมูลท้องถิ่นที่มีหลายระเบียน
ในการสร้างตารางที่มีหลายระเบียน ให้ใช้แบบจำลองดังนี้:
เปลี่ยนแหล่งข้อมูลเป็นlocal?model=proto_customers_all
ตัวอย่าง
<div class="w3-container" appml-data="local?model=proto_customers_all">
<h1>Customers</h1>
<table class="w3-table-all">
<tr>
<th>Customer</th>
<th>City</th>
<th>Country</th>
</tr>
<tr appml-repeat="records">
<td>{{CustomerName}}</td>
<td>{{City}}</td>
<td>{{Country}}</td>
</tr>
</table>
</div>
เพิ่มเทมเพลตการนำทาง
สมมติว่าคุณต้องการให้แอปพลิเคชันทั้งหมดของคุณมีแถบเครื่องมือการนำทางทั่วไป:
สร้างเทมเพลต HTML สำหรับมัน:
inc_listcommands.htm
<div class="w3-bar w3-border w3-section">
<button class="w3-button" id='appmlbtn_first'>❮❮</button>
<button class="w3-button" id='appmlbtn_previous'>❮</button>
<button class="w3-button w3-hover-none" id='appmlbtn_text'></button>
<button class="w3-button" id='appmlbtn_next'>❯</button>
<button class="w3-button" id='appmlbtn_last'>❯❯</button>
<button class="w3-btn ws-green" id='appmlbtn_query'>Filter</button>
</div>
<div id="appmlmessage"></div>
บันทึกเทมเพลตในไฟล์ที่มีชื่อที่ถูกต้อง เช่น "inc_listcommands.htm"
รวมเทมเพลตในต้นแบบของคุณด้วยแอตทริบิวต์appml-include-html :
ตัวอย่าง
<div class="w3-container" appml-data="local?model=proto_customers_all">
<h1>Customers</h1>
<div appml-include-html="inc_listcommands.htm"></div>
<table class="w3-table-all">
<tr>
<th>Customer</th>
<th>City</th>
<th>Country</th>
</tr>
<tr appml-repeat="records">
<td>{{CustomerName}}</td>
<td>{{City}}</td>
<td>{{Country}}</td>
</tr>
</table>
</div>