Based on decorator - I use TypeScript to process table configuration again

original
03/03 15:06
Reading number 141

1、 First look at the code after implementation

1. View layer

 <template> <atable v-loading="isLoading" :data-list="response.list" show-detail :entity="MaterialEntity" @on-detail="onDetail" @on-edit="onEdit" @on-delete="onDelete" @on-sort-change="request.sort = $event; getList()" /> </template> <script lang="ts" setup> import { ref } from 'vue' import { ATable } from '@/airpower/component' import { MaterialEntity } from '@/model/entity/MaterialEntity' import { MaterialService } from '@/service/MaterialService' import { AirRequestPage } from '@/airpower/model/AirRequestPage' import { AirResponsePage } from '@/airpower/model/AirResponsePage' const isLoading = ref(false) const response = ref(new AirResponsePage<MaterialEntity>()) const request = ref(new AirRequestPage(MaterialEntity)) async function getList() { response.value = await MaterialService.create(isLoading).getPage(request.value) } async function onEdit(row: MaterialEntity) { await AirDialog.show(MaterialEditor, row) getList() } async function onDelete(data: MaterialEntity) { Await MaterialService. create (isLoading). delete (data. id, 'Delete material successfully') getList() } async function onAdd() { await AirDialog.show(MaterialEditor) getList() } async function onDetail(data: MaterialEntity) { await AirDialog.show(MaterialDetail, data) getList() } getList() </script>

The effect is shown in the figure below

 image.png

above ATable It is a table component that we encapsulate. By default entity The attribute configured entity class reads the table columns - of course, each column supports slots, as follows:

 <atable :entity="MaterialEntity"> <template #materialname="{data}"> I am the material: {{data. materialName}} <template> </template></template></atable>

In this way, even if materialName The attribute marks the disorderly configuration, which is not as high as the priority of this slot.

2. Model layer

 import { ClassName,  Dictionary, FieldName } from '@/airpower/decorator/Custom' import { TableField } from '@/airpower/decorator/TableField' import { BaseEntity } from '@/base/BaseEntity' /** *# Material Entity * @author Hamm */ @ClassName ('material ') export class MaterialEntity extends BaseEntity { @TableField({ forceShow: true, isCopyField: true, }) @FieldName ('materialName ') materialName!: string @TableField() @FieldName ('specification and model ') materialSpc!: string @Dictionary(MaterialTypeDictionary) @TableField({ showColor: true, width: 100, }) @FieldName ('materialType ') materialType!: MaterialType } /** *# Material Type * @author Hamm */ export enum MaterialType { /** *# Public materials */ PUBLIC = 1, /** *# Private materials */ PRIVATE = 2 } /** *# Item Type Enumeration Dictionary * @author Hamm */ export const MaterialTypeDictionary = AirDictionaryArray.createCustom<imaterialtypedictionary>([ { key: MaterialType.PUBLIC, Label: 'Public material', color: AirColor.SUCCESS, other: 1, }, { key: MaterialType.PRIVATE, Label: 'Private material', color: AirColor.NORMAL, other: 2, }, ])

Other dependencies in some projects in the above model are no longer attached, such as:

  • Base class BaseEntity (It contains some fields that must be included by fixed entities, such as id createTime Etc.)

  • Method of creating dictionary by dictionary array AirDictionaryArray.createCustom<t extends idictionary>(list: T[]): AirDictionaryArray<t>

  • Custom special dictionary IMaterialTypeDictionary (Inherited from standard dictionary IDictionary , some components limit that the standard dictionary and its subclasses or interfaces must be passed in)

  • @TableField(config: ITableFieldConfig) Interface for ITableFieldConfig In fact, it contains some general table configurations, as follows:

     /** *# Table Field Configuration Interface * @author Hamm */ export interface ITableFieldConfig extends IFieldConfig { /** *# Table Field Width */ width?: number; /** *# Enumeration dictionary * --- * ###  💡  If the dictionary is configured with '` ` color ` ` `, you can use the' ` showColor ` ` configuration item to display colors */ dictionary?: AirDictionaryArray<idictionary>; /** *# Conversion rules can be passed in for dates */ dateTimeFormatter?: AirDateTimeFormatter | string; /** *# Whether to display the color status light of enumeration dictionary * --- * ###  💡  If it is displayed, please ensure that the incoming '` dictionary' ` is configured with '` ` color``` */ showColor?: boolean; /** *# Whether the field can be sorted but not by default * --- * ###  💡 ``` Custom ` ` ` For custom sorting, ` ` ` ATable ` ` components will trigger ` ` onSortChange ` ` events */ sortable?: boolean | 'custom'; /** *# Column Alignment */ align?: 'right' | 'left' | 'center'; /** *# Post text * --- * ###  💡  It is generally used to display some text of similar units */ suffixText?: string; /** *# is a field that can be copied * --- * ###  💡  This table column allows one key copy */ isCopyField?: boolean; /** *# Picture Field * --- * ###  💡  Configurable ` ` ` imageWidth ` ` `, ` ` ` imageHeight ` ` and so on */ isImage?: boolean; /** *# The default width of the image is 60 */ imageWidth?: number; /** *# The default height of the image is 60 */ imageHeight?: number; /** *# Empty data string * --- * ###  💡  Global disclosure can be made in ` ` ` AirConfig. defaultTableEmptyValue ` ` `, *This configuration item will be used preferentially. Only normal fields and mount fields are supported */ emptyValue?: string //There are also too many configurations. Don't be afraid of trouble. Anyway, there is an automatic prompt of '' TypeScript '', which is great. }

3. Service layer

 /** *# Material Interface Service * @author Hamm */ export class MaterialService extends AbstractBaseService<materialentity> { entityClass = MaterialEntity baseUrl = 'material' }

Similarly, the above Service class is no longer attached AbstractBaseService<e extends baseentity> In short, some general additions, deletions, modifications and queries have been implemented in the Service base class

2、 Why do you write that

In fact, Java is used to writing. Everything is used to using classes or interfaces to restrict the parameter types and data structures I need.

Also, TypeScript has many code hints and the convenience of refactoring with the help of vscode. Of course, just writing JavaScript or AnyScript is not such a pleasure.

1. Based on TypeScript Code hints and data type constraints for

 image.png

 image.png

2. Based on Java Annotation Implementation of decorator configuration of ideas

All about materiel The configuration information such as tables, forms, data conversion methods, validation, dictionaries, and searches of MaterialEntity This material entity is configured through the decorator. Other places (such as tables, forms, search boxes, etc.

3. Front end development based on face object thinking

Originally, this article is not suitable for describing too much on object-oriented, but it has already mentioned a lot TypeScript It's a bit unreasonable not to mention the new features of.

First, I will attack the Denver Nuggets TypeScript 's article:

  • No new idea: carrying tutorial, teaching you how to prepare environment and get started, teaching you how to use interface To constrain the data structure
  • Misleading users: As mentioned above, everyone forgot class extends implements The existence of
  • Type Gymnastics: As I commented under many Nuggets articles before Type gymnastics is the most fun of Typescript, and it is also the last thing that should not exist
  • There are too many: next time I will attack, the article will be biased.

In short, based on object-oriented, the front end is still promising. Some very old and traditional thinking may never be incisive on the front end, but I believe that it should be soon, because:

  • TypeScript Prevalence in the front end
  • Decorator Popularization in the front end php Decorators are starting to come out)
  • Object oriented, generic, AOP idea, dependency injection and other back-end concepts are transferred to the front
  • Vue.js The emergence of such a relatively low entry front-end framework
  • The emergence of articles such as death anxiety at the front end of selling
  • There are still a lot of it

3、 That's all

That's all for today. Those who are interested can exchange and learn together.

Yes, I am Java Operation and maintenance boy Front-end offspring , but not only.

Updated at 17:31:14 on July 26, 2023

The project has been opened in Github: https://github.com/HammCn/AirPower4T

4、 See this column for more articles

https://juejin.cn/column/7249148919505682491 </e></materialentity></idictionary></t></t></imaterialtypedictionary>

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
zero Collection
zero fabulous
 Back to top
Top