AppML - Controller


AppML application allows to control the UI using the controller function. appml-controller tag provides the name of the javascript function which acts as a controller. AppML application may or may not have a controller.

Syntax

<table appml-controller="studentController"></table>

AppML sends messages to controller function via an application object denoted by $appml. Based on the properties of $appml we can perform various types of operations on HTML content. Following are some of the important examples.

  • Initialize data

  • Update application data

  • Input/output handling

  • Data validation

  • Data summarization

  • Error handling

  • Application start/stop

Example

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://www.w3schools.com/appml/2.0.3/appml.js"></script>
<body>
   <h1>Students</h1>
   <table appml-data="students" appml-controller="studentController">
      <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>
   <script>
      var students = {
         "records":[
            {"studentName":"Ramesh","class":"12","section":"White"},
            {"studentName":"Suresh","class":"12","section":"Red"},
            {"studentName":"Mohan","class":"12","section":"Red"},
            {"studentName":"Robert","class":"12","section":"Red"},
            {"studentName":"Julie","class":"12","section":"White"},
            {"studentName":"Ali","class":"12","section":"White"},
            {"studentName":"Harjeet","class":"12","section":"White"}
         ]};
      function studentController($appml) {
         if ($appml.message == "display") {
            if ($appml.display.name == "studentName") {
               $appml.display.value = $appml.display.value.toUpperCase();
            }
         }
      }
   </script>
</body>
</html>

Output

Deploy the application on Web Server and access the html page. Verify the output.

first application
Advertisements