AppML フォーム


この章では、データベースに対する入力フォームを作成する方法を示します。


Note このページの例は、ローカル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>
Try It Yourself »

HTML フォームの説明

appml-data="local?model=model_customersform" は、フォーム用の AppML アプリケーションを定義します。


HTML フォーム・コマンドの作成

好みのスタイルシートをを使用(ここでは 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>
Try It Yourself »

テーブルにクリック可能な列を追加

前の章では、データベースからレコードをリストするアプリケーションを作成しました。

ここで、テーブルに新しい列を追加します:

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>
Try It Yourself »

新しい列の onclick イベントは、id="Form01" を持った HTML 要素に配置された、 AppML アプリケーションを実行するための呼出しをトリガします:


最後に、フォームを隠します

フォームを非表示にするため、フォームにスタイルを追加します:

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>
Try It Yourself »