Introduction to Ant Design Table (AntD)
Ant Design Table, or AntD Table, is a powerful and feature-rich component that the Ant Design framework provides for ReactJS. Its comprehensive functionalities make it an excellent choice for displaying and manipulating tabular data in web applications.
Enhance your skills with Ant Design Table by signing up for a Full Stack Software Development Bootcamp.
Setting Up an Ant React Table
Follow these steps to set up an Ant Design Table in a ReactJS project:
- Install Ant Design: npm install antd
- Import necessary components: import { Table } from ‘antd’;
- Use the <Table> component, providing data and column configuration as props.
- Customise the dataSource and columns arrays.
- Render the Table component in your React component hierarchy.
Check out our free technology courses to get an edge over the competition.
Creating a Basic Ant Table
Follow these steps to create a basic React Ant Design Table:
- Install Ant Design and import the components and styles required.
- Define the Table’s data source and column configuration:
- Render the Table component in your React component:
- Use the <MyTable /> component to display the Table wherever you want.
- Explore additional Ant Design (React) features and options to customise it further.
Customising Table Columns and Rows
Customise Ant Design Tables and rows with ReactJS using various AntD properties and components. Follow the steps below:
- Install Ant Design and import the components and styles required.
- Create a custom Table component with columns and data.
- Customise the Table appearance using properties like rowClassName.
- Define custom CSS styles for Table rows.
Sharpen your AntD skills with a comprehensive course like the Master of Science in Computer Science from LJMU.
Sorting and Filtering Data in AntD Table
Use the library’s built-in features and properties to enable sorting and filtering in an Ant Design (AntD) Table by following the given steps:
- Import the necessary components and styles from Ant Design.
- Create a state variable to store the filtered data.
- Define your Table’s columns and add the sorter and filter properties.
- Define the data for your Table.
- Render the Table component with the defined columns, data, and filtering properties.
- Implement the onChange event handler to sort and filter.
- When sorting is triggered, use the sorter object to determine the column key and sorting order. Sort the data accordingly and update the state with the sorted data.
- Use the filter object to determine the column key and filter value once filtering is triggered. Filter the data based on the provided value and update the state with the filtered data.
- If no sorting or filtering is applied, reset the filtered data state to an empty array.
- Render the custom Table component in your application.
Paginating and Grouping Data in AntD Table
Use the pagination and row grouping features provided by AntD to paginate and group data in an Ant Design (AntD) Table using ReactJS.
- Import the necessary components and styles from AntD
- Define the columns and data for your Table.
- Set up pagination:
- Define a state variable to store the current page number and items per page.
- Implement an onChange event handler to update the state when the page changes or the items per page are modified.
- Render the Pagination component, passing the current page and the total number of items as props.
- Group rows in the Table:
- Define a rowKey for each row to ensure uniqueness.
- Use the rowSpan property to specify the number of rows a particular cell should span within a group.
- Render the Table:
- Pass the columns and data to the Table component as props.
- Use the pagination prop to enable pagination, passing the current page and items per page state variables.
- For row grouping, use the rowSpan property within columns where grouping is required.
Adding Selections and Actions to AntD Table
To add selections and actions to an Ant Design (AntD) Table using ReactJS:
- Import the necessary components and styles from AntD.
- Define a state variable to store the selected row keys.
- Use the rowSelection property in the Table component to enable row selection, passing the selected row keys and an onChange event handler.
- Implement the onChange event handler to update the selected row keys state.
- Add additional columns to the Table component to display actions or checkboxes for each row.
- Implement action or checkbox event handlers to perform desired actions based on the selected rows.
- Customise the actions or checkboxes display using the render or customRender property in the columns.
- Optionally, customise the appearance of selected rows using the rowClassName property.
- Render the Table component in your application.
Implementing Search and Highlighting in AntD Table
To implement search functionality and highlighting in an Ant Design (AntD) Table using ReactJS:
- Define a state variable to store the search input value and a filtered data state variable.
- Implement an onSearch event handler to update the search input value state and filter the data based on the input value.
- Render a search input component and bind it to the onSearch event handler.
- Display the filtered rows in the Table using the filtered data state variable.
- Implement a highlighting mechanism in the Table:
- Define a function that takes the search input value and cell data as arguments and returns the highlighted text.
- In the column’s render or customRender property, apply the highlighting function to the cell data using JSX.
- Render the Table component in your application.
Adding Loading and Error States to AntD Table
To add loading and error states to an Ant Design (AntD) Table using ReactJS:
- Define a state variable to track the loading state and an error state variable.
- Set the initial loading state to true and the error state to false.
- Use an asynchronous function or API call to fetch the data for the Table.
- Within the function or API call, update the loading state accordingly.
- If an error occurs during the data fetching process, update the error state to true.
- Render the Table component with the loading prop set to the loading state variable.
- Optionally, handle the error state in the component’s rendering logic by displaying an error message or taking appropriate actions.
- Customise the loading state appearance using the Spin component or other AntD-provided loading indicators.
- Render the Table component and handle your application’s error and loading states.
Integrating AntD Table with Backend APIs
To integrate an Ant Design (AntD) Table with backend APIs in ReactJS:
- Define a state variable to store the data fetched from the backend API.
- Use a lifecycle method (e.g., useEffect) or an event handler to make an API request to fetch the data from the backend.
- Within the API request, update the state variable with the fetched data.
- Render the Table component with the data obtained from the backend API.
- Customise the columns based on the data structure received from the API.
- Optionally, handle error scenarios and loading states while fetching data from the backend.
- Customise the Table appearance and behaviour using AntD’s properties, such as pagination, sorting, and filtering, to match the API response and user requirements.
- Implement event handlers or callback functions to handle actions performed on the Table, such as editing or deleting data.
- Make API requests to update the backend data based on the user’s actions.
Explore our Popular Software Engineering Courses
Best Practices and Tips for Working with AntD Table
Follow these best practices and tips when working with Ant Design (AntD) Table:
- Assign unique keys to columns for improved performance and to avoid rendering issues.
- Implement server-side pagination and filtering to reduce the amount of data fetched and improve loading times.
- Use memorisation techniques like React.memo or useMemo to optimise rendering for column cells or custom components.
- Employ virtual scrolling or lazy loading techniques for Tables with many rows to enhance performance.
- Use built-in row selection and action features to handle bulk operations efficiently.
- Leverage AntD’s APIs and styling options to customise Table appearance and behaviour to match your design and requirements.
- Ensure the Table’s accessibility by providing appropriate labels, keyboard navigation, and ARIA attributes.
Future Updates and Developments in AntD Table
Since its inception, AntD has been periodically optimised with regular updates, bug fixes and new features. With the latest update of AntD version 5.6.2 released on 19th June 2023, this Table component has received many features, style addons, bug fixes and Table fixes.
As for future updates, AntD is set to receive weekly bug fixes and minor version monthly releases.
Conclusion
Creating an Ant Design (AntD) Table in ReactJS is a powerful way to display and manage tabular data in your applications. Follow the provided steps and leverage the extensive features and customisation options offered by AntD to create interactive and efficient Tables, enhancing user experience and streamlining data presentation.
Register for upGrad’s Executive PG Programme in Full Stack Development from IIITB and start building your feature-rich Tables with AntD.