本文我们将介绍如何使用 FormMaking 来引入自己的高级组件,并可以通过设计器进行配置,处理事件等操作。

封装分页数据表格组件

我们将封装 一个分页数据表格组件,组件采用 ElementPlus TableV2

  1. <template>
  2. <div>
  3. <div style="height: 400px">
  4. <el-auto-resizer>
  5. <template #default="{ height, width }">
  6. <el-table-v2
  7. :columns="columns"
  8. :data="data"
  9. :width="width"
  10. :height="height"
  11. fixed
  12. />
  13. </template>
  14. </el-auto-resizer>
  15. </div>
  16. <el-pagination
  17. background
  18. layout="prev, pager, next"
  19. :total="1000"
  20. v-model:current-page="currentPage"
  21. @current-change="loadPageData"
  22. />
  23. </div>
  24. </template>
  25. <script setup>
  26. import { onMounted, ref, watch } from 'vue'
  27. const props = defineProps({
  28. modelValue: {
  29. type: Array,
  30. default: () => []
  31. },
  32. columns: {
  33. type: Array,
  34. default: () => []
  35. }
  36. })
  37. const emit = defineEmits(['on-load'])
  38. const data = ref(props.modelValue)
  39. const currentPage = ref(1)
  40. const loadPageData = (index) => {
  41. // 通过 emit,可以将事件抛出到设计器中进行配置
  42. emit('on-load', index)
  43. }
  44. onMounted(() => {
  45. emit('on-load', currentPage.value)
  46. })
  47. watch(() => props.modelValue, (val) => {
  48. data.value = val
  49. })
  50. </script>

引入到设计器

注册组件

首先应该在自己项目中进行组件的注册。

  1. import CustomPaginationTable from 'PaginationTable.vue'
  2. app.use(FormMakingV3, {
  3. components: [{
  4. name: 'custom-pagination-table',
  5. component: CustomPaginationTable // 自定义组件
  6. }]
  7. })

也可以使用Vue.component 进行组件的注册

  1. app.component('custom-pagination-table', CustomPaginationTable)

设计器配置

  1. <template>
  2. <fm-making-form
  3. :custom-fields="customFields"
  4. >
  5. </fm-making-form>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. customFields: [
  12. {
  13. name: '分页数据列表',
  14. el: 'custom-pagination-table',
  15. options: {
  16. defaultValue: [],
  17. labelWidth: 0,
  18. isLabelWidth: false,
  19. hidden: false,
  20. dataBind: true,
  21. validator: '',
  22. extendProps: {
  23. columns: [] // 用于配置表格的列
  24. }
  25. },
  26. events: {
  27. onLoad: '' // 定义设计器可以配置的事件,处理组件 emit 的事件
  28. }
  29. }
  30. ]
  31. }
  32. }
  33. }
  34. </script>

然后在自定义字段中就展示出来了
image.png

配置组件表格

表格列配置

在字段属性中对扩展属性配置 进行设置
image.png
image.png

数据加载

数据加载的 on-load事件,我们在自定义组件中通过emit抛出到设计器中进行配置,在字段属性-动作设置中添加onLoad事件配置如下:
image.png

效果展示

我们来查看下最后的效果
Kapture 2022-06-23 at 19.07.50.gif