AppML - 模型
AppML 模型描述了一個應用程式。它是一種基於 JSON 的格式,我們可以定義以下功能。
語法
<table appml-data="local?model=model_students"></div>
此處的 model_students.js 是 AppML 資料模型檔案。
model_students.js
{
"rowsperpage" : 10,
"database" : {
"connection" : "localsql",
"sql" : "SELECT studentName, class, section FROM Students",
"orderby" : "StudentName"
},
"filteritems" : [
{"item" : "studentName", "label" : "Student"},
{"item" : "class"},
{"item" : "section"}
],
"sortitems" : [
{"item" : "studentName", "label" : "Student"},
{"item" : "class"},
{"item" : "section"}
]
}
以下是 AppML 模型的常見用法。
定義到 MySQL、SQL Server、MS Access 和 Oracle 的資料庫連線。
定義連線以訪問檔案,如 JSON、XML、基於 csv 的文字檔案。
定義用於 CRUD(建立、讀取、更新、刪除)操作的 SQL 語句。
在表上應用篩選器/排序限制。
定義資料型別、資料格式以及對它們的限制。
對應用程式使用者應用安全性。定義使用者組。
示例應用程式
以下示例演示如何使用 AppML 模型從基於 csv 的文字檔案中獲取資料。
步驟 1:建立資料
第一步是建立一個基於 csv 的資料檔案,該檔案將提供要在應用程式 UI 中顯示的記錄。建立一個 students-records.txt
students_records.txt
Ramesh,12,White, Suresh,12,Red, Mohan,12,Red, Robert,12,Red, Julie,12,White, Ali,12,White, Harjeet,12,White,
步驟 2:建立模型
建立 student_model.js 檔案,它將充當描述記錄的模型。
{
"rowsperpage" : 7,
"data" : {
"type" : "csvfile",
"filename" : "students_records.txt",
"items" : [
{"name" : "studentName", "index" : 1},
{"name" : "class", "index" : 2},
{"name" : "section", "index" : 3}
]
}
}
student_application.html
<!DOCTYPE html>
<html lang="en-US">
<title>Students</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even) {background-color: #f2f2f2;}
</style>
<script src="https://w3schools.tw/appml/2.0.3/appml.js"></script>
<body>
<table appml-data="appml.php?model=students_model">
<tr>
<th>Student</th>
<th>Class</th>
<th>Section</th>
</tr>
<tr appml-repeat="records">
<td>{{studentName}}</td>
<td>{{class}}</td>
<td>{{section}}</td>
</tr>
</table>
</body>
</html>
輸出
將應用程式部署到 Web 伺服器並訪問 html 頁面。驗證輸出。
廣告