This tutorial mainly introduces how to use CustomLayout to achieve auto layout.
- Note: If you want to implement a fully customized layout by defining coordinates, you can refer to the tutorial: CustomRender. The CustomRender approach supports users in customizing the elements needed within a cell, using a callback function to return an array of elements, specifying the type, style, and coordinates of the elements (VTable CustomRender layout capability design). However, this method is more low-level, and if users want to implement a complex style, they need to manually calculate the positions of various elements, and manually handle alignment, line wrapping, and other functions, which is relatively difficult to get started with and has low maintainability. -
CustomLayout provides a simple box model layout capability on top of the CustomRender API. Users can achieve basic layout capabilities such as alignment and wrapping by configuring containers and elements, facilitating the implementation and maintenance of more complex cell content. VTable uses the graphical and layout capabilities provided by VRender to implement the customLayout function. Currently, the use of JSX syntax is recommended for its clearer hierarchical structure, see example.
Below is a relatively complex text and icon mixed layout implemented using CustomLayout (red indicates the bounds of different containers):
CustomLayout Configuration
Similar to customRender, customLayout is divided into two interfaces: customLayout and headerCustomLayout, to configure custom rendering for headers and content respectively, within columns/rows.
The customLayout function returns an object, which needs to include: rootContainer to specify the root node for the custom rendering content, and renderDefault to specify whether the original cell content needs to be rendered (consistent with customRender).
Here is a configuration example, where VGroup, VImage, and VText are used, and finally returned:
It is divided into five containers: A, B, CD, C, and D
The header part is laid out horizontally, divided into three sections (A, B, CD):
The left and right sides (A, B), with specified pixel width (determined by the icon size), and the height equal to the cell height
The middle part (CD) has the cell height, with the specified width being the cell width minus the total width of AB
The middle part is laid out vertically, divided into two sections (C, D):
The upper part (C) has a specified height (determined by the "all" text style), and the width is the width of the parent container
The lower part (D) does not have a specified height; its width is the width of the parent container, and the actual height is determined by the layout result, with parts exceeding the container being truncated
The lower middle part (D) is laid out horizontally, with three elements: group text, province button, and city button
The province and city buttons are composed of multiple elements, and the height of the entire container is determined by the layout wrap result. The minimum height is when it is displayed in one line without wrapping; the maximum height is when all three elements are wrapped and displayed in three lines.
Container primitives VGroup are box model layout containers that support automatic layout of elements within them; VGroup's child elements can be VGroup or basic primitives; layout supports configuring the following properties:
Primitives can be configured with the boundsPadding property to achieve a margin effect
boundsPadding: [marginTop, marginRight, marginBottom, marginLeft]
The margin of a primitive is calculated in the space it occupies
Primitive State Updates and Interaction Events
In addition to basic properties, state updates can be used to implement interaction effects such as hover:
To create primitive objects using CustomLayout, you need to use createXXX to create primitives. For specific configuration properties, refer to VRender Primitive Configuration
VTable provides animation support for custom layouts, you can refer to the VRender animation tutorial for details. It should be noted that the animation needs to be configured as a timeline on the VTable instance to ensure the consistency of the animation.
If you create a primitive in JSX, you need to add the animation attribute and timeline to the primitive tag. The animation attribute is an array containing the operations in the VRender animation, which will be chained after the object is instantiated, for example: