我们在业务开发中,经常会遇到自定义的视图或字段。有时候期望点击某一块区域的时候,打开一个弹窗或者是跳转新页面,但是希望这个动作是界面设计器拖拽进来的。
这篇文章详细的讲解了自定义的视图、字段
怎么执行界面设计器拖出来的按钮。
自定义视图
1: 先设计一个页面,把对应的动作拖拽进来,可以不需要配置字段
2: 将该页面绑定菜单
3: 自定义对应的页面
当我们自定义视图的时候,首先会注册一个视图,下面是我自定义的一个表单视图
registerLayout(
`<view type="FORM">
<template slot="action">
<element widget="actionBar" slot="actionBar" slotSupport="action">
<xslot name="actions" slotSupport="action" />
</element>
</template>
<element widget="MyWidget">
</element>
</view>`,
{
moduleName: 'ys0328',
model: 'ys0328.k2.Model0000000453',
actionName: 'MenuuiMenu78ec23b054314ff5a12b4fe95fe4d7b5',
viewType: ViewType.Form
}
);
我自定义了一个叫做MyWidget
的element,下面是对应的ts
代码
@SPI.ClassFactory(BaseElementWidget.Token({ widget: 'MyWidget' }))
export class MyWidgetManageWidget extends BaseElementWidget {
public initialize(props: BaseElementWidgetProps): this {
super.initialize(props);
this.setComponent(MyVue);
return this;
}
}
这是对应的vue文件: MyVue.vue
<template>
<div @click="onClick">
点击打开弹窗
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
props: ['onClick']
});
</script>
这个时候,我希望点击的时候,执行onClick,打开一个弹窗,这时只需要在对应的ts文件中写对应的代码逻辑即可:
@SPI.ClassFactory(BaseElementWidget.Token({ widget: 'MyWidget' }))
export class MyWidgetManageWidget extends BaseElementWidget {
// 获取当前页面所有的按钮
@Widget.Reactive()
public get currentViewActions() {
return this.metadataRuntimeContext.model.modelActions || []
}
// 点击事件
@Widget.Method()
public onClick() {
// 找到对应的按钮
const action = this.currentViewActions.find((a) => a.label === '点击打开弹窗');
// 执行按钮
executeViewAction(action as any);
}
public initialize(props: BaseElementWidgetProps): this {
super.initialize(props);
this.setComponent(MyVue);
return this;
}
}
以上我们就完成的自定义的视图如何触发界面设计器拖拽出来的按钮。
这是页面运行时的效果图
自定义字段
先自定义对应的字段,下面是我自定义的字段
@SPI.ClassFactory(
BaseFieldWidget.Token({
viewType: ViewType.Form,
ttype: ModelFieldType.String,
widget: 'MyField'
})
)
export class MyFieldWidget extends FormFieldWidget {
// 获取当前页面所有的按钮
@Widget.Reactive()
public get currentViewActions() {
return this.metadataRuntimeContext.model.modelActions || []
}
// 点击事件
@Widget.Method()
public onClick() {
// 找到对应的按钮
const action = this.currentViewActions.find((a) => a.label === '点击打开弹窗');
// 执行按钮
executeViewAction(action as any);
}
public initialize(props: BaseElementWidgetProps): this {
super.initialize(props);
this.setComponent(MyVue);
return this;
}
}
<template>
<div @click="onClick">
点击字段打开弹窗
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
props: ['onClick']
});
</script>
其实不管是自定义的视图还是字段,里面执行动作的写法其实是一样的。
如果遇到找不到的action,可以通过debugger的方式排查this.metadataRuntimeContext.model.modelActions
Oinone社区 作者:汤乾华原创文章,如若转载,请注明出处:https://doc.oinone.top/frontend/4384.html
访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验