Skip to main content

Archive

Show more

Angular.js Tables

Angular.js Tables

In Angular.js, tables are commonly used to display tabular data in a structured format. Angular.js provides directives and features to efficiently create and manipulate tables.


1. Overview

Angular.js tables allow you to:

  • Display Data: Tables can be used to display data retrieved from a server or stored locally in an array.
  • Dynamic Rendering: Angular.js directives such as ng-repeat allow for dynamic rendering of table rows based on data.
  • Sorting and Filtering: Angular.js provides built-in features for sorting and filtering table data without the need for additional JavaScript code.
  • Interactive Tables: Tables can be made interactive by incorporating Angular.js directives for handling user interactions such as clicks or selections.

2. Creating Tables

To create tables in Angular.js, you can use HTML markup along with Angular.js directives:

<table>
    <thead>
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
            <th>Header 3</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="item in items">
            <td>{{ item.property1 }}</td>
            <td>{{ item.property2 }}</td>
            <td>{{ item.property3 }}</td>
        </tr>
    </tbody>
</table>

In this example, the ng-repeat directive iterates over an array of items and dynamically creates table rows based on the data.


3. Sorting and Filtering

Angular.js provides built-in directives for sorting and filtering table data:

  • ng-repeat: Renders table rows based on an array of items.
  • orderBy: Sorts table data based on a specified property.
  • filter: Filters table data based on a given criteria.

These directives can be combined to create sortable and filterable tables without writing additional JavaScript code.


4. Interactive Tables

Angular.js allows you to make tables interactive by adding event handling directives such as ng-click or ng-change:

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="item in items">
            <td>{{ item.id }}</td>
            <td>{{ item.name }}</td>
            <td>
                <button ng-click="editItem(item)">Edit</button>
                <button ng-click="deleteItem(item)">Delete</button>
            </td>
        </tr>
    </tbody>
</table>

In this example, clicking the "Edit" or "Delete" buttons triggers corresponding functions defined in the controller.


5. Conclusion

Angular.js tables provide a convenient way to display and interact with tabular data in web applications. By leveraging Angular.js directives and features, you can create dynamic, sortable, and interactive tables with minimal effort.

Comments