自定义视图组件(v4)

阅读之前

你应该:

什么是视图组件

我们将一个视图中提供数据源的组件称为视图组件。

下面,我们将根据提供的示例布局进行进一步介绍。

示例布局(默认表格视图布局)

<view type="TABLE">
    <pack widget="group">
        <view type="SEARCH">
            <element widget="search" slot="search" />
        </view>
    </pack>
    <pack widget="group" slot="tableGroup">
        <element widget="actionBar" slot="actionBar">
            <xslot name="actions" />
        </element>
        <element widget="table" slot="table">
            <element widget="expandColumn" slot="expandRow" />
            <xslot name="fields" />
            <element widget="rowActions" slot="rowActions" />
        </element>
    </pack>
</view>
  • view: 视图标签;一个视图中的所有组件将共享数据源,视图的数据源通过视图组件进行提供。(在这个示例中,该视图的数据源通过widget="table"(TableWidget)提供)
  • pack: 容器组件标签;
  • element: 通用元素组件标签;
  • xslot:dsl插槽;

根据标签性质,我们可以将这个示例布局进一步简化,只留下我们目前要关注的主要内容。

<view type="TABLE">
    <element widget="table" slot="table">
        <xslot name="fields" />
    </element>
</view>

在以上示例布局中,有且仅有一个组件会向视图提供数据源,那就是widget="table"(TableWidget)这个组件。
我们接下来将对这个组件进行自定义,以实现业务中所需的列表(List)数据源展示方式。

1 平台组件简介

平台提供的基础组件有如下几种:

组件名称 描述
BaseElement element标签通用组件
BaseElementViewWidget 通用视图组件
BaseElementObjectViewWidget 对象(Object)数据源通用视图组件
BaseElementListViewWidget 列表(List)数据源通用组件

平台提供的内置组件有如下几种:(均使用element标签)

组件名称 标签 视图类型 描述
TableWidget widget="table" TABLE 内置表格组件
FormWidget widget="form" FORM 内置表单组件
DetailWidget widget="detail" DETAIL 内置详情组件
GallertWidget widget="gallery" GALLERY 内置画廊组件
TreeWidget/CardCascaderWidget widget="tree/cardCascader" TREE 内置树/卡片级联组件

我们可以根据业务场景,继承不同的组件,来实现自己的业务场景。
在自定义过程中,我们建议尽可能的将逻辑控制在组件内部。如果场景是唯一且确定的,也可以进行一些特殊逻辑处理。

2 场景:实现一个虚拟滚动表格(不使用分页器)

2.1 确定组件名称 widget="VirtualTable"

通过布局设置自定义组件名称

我们将原表格布局中的widget="table"改为我们所需要的自定义组件名称即可。

多个视图可以绑定同一个布局,所以这种修改方式更适用于大范围使用相同布局的情况。

<view type="TABLE">
    <pack widget="group">
        <view type="SEARCH">
            <element widget="search" slot="search />
        </view>
    </pack>
    <pack widget="group" slot="tableGroup">
        <element widget="actionBar" slot="actionBar>
            <xslot name="actions" />
        </element>
        <element widget="VirtualTable" slot="table">
            <element widget="expandColumn" slot="expandRow" />
            <xslot name="fields" />
            <element widget="rowActions" slot="rowActions" />
        </element>
    </pack>
</view>

通过DSL设置自定义组件名称

我们使用了slot="table"这个插槽,通过属性合并覆盖的方式,在DSL上直接指定我们所需要的自定义组件名称即可。

这种修改方式更适用于个别几个视图需要使用该组件的情况。

<view type="TABLE">
    <template slot="table" widget="VirtualTable">
        ......
    </template>
</view>

2.2 简单实现一个基础功能的虚拟滚动表格

定义一个VirtualTable.vue文件,使用平台提供的oio-table组件。目前内部采用vxe-table封装,相关api文档 点击查看

props定义:

  • showDataSource: 当前展示数据;通过平台内置BaseElementListViewWidget组件提供。
<template>
  <oio-table ref="table" border show-overflow height="400" :row-config="{ isHover: true }" :data="showDataSource">
    <slot />
  </oio-table>
</template>
<script lang="ts">
import { ActiveRecord, OioTable } from '@kunlun/dependencies';
import { defineComponent, PropType } from 'vue';

export default defineComponent({
  name: 'VirtualTable',
  components: {
    OioTable
  },
  props: {
    showDataSource: {
      type: Array as PropType<ActiveRecord[]>
    }
  }
});
</script>

定义一个VirtualTableWidget.ts文件,继承BaseElementListViewWidget组件,可快速实现根据元数据查询列表数据等相关功能。

通过重写showPagination属性,强制关闭分页器。

import { BaseElementListViewWidget, BaseElementWidget, SPI, ViewType, Widget } from '@kunlun/dependencies';
import VirtualTable from './VirtualTable.vue';

@SPI.ClassFactory(
  BaseElementWidget.Token({
    viewType: ViewType.Table,
    widget: 'VirtualTable'
  })
)
export class VirtualTableWidget extends BaseElementListViewWidget {
  public initialize(props) {
    super.initialize(props);
    this.setComponent(VirtualTable);
    return this;
  }

  @Widget.Reactive()
  protected get showPagination(): boolean | undefined {
    return false;
  }
}

2.3 工程结构如下图所示

image.png
VirtualTableWidget.ts文件在main.ts(入口文件)导入即可。

Oinone社区 作者:oinone原创文章,如若转载,请注明出处:https://doc.oinone.top/frontend/22.html

访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验

(0)
oinone的头像oinone
上一篇 2023年6月20日 pm4:07
下一篇 2023年11月2日 pm1:58

相关推荐

  • 组件生命周期(v4)

    阅读之前: 你应该: 了解DSL相关内容。母版-布局-DSL 渲染基础(v4) 对第三方框架的组件生命周期有所了解。如Vue组件生命周期 了解平台实现的Class Component(ts)相关内容。Class Component(ts)(v4) 组件生命周期 任何一个Widget其标准生命周期应当包括beforeCreated、created、beforeMount、mounted、beforeUnmount、unmounted这六个基本的生命周期函数、以及beforeUpdate和updated在响应式更新时会进行调用的生命周期函数。特别的,还有activated和deactivated在组件搭配keep-alive特性时使用的生命周期函数。 具体的生命周期执行过程在这里不再进行赘述,这里的基本逻辑与Vue组件生命周期基本完全一致,感兴趣的读者可以阅读Vue相关文档进行学习。

    2023年11月1日
    92210
  • OioNotification 通知提醒框

    全局展示通知提醒信息。 何时使用 在系统四个角显示通知提醒信息。经常用于以下情况: 较为复杂的通知内容。 带有交互的通知,给出用户下一步的行动点。 系统主动推送。 API OioNotification.success(title,message, config) OioNotification.error(title,message, config) OioNotification.info(title,message, config) OioNotification.warning(title,message, config) config 参数如下: 参数 说明 类型 默认值 版本 duration 默认 3 秒后自动关闭 number 3 class 自定义 CSS class string –

    2023年12月18日
    77600
  • 自定义mutation时出现校验不过时,如何排查

    场景描述 用户在自定义接口提供给前端调用时 @Action(displayName = "注册", bindingType = ViewTypeEnum.CUSTOM) public BaseResponse register(UserZhgl data) { //…逻辑 return result; } import java.io.Serializable; public class BaseResponse implements Serializable { private String code; private String msg; public BaseResponse() { } public BaseResponse(String code, String msg) { this.code = code; this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } } gql执行时出现报错 { "errors":[ { "message":"Validation error of type SubSelectionNotAllowed: Sub selection not allowed on leaf type Object of field register @ 'zhglMutation/register'", "locations":[ { "line":3, "column":3 } ], "extensions":{ "classification":"ValidationError" } } ] } 解决方案 1.返回对象不为空时,对象必须是模型,否则无法解析返回参数2.前端调用GQL异常时,可以用Insomnia工具对GQL进行测试,根据错误提示对GQL进行修改和排查3.GQL正常情况下,执行以后可根据后端日志进行错误排查

    2023年11月1日
    1.1K00
  • 表格如何支持表尾合计计算

    介绍 可以通过扩展TableWidget.ts实现 示例代码 import { BaseElementWidget, DslDefinitionType, SPI, TableWidget, ViewType, Widget } from '@kunlun/dependencies'; @SPI.ClassFactory( BaseElementWidget.Token({ type: ViewType.Table, widget: 'table', model: 'resource.k2.Model0000000109', viewName: '移动端品牌_TABLE_0000000000021513' }) ) export class FooterStatisticsTable extends TableWidget { public initialize(props) { if (props.template) { props.template?.widgets?.forEach((a) => { if (a.dslNodeType === DslDefinitionType.FIELD && this.statisticsFieldList.includes(a.name)) { a.statistics = true; } }); } super.initialize(props); return this; } // 需要表尾做合并的字段名称 public statisticsFieldList = ['fansNum']; @Widget.Reactive() protected get showFooter(): boolean | undefined { return true; } } 效果预览

    2024年10月14日
    99100
  • oio-modal 对话框

    API 参数 说明 类型 默认值 版本 cancelText 取消按钮文字 string| slot 取消 closable 是否显示右上角的关闭按钮 boolean true closeIcon 自定义关闭图标 VNode | slot – confirmLoading 确定按钮 loading boolean 无 destroyOnClose 关闭时销毁 Modal 里的子元素 boolean false footer 底部内容,当不需要默认底部按钮时,可以设为 :footerInvisible="true" slot 确定取消按钮 getTriggerContainer 指定 Modal 挂载的 HTML 节点 (instance): HTMLElement () => document.body keyboard 是否支持键盘 esc 关闭 boolean true mask 是否展示遮罩 boolean true maskClosable 点击蒙层是否允许关闭 boolean true enterText 确认按钮文字 string 确定 title 标题 string|slot 无 visible(v-model:visible) 对话框是否可见 boolean 无 width 宽度 string|number 520 wrapClassName 对话框外层容器的类名 string – zIndex 设置 Modal 的 z-index number 1000 cancelCallback 点击遮罩层或右上角叉或取消按钮的回调, return true则关闭弹窗 function(e) enterCallback 点击确定回调 function(e)

    2023年12月18日
    98100

Leave a Reply

登录后才能评论