Wiidede's blog Wiidede's blog
  • 前端
  • Python
  • 算法
  • 生活
  • 其他
  • 分类
  • 标签
  • 归档
  • 关于我
  • 赞赏
  • 我的小站 (opens new window)
GitHub (opens new window)

Wiidede

小的的写前端
  • 前端
  • Python
  • 算法
  • 生活
  • 其他
  • 分类
  • 标签
  • 归档
  • 关于我
  • 赞赏
  • 我的小站 (opens new window)
GitHub (opens new window)
  • 整理一些css样式
  • vue隔代组件层层动态插槽并且附带数据
  • vue判断字符串是否溢出来显示弹窗、解决el-table tooltip 内过多导致无法显示,内容闪烁
  • 整理一些js写法
  • ElementUI timePicker 增加此刻按钮 引发的dom操作的学习
  • 毕业设计(水表识别)前端知识整理
  • html小知识
  • axios请求api然后下载文件
  • vue3+ts根据高度改变元素的透明度
  • vue3 + ElementPlus 换肤方案(Css变量)
  • Moment的一些使用方法
  • echarts基础vue组件
  • element UI el-date-picker 年月日切换组件
  • 可以不选择的el-radio单选框
  • vue的小技巧总结
  • 全局动态权限判断(Vue指令)
  • vue-anchor 探索
  • Deep Dive with Evan You 笔记
  • 前端基础知识查漏补缺
  • WebPack 知识总结
  • 我写的一些可以日后参考的代码
  • 接口变化后,封装接口函数,改变返回内容
  • 项目组件整理
  • 前端框架设计想发
  • 全局进度条
  • 带有token的图片vue组件:authImg,使用axios下载图片
  • 前端npm包推荐
  • 给ElInputNumber添加prefix
  • ElPagination添加页数总数
  • el-tab做成chrome类似的tab样式
  • vue-grid-layout-组件配置
  • 项目数据字典封装
  • 图表组件响应式探索
  • ElementPlus表格table列自动合并composition
  • 简单的curd组件封装
  • ElementPlus表格自定义合计列composition
  • 一些处理表格数据composition api
  • div内容溢出后,内容向左悬浮,vue组件封装
  • 文本数字溢出后,按比例缩小,vue组件封装
  • 表格使用async-validator检验composition
  • ElementPlus Form一些简单的组件整合
  • arco-design快速使用tailwind的颜色、unocss动态颜色
  • 前端
wiidede
2022-09-29

vue-grid-layout-组件配置

# vue-grid-layout 组件配置

可以自定义各组件的布局,可以从外部拖入

<script setup lang="ts">
import GridItem from '@/components/GridLayout/components/grid-item.vue'
import GridLayout from '@/components/GridLayout/components/grid-layout.vue'

withDefaults(defineProps<{
  edit: boolean
}>(), {
  edit: false,
})

const gridLayoutRef = ref()
const gridItemRefs = ref<any[]>([])

const componentList: ComponentItem[] = [
  { w: 2, h: 4, i: 'xx', l: 'xxx' },
]

const componentMap = {
  xx: defineAsyncComponent(() => import('~/xx')),
  dragging: null,
}
type ComponentKeys = keyof typeof componentMap
interface ComponentItem {
  x?: number
  y?: number
  w?: number
  h?: number
  i: ComponentKeys
  l?: string
}

const colNum = 9
const layout = ref<ComponentItem[]>([])
const setLayout = (newLayout: ComponentItem[]) => {
  layout.value = newLayout
}

// drag
const currentComponent = ref<ComponentItem>()
const DragPos: Partial<ComponentItem> = {}
const { x, y } = useMouse({ type: 'client' })

const allowDrop = (e: DragEvent) => {
  e.preventDefault()
}
const deleteLayout = (i: ComponentItem['i']) => {
  const draggingIndex = layout.value.findIndex(obj => obj.i === i)
  if (draggingIndex !== -1) {
    layout.value.splice(draggingIndex, 1)
  }
}
const drag = (e: DragEvent, comp: ComponentItem) => {
  currentComponent.value = comp
  const parentRect = gridLayoutRef.value!.$el.getBoundingClientRect()
  let mouseInGrid = false
  if (((x.value > parentRect.left) && (x.value < parentRect.right)) && ((y.value > parentRect.top) && (y.value < parentRect.bottom))) {
    mouseInGrid = true
  }
  if (mouseInGrid === true && (layout.value.findIndex(item => item.i === 'dragging')) === -1) {
    layout.value.push({
      ...comp,
      i: 'dragging',
      x: (layout.value.length * 2) % (colNum),
      y: layout.value.length + (colNum), // puts it at the bottom
    })
  }
  const index = layout.value.findIndex(item => item.i === 'dragging')
  if (index !== -1) {
    const itemRef = gridItemRefs.value.find(item => item.i === 'dragging')
    if (itemRef) {
      itemRef.dragging = { top: y.value - parentRect.top, left: x.value - parentRect.left }
      const newPos = itemRef.calcXY(y.value - parentRect.top, x.value - parentRect.left)

      if (mouseInGrid === true) {
        gridLayoutRef.value.dragEvent('dragstart', 'dragging', newPos.x, newPos.y, comp.h, comp.w)
        DragPos.x = layout.value[index].x
        DragPos.y = layout.value[index].y
      }
      if (mouseInGrid === false) {
        gridLayoutRef.value.dragEvent('dragend', 'dragging', newPos.x, newPos.y, comp.h, comp.w)
        deleteLayout('dragging')
      }
    }
  }
}
const dragend = (e: DragEvent) => {
  deleteLayout('dragging')
  const parentRect = gridLayoutRef.value!.$el.getBoundingClientRect()
  let mouseInGrid = false
  if (((x.value > parentRect.left) && (x.value < parentRect.right)) && ((y.value > parentRect.top) && (y.value < parentRect.bottom))) {
    mouseInGrid = true
  }
  if (mouseInGrid === true) {
    const final: ComponentItem = {
      ...currentComponent.value!,
      x: DragPos.x,
      y: DragPos.y,
    }
    layout.value.push(final)
    gridLayoutRef.value.dragEvent('dragend', final.i, final.x, final.y, final.h, final.w)
  }
}

defineExpose({
  setLayout,
})
</script>

<template>
  <div
    @dragenter="allowDrop"
    @dragover="allowDrop"
  >
    <GridLayout
      ref="gridLayoutRef"
      v-model:layout="layout"
      flex-1
      :col-num="colNum"
      :row-height="10"
      :is-draggable="edit"
      :is-resizable="false"
      :is-mirrored="false"
      :margin="[25, 25]"
    >
      <GridItem
        v-for="item in layout"
        v-show="item.i !== 'dragging'"
        :key="item.i"
        ref="gridItemRefs"
        class="relative"
        :x="item.x"
        :y="item.y"
        :w="item.w"
        :h="item.h"
        :i="item.i"
      >
        <component :is="componentMap[item.i]" v-if="componentMap[item.i]" class="grid-item-content" />
        <el-icon v-if="edit" class="absolute right-8px top-8px cursor-pointer" @click="deleteLayout(item.i)">
          <Close />
        </el-icon>
      </GridItem>
    </GridLayout>
  </div>
  <div v-if="edit" class="component-list flex-col-center justify-center">
    <div
      v-for="comp in componentList.filter((comp) => !layout.find((item) => item.i === comp.i))"
      :key="comp.i"
      class="component-item"
      draggable="true"
      unselectable="on"
      @drag="drag($event, comp)"
      @dragend="dragend"
    >
      <div class="i-ic-baseline-grid-view mr4px mt1px" />
      <span>{{ comp.l }}</span>
    </div>
  </div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#前端#Vue#Vue组件
上次更新: 2023/06/01, 12:40:50

← el-tab做成chrome类似的tab样式 项目数据字典封装→

Theme by Vdoing | Copyright © 2021-2023 Wiidede | Website use MIT License | Article content & logo use CC-BY-SA-4.0 License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式