Vue如何实现可视化拖拽表单(一)

本文介绍了如何在Vue项目中使用Vuedraggable组件实现可视化拖拽功能,包括基本用法如拖拽排序,以及相关事件的处理。通过实例演示了如何在云程低代码平台上应用这些技术。
摘要由CSDN通过智能技术生成

Vue如何实现可视化拖拽表单(一)

在我们平时做需求中,往往会遇到拖拽模块的需求。刚开始没有接触过拖拽的时候,会对拖拽有陌生感,会比较担心实现起来会比较的复杂,可能自己很难搞定,低代码平台基本都会涵盖拖拽功能,今天来看一下到底是怎么实现的。

以云程低代码平台做下效果演示
在这里插入图片描述
使用vuedraggable组件

Vuedraggable 是标准的组件式封装,并且将可拖动元素放进了transition-group上面,过渡动画都比较好

安装命令

npm i vuedraggable
yarn add vuedraggable

引入并注册

 import draggable from 'vuedraggable'
  export default {
        components: {
            draggable,
        },
项目Value
drag可拖动的项目被拖动
dragstart拖动结束(例如放开鼠标)
dragend用户完成元素拖动后触发
dragenter拖动的项目进入可放置元素
dragleave可拖动的项目离开可放置元素
dragover可拖动项目在可放置元素上移动(每一百毫秒左右调用一次)
drop可拖动项目被放置在可放置元素上

dragenter和dragover事件的默认行为是拒绝接受任何被拖放的元素。因此,我们必须阻止浏览器这种默认行为。e.preventDefault();

组在使用的时候,可以通过v-model来双向绑定本地data,如果需要更新或者是触发父组件监听的事件,可以在updated()中去emit

基础用法–拖拽排序

 <draggable v-model="selectItem.customize.linkList" group="someGroup" animation="1000" item-key="id">
          <template v-for="(item, index) in selectItem.customize.linkList">
            <div class="right-card" style="margin-top: 10px">
              <a-form-item label="链接地址">
                <a-select size="small" v-model="item.url">
                  <a-select-option v-for="(site,index) in searchMenuOptions" :key="index" :value="site.path">
                    {{ site.meta.title }}
                  </a-select-option>
                </a-select>
              </a-form-item>
              <a-form-item label="打开方式">
                <a-select v-model="item.openType" size="small">
                  <a-select-option value="_self">当前窗口</a-select-option>
                  <a-select-option value="_blank">新窗口</a-select-option>
                </a-select>
              </a-form-item>
              <a-form-item label="图标选择">
                <ych-icon-select v-model="item.icon" write :iconfont="true" :size="'small'"></ych-icon-select>
              </a-form-item>
            </div>
          </template>
        </draggable>

效果展示
在这里插入图片描述

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
实现可视化可拖放的自定义表单可以使用Vue.js和一些第三方库来帮助我们完成。下面是一个简单的实现步骤: 1. 安装并引入vue-draggable插件 可以使用npm安装vue-draggable插件:`npm install vuedraggable --save`,然后在Vue组件中引入该插件。 ```javascript import draggable from 'vuedraggable' export default { components: { draggable } } ``` 2. 创建表单设计器组件并添加表单元素 在Vue组件中,可以使用`draggable`组件来实现拖放功能。我们可以使用`v-for`指令渲染表单元素列表,并将元素添加到`draggable`组件中。 ```html <draggable v-model="formElements"> <div v-for="(element, index) in formElements" :key="index"> <input v-if="element.type === 'input'" :type="element.inputType" :placeholder="element.placeholder"> <select v-else-if="element.type === 'select'"> <option v-for="(option, optionIndex) in element.options" :key="optionIndex">{{ option }}</option> </select> </div> </draggable> ``` 3. 添加表单元素到表单设计器 我们可以添加按钮来添加新的表单元素。在Vue组件中,我们可以使用`@click`事件添加新的元素到表单元素列表。 ```html <button @click="addInput">Add Input</button> <button @click="addSelect">Add Select</button> ``` ```javascript export default { data() { return { formElements: [] } }, methods: { addInput() { this.formElements.push({ type: 'input', inputType: 'text', placeholder: 'Enter text' }) }, addSelect() { this.formElements.push({ type: 'select', options: ['Option 1', 'Option 2', 'Option 3'] }) } } } ``` 4. 渲染表单 最后,我们可以使用`v-for`指令再次渲染表单元素列表,并将表单元素添加到表单中。 ```html <form> <div v-for="(element, index) in formElements" :key="index"> <input v-if="element.type === 'input'" :type="element.inputType" :placeholder="element.placeholder"> <select v-else-if="element.type === 'select'"> <option v-for="(option, optionIndex) in element.options" :key="optionIndex">{{ option }}</option> </select> </div> <button type="submit">Submit</button> </form> ``` 完整代码如下: ```html <template> <div> <draggable v-model="formElements"> <div v-for="(element, index) in formElements" :key="index"> <input v-if="element.type === 'input'" :type="element.inputType" :placeholder="element.placeholder"> <select v-else-if="element.type === 'select'"> <option v-for="(option, optionIndex) in element.options" :key="optionIndex">{{ option }}</option> </select> </div> </draggable> <button @click="addInput">Add Input</button> <button @click="addSelect">Add Select</button> <form> <div v-for="(element, index) in formElements" :key="index"> <input v-if="element.type === 'input'" :type="element.inputType" :placeholder="element.placeholder"> <select v-else-if="element.type === 'select'"> <option v-for="(option, optionIndex) in element.options" :key="optionIndex">{{ option }}</option> </select> </div> <button type="submit">Submit</button> </form> </div> </template> <script> import draggable from 'vuedraggable' export default { components: { draggable }, data() { return { formElements: [] } }, methods: { addInput() { this.formElements.push({ type: 'input', inputType: 'text', placeholder: 'Enter text' }) }, addSelect() { this.formElements.push({ type: 'select', options: ['Option 1', 'Option 2', 'Option 3'] }) } } } </script> ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值