布局

基本布局

v-formly-v3 表单支持三种布局,水平horizontal,垂直vertical,行内inline,默认水平布局。

代码演示

<template>
  <div>
    <a-radio-group
      v-model:value="mode"
      style="margin-bottom: 32px; margin-left: 64px"
    >
      <a-radio-button value="horizontal"> 水平 </a-radio-button>
      <a-radio-button value="vertical"> 垂直 </a-radio-button>
      <a-radio-button value="inline"> 行内 </a-radio-button>
    </a-radio-group>
    <v-formly-v3 ref="form" v-model="formData" :meta="meta" :layout="mode">
    </v-formly-v3>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";

let mode = ref("horizontal");
const meta = {
  type: "object",
  properties: {
    name: {
      title: "姓名",
      type: "string",
      default: "kevin",
      ui: {
        showRequired: true,
        errors: {
          required: "请输入姓名",
        },
      },
    },
    desc: {
      title: "描述",
      type: "string",
      default: "Base on technical, but not limited on it!",
      ui: {
        change: (val: any) => {
          console.log("val = ", val);
        },
      },
    },
    enable: {
      title: "启用",
      type: "boolean",
    },
  },
  required: ["name"],
};
let formData = ref({ enable: true });
</script>

<style lang="less" scoped></style>
复制

自定义复杂布局

利用 AntDv 的 grid 布局功能,v-formly 可以支持复杂的布局结构。

提示

自定义复杂布局搭配 v-formly-v3 的垂直布局使用更美观!

代码演示

<template>
  <div>
    <v-formly-v3
      ref="form"
      v-model="formData"
      :meta="meta"
      :layout="'vertical'"
    >
    </v-formly-v3>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";

const meta = {
  type: "object",
  properties: {
    name: {
      title: "姓名",
      type: "string",
      default: "kevin",
      ui: {
        showRequired: true,
        errors: {
          required: "请输入姓名",
        },
      },
    },
    desc: {
      title: "描述",
      type: "string",
      default: "Base on technical, but not limited on it!",
      ui: {
        change: (val: string) => {
          console.log("val = ", val);
        },
      },
    },
    field1_1: {
      title: "字段1_1",
      type: "string",
      ui: {
        grid: {
          span: 8,
        },
      },
    },
    field1_2: {
      title: "字段1_2",
      type: "string",
      ui: {
        grid: {
          span: 8,
        },
      },
    },
    field1_3: {
      title: "字段1_3",
      type: "string",
      ui: {
        grid: {
          span: 8,
        },
      },
    },
    field2_1: {
      title: "字段2_1",
      type: "string",
      ui: {
        grid: {
          span: 12,
        },
      },
    },
    field2_2: {
      title: "字段2_2",
      type: "string",
      ui: {
        grid: {
          span: 12,
        },
      },
    },
    obj: {
      type: "object",
      properties: {
        obj1: {
          title: "子对象1",
          type: "string",
          ui: {
            grid: {
              span: 4,
            },
          },
        },
        obj2: {
          title: "子对象2",
          type: "string",
          ui: {
            grid: {
              span: 4,
            },
          },
        },
        obj3: {
          title: "子对象3",
          type: "string",
          ui: {
            grid: {
              span: 4,
            },
          },
        },
        obj4: {
          title: "子对象4",
          type: "string",
          ui: {
            grid: {
              span: 4,
            },
          },
        },
        obj5: {
          title: "子对象5",
          type: "string",
          ui: {
            grid: {
              span: 4,
            },
          },
        },
        obj6: {
          title: "子对象6",
          type: "string",
          ui: {
            grid: {
              span: 4,
            },
          },
        },
      },
    },
    enable: {
      title: "启用",
      type: "boolean",
    },
  },
  required: ["name"],
};
let formData = ref({ enable: true });
</script>

<style lang="less" scoped></style>
复制