AppML Forms

本章演示如何针对数据库创建输入表单。

本页上的示例使用本地 SQL 数据库。

本地 SQL 数据库在 IE 或 Firefox 中不起作用。请使用 Chrome 或 Safari。

创建一个表单模型

model_customersform.js
  1. {
  2. "database" : {
  3. "connection" : "localmysql",
  4. "maintable" : "Customers",
  5. "keyfield" : "CustomerID",
  6. "sql" : "SELECT * FROM Customers"},
  7. "updateItems" : [
  8. {"item" : "CustomerName"},
  9. {"item" : "Address"},
  10. {"item" : "PostalCode"},
  11. {"item" : "City"},
  12. {"item" : "Country"}]
  13. }

创建一个 HTML 表单

在上一章中,您创建了一个应用程序,用于列出数据库中的记录。

现在向页面添加一个表单应用程序:

HTML Form
  1. <!DOCTYPE html>
  2. <html>
  3. <link rel="stylesheet" href="https://cankaoshouce.com/css/w3.css">
  4. <script src="https://cankaoshouce.com/js/appml/appml.js"></script>
  5. <script src="/js/appml/appml_sql.js"></script>
  6. <body>
  7. <div class="w3-container">
  8. <div id="Form01" class="w3-container w3-light-grey w3-padding-large w3-margin-bottom" appml-data="local?model=/example/appml/model_customersform">
  9. <p>
  10. <label for="customername">Customer:</label>
  11. <input id="customername" class="w3-input w3-border">
  12. </p>
  13. <p>
  14. <label for="address">Address:</label>
  15. <input id="address" class="w3-input w3-border">
  16. </p>
  17. <p>
  18. <label for="city">City:</label>
  19. <input id="city" class="w3-input w3-border">
  20. </p>
  21. <p>
  22. <label for="postalcode">Postal Code:</label>
  23. <input id="postalcode" class="w3-input w3-border">
  24. </p>
  25. <p>
  26. <label for="country">Country:</label>
  27. <input id="country" class="w3-input w3-border">
  28. </p>
  29. </div>
  30. <div appml-data="local?model=/example/appml/model_customerslist">
  31. <h2>Customers</h2>
  32. <div appml-include-html="/example/appml/inc_listcommands.html"></div>
  33. <div appml-include-html="/example/appml/inc_filter.html"></div>
  34. <table class="w3-table-all">
  35. <tr>
  36. <th></th>
  37. <th>Customer</th>
  38. <th>City</th>
  39. <th>Country</th>
  40. </tr>
  41. <tr appml-repeat="records">
  42. <td style="cursor:pointer;width:34px;"
  43. onclick="appml('Form01').run({{CustomerID}})">&#9998;</td>
  44. <td>{{CustomerName}}</td>
  45. <td>{{City}}</td>
  46. <td>{{Country}}</td>
  47. </tr>
  48. </table>
  49. </div>
  50. </div>
  51. </body>
  52. </html>

HTML 表单解释

appml-data="local?model=model_customersform" 定义表单的 AppML 应用程序。


创建 HTML 表单命令

使用你最喜欢的样式表(我们使用 bootstrap),创建你想要的表单命令:

inc_formcommands.html
  1. <span onclick="document.getElementById('Form01').style.display='none'" class="w3-button w3-xlarge w3-right">&times;</span>
  2. <div class="w3-bar w3-border w3-white">
  3. <button onclick="appml('Form01').newRecord();" class="w3-btn">New</button>
  4. <button onclick="appml('Form01').saveRecord();" class="w3-btn w3-green">Save</button>
  5. <button onclick="appml('Form01').deleteRecord();" class="w3-btn">Delete</button>
  6. </div>
  7. <div id="appmlmessage" class="w3-container w3-pale-yellow w3-padding" style="display:none;">
  8. <span onclick="this.parentNode.style.display='none';" class="w3-button w3-xlarge w3-right">&times;</span>
  9. <div id="message"></div>
  10. </div>

包含表单命令

在您的表单中包含表单命令:

HTML 表单
  1. <!DOCTYPE html>
  2. <html>
  3. <link rel="stylesheet" href="https://cankaoshouce.com/css/w3.css">
  4. <script src="https://cankaoshouce.com/js/appml/appml.js"></script>
  5. <script src="/js/appml/appml_sql.js"></script>
  6. <body>
  7. <div class="w3-container">
  8. <div id="Form01" class="w3-container w3-light-grey w3-padding-large w3-margin-bottom" appml-data="local?model=/example/appml/model_customersform">
  9. <div appml-include-html="/example/appml/inc_formcommands.html"></div>
  10. <p>
  11. <label for="customername">Customer:</label>
  12. <input id="customername" class="w3-input w3-border">
  13. </p>
  14. <p>
  15. <label for="address">Address:</label>
  16. <input id="address" class="w3-input w3-border">
  17. </p>
  18. <p>
  19. <label for="city">City:</label>
  20. <input id="city" class="w3-input w3-border">
  21. </p>
  22. <p>
  23. <label for="postalcode">Postal Code:</label>
  24. <input id="postalcode" class="w3-input w3-border">
  25. </p>
  26. <p>
  27. <label for="country">Country:</label>
  28. <input id="country" class="w3-input w3-border">
  29. </p>
  30. </div>
  31. <div appml-data="local?model=/example/appml/model_customerslist">
  32. <h2>Customers</h2>
  33. <div appml-include-html="/example/appml/inc_listcommands.html"></div>
  34. <div appml-include-html="/example/appml/inc_filter.html"></div>
  35. <table class="w3-table-all">
  36. <tr>
  37. <th></th>
  38. <th>Customer</th>
  39. <th>City</th>
  40. <th>Country</th>
  41. </tr>
  42. <tr appml-repeat="records">
  43. <td style="cursor:pointer;width:34px;"
  44. onclick="appml('Form01').run({{CustomerID}})">&#9998;</td>
  45. <td>{{CustomerName}}</td>
  46. <td>{{City}}</td>
  47. <td>{{Country}}</td>
  48. </tr>
  49. </table>
  50. </div>
  51. </div>
  52. </body>
  53. </html>

向表中添加一个可单击的列

在上一章中,您创建了一个应用程序,用于列出数据库中的记录。

现在向表中添加一个新列:

HTML 源码
  1. <!DOCTYPE html>
  2. <html>
  3. <link rel="stylesheet" href="https://cankaoshouce.com/css/w3.css">
  4. <script src="https://cankaoshouce.com/js/appml/appml.js"></script>
  5. <script src="/js/appml/appml_sql.js"></script>
  6. <body>
  7. <div class="w3-container">
  8. <div id="Form01" class="w3-container w3-light-grey w3-padding-large w3-margin-bottom" appml-data="local?model=/example/appml/model_customersform">
  9. <div appml-include-html="/example/appml/inc_formcommands.html"></div>
  10. <p>
  11. <label for="customername">Customer:</label>
  12. <input id="customername" class="w3-input w3-border">
  13. </p>
  14. <p>
  15. <label for="address">Address:</label>
  16. <input id="address" class="w3-input w3-border">
  17. </p>
  18. <p>
  19. <label for="city">City:</label>
  20. <input id="city" class="w3-input w3-border">
  21. </p>
  22. <p>
  23. <label for="postalcode">Postal Code:</label>
  24. <input id="postalcode" class="w3-input w3-border">
  25. </p>
  26. <p>
  27. <label for="country">Country:</label>
  28. <input id="country" class="w3-input w3-border">
  29. </p>
  30. </div>
  31. <div appml-data="local?model=/example/appml/model_customerslist">
  32. <h1>Customers</h1>
  33. <div appml-include-html="/example/appml/inc_listcommands.html"></div>
  34. <div appml-include-html="/example/appml/inc_filter.html"></div>
  35. <table class="w3-table-all">
  36. <tr>
  37. <th></th>
  38. <th>Customer</th>
  39. <th>City</th>
  40. <th>Country</th>
  41. </tr>
  42. <tr appml-repeat="records">
  43. <td style="cursor:pointer;width:34px;"
  44. onclick="appml('Form01').run({{CustomerID}})">&#9998;</td>
  45. <td>{{CustomerName}}</td>
  46. <td>{{City}}</td>
  47. <td>{{Country}}</td>
  48. </tr>
  49. </table>
  50. </div>
  51. </div>
  52. </body>
  53. </html>

onclick 事件(在新列中)触发一个调用,以运行 id="Form01" 的 HTML 元素中的 AppML 应用程序:

appml('Form01') 返回 appml 应用程序

run({CustomerID}}) 以 CustomerID 作为参数运行应用程序。


最后隐藏表单

添加一个样式隐藏 form 表单:

HTML
  1. <div id="Form01" appml-data="local?model=/example/appml/model_customersform"
  2. appml-controller="myFormController"
  3. class="jumbotron" style="display:none">

将控制器添加到表单,以便仅在加载表单并准备好显示数据时显示表单

控制器
  1. <!DOCTYPE html>
  2. <html>
  3. <link rel="stylesheet" href="https://cankaoshouce.com/css/w3.css">
  4. <script src="https://cankaoshouce.com/js/appml/appml.js"></script>
  5. <script src="/js/appml/appml_sql.js"></script>
  6. <body>
  7. <div class="w3-container">
  8. <div id="Form01" class="w3-container w3-light-grey w3-padding-large w3-margin-bottom" appml-data="local?model=/example/appml/model_customersform" appml-controller="myFormController" style="display:none;">
  9. <div appml-include-html="/example/appml/inc_formcommands.html"></div>
  10. <p>
  11. <label for="customername">Customer:</label>
  12. <input id="customername" class="w3-input w3-border">
  13. </p>
  14. <p>
  15. <label for="address">Address:</label>
  16. <input id="address" class="w3-input w3-border">
  17. </p>
  18. <p>
  19. <label for="city">City:</label>
  20. <input id="city" class="w3-input w3-border">
  21. </p>
  22. <p>
  23. <label for="postalcode">Postal Code:</label>
  24. <input id="postalcode" class="w3-input w3-border">
  25. </p>
  26. <p>
  27. <label for="country">Country:</label>
  28. <input id="country" class="w3-input w3-border">
  29. </p>
  30. </div>
  31. <div appml-data="local?model=/example/appml/model_customerslist">
  32. <h1>Customers</h1>
  33. <div appml-include-html="/example/appml/inc_listcommands.html"></div>
  34. <div appml-include-html="/example/appml/inc_filter.html"></div>
  35. <table class="w3-table-all">
  36. <tr>
  37. <th></th>
  38. <th>Customer</th>
  39. <th>City</th>
  40. <th>Country</th>
  41. </tr>
  42. <tr appml-repeat="records">
  43. <td style="cursor:pointer;width:34px;"
  44. onclick="appml('Form01').run({{CustomerID}})">&#9998;</td>
  45. <td>{{CustomerName}}</td>
  46. <td>{{City}}</td>
  47. <td>{{Country}}</td>
  48. </tr>
  49. </table>
  50. </div>
  51. </div>
  52. <script>
  53. function myFormController($appml) {
  54. if ($appml.message == "ready") {return -1;}
  55. if ($appml.message == "loaded") {
  56. document.getElementById("Form01").style.display="";
  57. }
  58. }
  59. </script>
  60. </body>
  61. </html>