ล่าสุดการพัฒนาเว็บบทเรียน
 

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" appml-data="local?model=model_customersform"
class="jumbotron">

<div class="form-group">
  <label for="customername">Customer:</label>
  <input id="customername" class="form-control">
</div>

<div class="form-group">
  <label for="address">Address:</label>
  <input id="address" class="form-control">
</div>

<div class="form-group">
  <label for="city">City:</label>
  <input id="city" class="form-control">
</div>

<div class="form-group">
  <label for="postalcode">Postal Code:</label>
  <input id="postalcode" class="form-control">
</div>

<div class="form-group">
  <label for="country">Country:</label>
  <input id="country" class="form-control">
</div>

</div>
ลองตัวเอง»

HTML แบบฟอร์มการอธิบาย

appml ข้อมูล = "ท้องถิ่นรูปแบบ = model_customersform" กำหนดแอปพลิเค AppML สำหรับแบบฟอร์ม


สร้างคำสั่งฟอร์ม HTML

ใช้แผ่นสไตล์ที่คุณชื่นชอบ (we use bootstrap) และสร้างรูปแบบคำสั่งที่ต้องการของคุณ:

inc_formcommands.htm

<button type="button" class="close" onclick="document.getElementById('Form01').style.display='none';">X</button>

<button type="button" class="close">X</button>

<div class="btn-toolbar" style="margin-bottom:20px;">
<div class="btn-group">

<button type="button" class="btn btn-default" onclick="appml('Form01').newRecord();">
<span class="glyphicon glyphicon-new-window"></span> New</button>

<button type="button" class="btn btn-primary" onclick="appml('Form01').saveRecord();">
<span class="glyphicon glyphicon-floppy-disk"></span> Save</button>

<button type="button" class="btn btn-default" onclick="appml('Form01').deleteRecord();">
<span class="glyphicon glyphicon-trash"></span> Delete</button>

</div>
</div>

<div id="appmlmessage" class="alert alert-warning" style="display:none;">
<button type="button" class="close"
onclick="this.parentNode.style.display='none';">X</button>
<div id="message"></div>

</div>

รวมถึงคำสั่งแบบฟอร์ม

รวมถึงคำสั่งรูปแบบในรูปแบบของคุณ :.

ฟอร์ม HTML

<div id="Form01" appml-data="local?model=model_customersform"
class="jumbotron">

<div appml-include-html="inc_formcommands.htm"></div>

<div class="form-group">
<label for="customername">Customer:</label>
<input id="customername" class="form-control">
</div>

<label for="address">Address:</label>
<input id="address" class="form-control">
</div>

<div class="form-group">
<label for="city">City:</label>
<input id="city" class="form-control">
</div>

<div class="form-group">
<label for="postalcode">Postal Code:</label>
<input id="postalcode" class="form-control">
</div>

<div class="form-group">
<label for="country">Country:</label>
<input id="country" class="form-control">
</div>

</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="table table-striped table-bordered">
  <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}})">
        <span class="glyphicon glyphicon-edit"></span></td>

    <td>{{CustomerName}}</td>
    <td>{{City}}</td>
    <td>{{Country}}</td>
  </tr>
</table>

</div>
ลองตัวเอง»

เหตุการณ์ onclick (in the new column) ก่อให้เกิดการเรียกร้องให้เรียกใช้โปรแกรมประยุกต์ AppML อยู่ในองค์ประกอบ HTML มี id = "Form01" A:

  • appml('Form01') ผลตอบแทนการประยุกต์ใช้ AppML
  • run({{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>
ลองตัวเอง»