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フォームコマンドを作成する

お気に入りのスタイルシート(ブートストラップを使用)を使用して、必要なフォームコマンドを作成します。

inc_formcommands.htm

<span onclick="document.getElementById('Form01').style.display='none'" class="w3-button w3-xlarge w3-right">&times;</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">&times;</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}})">&#9998;</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>