AngularJS ตาราง

อินดิเคกเตอร์ ng-repeat มีความเหมาะสมสำหรับการแสดงตาราง

แสดงข้อมูลในตาราง

การแสดงตารางด้วย AngularJS นั้นง่ายและเรียบร้อย

AngularJS ตัวอย่าง

<div ng-app="myApp" ng-controller="customersCtrl">
<table>
  <tr ng-repeat="x in names">
    <td>{{ x.ชื่อ }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
  $http.get("customers.php")
  .then(function (response) {$scope.names = response.data.records;});
});
</script>

ทดลองด้วยตัวเอง

แสดงด้วย CSS สไตล์

เพื่อที่จะดูงามงดงายขึ้น คุณสามารถเพิ่ม CSS ลงในหน้าเว็บไซต์ของคุณ

CSS สไตล์

<style>
table, th , td {
  border: 1px solid grey;
  border-collapse: collapse;
  padding: 5px;
}
table tr:nth-child(odd) {
  background-color: #f1f1f1;
}
table tr:nth-child(even) {
  background-color: #ffffff;
}
</style>

ทดลองด้วยตัวเอง

แสดงด้วยตัวเลือก orderBy ฟิลเตอร์

เพื่อจัดลำดับตาราง โปรดเพิ่ม orderBy ตัวเลือกเฟิร์มเตอร์:

ตัวอย่าง AngularJS

<table>
  <tr ng-repeat="x in names | orderBy : 'ประเทศ'">
    <td>{{ x.ชื่อ }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

ทดลองด้วยตัวเอง

แสดงในรูปแบบใหญ่ด้วยตัวเลือก uppercase ฟิลเตอร์

เพื่อแสดงในรูปแบบใหญ่ โปรดเพิ่ม uppercase ตัวเลือกเฟิร์มเตอร์:

ตัวอย่าง AngularJS

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.ชื่อ }}</td>
    <td>{{ x.ประเทศ | uppercase }}</td>
  </tr>
</table>

ทดลองด้วยตัวเอง

แสดงดัชนีตาราง ($index)

เพื่อแสดงดัชนีตาราง โปรดเพิ่ม $index ของ <td>:

ตัวอย่าง AngularJS

<table>
  <tr ng-repeat="x in names">
    <td>{{ $index + 1 }}</td>
    <td>{{ x.ชื่อ }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

ทดลองด้วยตัวเอง

การใช้ $even และ $odd

ตัวอย่าง AngularJS

<table>
  <tr ng-repeat="x in names">
    <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Name }}</td>
    <td ng-if="$even">{{ x.Name }}</td>
    <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Country }}</td>
    <td ng-if="$even">{{ x.Country }}</td>
  </tr>
</table>

ทดลองด้วยตัวเอง