AppML 原型

在本章中,我们将为 Web 应用程序构建一个原型。


创建 HTML 原型

首先,使用您最喜欢的 CSS 创建一个体面的 HTML 原型

我们在这个例子中使用了 W3.CSS:

实例

<!DOCTYPE html>
<html lang="en-US">

<title>Customers</title>
<link rel="stylesheet" href="https://www.w3schools.cn/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.cn/w3css/4/w3.css">
<script src="https://www.w3schools.cn/appml/2.0.3/appml.js"></script>
<script src="https://www.w3schools.cn/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.cn/appml/2.0.3/appml.js">

添加本地 WebSQL 数据库:

<script src="https://www.w3schools.cn/appml/2.0.3/appml_sql.js">

定义数据源:

appml-data="customers.js"

为记录中的每条记录定义要重复的 HTML 元素:

appml_repeat="records"

为简单起见,在连接到 数据库。


创建 AppML 模型

为了能够使用数据库,您需要一个 AppML 数据库模型:

proto_customers.js

{
"rowsperpage" : 10,
"database" : {
"connection" : "localmysql",
"sql" : "Select * from Customers",
"orderby" : "CustomerName",
}

如果您没有本地数据库,可以使用 AppML 模型创建 Web SQL 数据库。

要创建包含单个记录的表,请使用如下模型: proto_customers_single.js

在 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>
亲自试一试 »

创建具有多条记录的本地数据库

要创建包含多条记录的表,请使用如下模型: proto_customers_all.js

将数据源更改为 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'>&#10094;&#10094;</button>
<button class="w3-button" id='appmlbtn_previous'>&#10094;</button>
<button class="w3-button w3-hover-none" id='appmlbtn_text'></button>
<button class="w3-button" id='appmlbtn_next'>&#10095;</button>
<button class="w3-button" id='appmlbtn_last'>&#10095;&#10095;</button>
<button class="w3-button w3-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>
亲自试一试 »