ตัวควบคุม W3.JS


คอนโทรลเลอร์คืออะไร?

ตัวควบคุมคือฟังก์ชันที่คุณเขียนเพื่อควบคุมข้อมูลของคุณ

ด้วยตัวควบคุมแบบเขียนเอง คุณสามารถแก้ไขข้อมูลได้ตามที่คุณต้องการ:

  • แปลงเป็นตัวพิมพ์ใหญ่
  • แปลงสกุลเงิน
  • คำนวณและสรุป
  • ค่ารอบ
  • ค่าทดแทน
  • เปลี่ยนสีตามค่า
  • อะไรก็ได้ที่คุณสามารถตั้งโปรแกรมได้

ตัวอย่างคอนโทรลเลอร์ 1

คุณต้องการแปลงข้อมูลเป็นตัวพิมพ์ใหญ่ก่อนแสดงผล

แปลงเป็นตัวพิมพ์ใหญ่

<script>
w3.getHttpObject("customers.js", myFunction);

function myFunction(myObject) {
  var i;
  var myArray = myObject.customers;
  for (i = 0; i < myArray.length; i++) {
    myArray[i]["CustomerName"] =
    myArray[i]["CustomerName"].toUpperCase();
  }
  w3.displayObject("id01", myObject);
}
</script>

ตัวอย่างคอนโทรลเลอร์ 2

สรุปราคา

<script>
w3.getHttpObject("cd_catalog.js", myFunction);

function myFunction(myObject) {
  var i, total = 0;
  var myArray = myObject.cd;
  for (i = 0; i < myArray.length; i++) {
    total += Number(myArray[i].price);
  }
  myObject.total = total.toFixed(2);
  w3.displayObject("id01", myObject);
}
</script>