拿來(lái)就用vue-gird-layout組件封裝示例
先來(lái)效果圖展示

展示

拖拽尺寸

拖拽后

移動(dòng)
組件封裝
大家這塊直接復(fù)制使用就OK
<template>
<grid-layout
:layout="layout"
:col-num="colNum"
:row-height="rowHeight"
:is-mirrored="isMirrored"
:vertical-compact="verticalCompact"
:margin="margin"
:use-css-transforms="useCssTransforms"
:useStyleCursor="useStyleCursor"
:is-draggable="isDraggable"
:is-resizable="isResizable"
@layout-updated="layoutUpdatedEvent"
>
<grid-item v-for="(item, index) in layout"
:x="item.x"
:y="item.y"
:w="item.w"
:h="item.h"
:i="item.i"
:minW="minW"
:minH="minH"
:key="index"
@resized="resize(item.i)">
<slot name="chart" :item="item" :index="index"></slot>
</grid-item>
</grid-layout>
</template>
<script>
import VueGridLayout from 'vue-grid-layout';
// import Chart from "@/components/chart/index.vue";
export default {
components: {
GridLayout: VueGridLayout.GridLayout,
GridItem: VueGridLayout.GridItem,
// Chart
},
props: {
// 布局?jǐn)?shù)據(jù)
layout: {
type: Array,
default: () => []
},
// 是否可拖拽
isDraggable: {
type: Boolean,
default: true
},
// 是否可改變大小
isResizable: {
type: Boolean,
default: true
},
// 多少列
colNum: {
type: Number,
default: 12
},
// 每行得高度
rowHeight: {
type: Number,
default: 30
},
// 是否可鏡像反轉(zhuǎn)
isMirrored: {
type: Boolean,
default: false
},
// 標(biāo)識(shí)布局是否垂直壓縮
verticalCompact: {
type: Boolean,
default: true
},
// 定義柵格中的元素邊距
margin: {
type: Array,
default: () => [10,10]
},
// 標(biāo)識(shí)是否使用CSS屬性 transition-property: transform;
useCssTransforms: {
type: Boolean,
default: true
},
// 是否使用動(dòng)態(tài)鼠標(biāo)指針樣式
useStyleCursor: {
type: Boolean,
default: true
},
// 元素最小寬
minW: {
type: Number,
default: 1
},
// 最小高
minH: {
type: Number,
default: 1
},
show: {
type: Boolean,
default: false
}
},
data() {
return {
height: '',
option: {},
gridShow: false
}
},
methods: {
layoutUpdatedEvent(newLayout) {
this.$emit('layoutUpdatedEvent',newLayout)
},
// 更新resize
resize(i) {
this.$emit('resize',i)
}
}
}
</script>
<style lang="scss" scoped>
:deep(.el-card__body) {
height: 100%;
}
</style>組件的調(diào)用
(主要是使用這塊 上面的直接復(fù)制即可)
<template>
<div>
<gridCanvas :layout="layout" @layoutUpdatedEvent="layoutUpdatedEvent" @resize="resize">
<template #chart="{item,index}">
<el-card v-if="show" ref="elCard" style="height: 100%" :key="item.i">
<Chart :ref="'echarts'+item.i" :chartsData="item.data" :radioShow="false" :height="'100%'" :id="index"></Chart>
</el-card>
</template>
</gridCanvas>
</div>
</template>
<script>
import gridCanvas from "@/components/gridCanvas/index.vue";
import Chart from "@/components/chart/index.vue";
export default {
components: {
gridCanvas,
Chart
},
data() {
return {
show: false,
layout: [
{"x":0,"y":0,"w":6,"h":10,"i":"0",
data: {
descData: ["廣東省", "江蘇省", "北京市", "浙江省", "山東省", "四川省", "河南省", "上海市12345", "河北省", "其他", "福建省", "安徽省", "湖北省", "遼寧省"],
dis_type: "barh",
label_desc: "最近一次測(cè)試",
label_id: 5,
subtext: "99.99%",
text: "圖標(biāo)1",
xData: [2211, 333, 372, 226, 248, 218, 103, 199, 174, 177, 146, 148, 171, 162],
yData: ["廣東省", "江蘇省", "北京市", "浙江省", "山東省", "四川省", "河南省", "上海市", "河北省", "其他", "福建省", "安徽省", "湖北省", "遼寧省"],
}
},
{"x":4,"y":0,"w":4,"h":8,"i":"1",
data: {
descData: ["廣東省", "江蘇省", "北京市", "浙江省", "山東省", "四川省", "河南省", "上海市12345", "河北省", "其他", "福建省", "安徽省", "湖北省", "遼寧省"],
dis_type: "pie",
label_desc: "最近二次測(cè)試",
label_id: 5,
subtext: "99.99%",
text: "圖標(biāo)2",
xData: [810, 357, 372, 226, 248, 218, 193, 189, 174, 477, 146, 148, 131, 162],
yData: ["廣東省", "江蘇省", "北京市", "浙江省", "山東省", "四川省", "河南省", "上海市", "河北省", "其他", "福建省", "安徽省", "湖北省", "遼寧省"],
}
},
{"x":0,"y":5,"w":4,"h":8,"i":"6",
data: {
descData: ["廣東省", "江蘇省", "北京市", "浙江省", "山東省", "四川省", "河南省", "上海市12345", "河北省", "其他", "福建省", "安徽省", "湖北省", "遼寧省"],
dis_type: "bar",
label_desc: "最近三次測(cè)試",
label_id: 5,
subtext: "99.99%",
text: "圖標(biāo)3",
xData: [810, 377, 342, 286, 274, 219, 190, 199, 174, 147, 146, 438, 171, 162],
yData: ["廣東省", "江蘇省", "北京市", "浙江省", "山東省", "四川省", "河南省", "上海市", "河北省", "其他", "福建省", "安徽省", "湖北省", "遼寧省"],
}
},
]
}
},
methods: {
// 拖拽之后觸發(fā)
layoutUpdatedEvent(newLayout) {
console.log(newLayout)
},
resize(i) {
this.$nextTick(() => {
const chart = eval(`this.$refs.echarts${i}`)
if(chart) {
chart.resize()
}
})
}
},
mounted() {
setTimeout(() => {
this.show = true
})
}
}
</script>
<style lang="scss" scoped>
:deep(.el-card__body) {
height: 100%;
}
</style>使用講解
直接引用我們自己封裝的組件 代碼中chart組件為我自己封裝的echarts組件,后續(xù)我會(huì)更新,你只需講chart組件換為你自己封裝的組件 其中#chart為插槽參數(shù) (其實(shí)就是每塊的數(shù)據(jù))layout變量為grid的核心數(shù)據(jù),分別代表x軸坐標(biāo)y軸坐標(biāo)寬度和高度其中data為你需要渲染的數(shù)據(jù) 還不能使用的
相關(guān)文章
vue3 setup語(yǔ)法糖下的定時(shí)器的使用與銷毀
如果在組件中需要使用定時(shí)器,注意在銷毀組件的時(shí)候,要對(duì)定時(shí)器進(jìn)行銷毀,否則時(shí)間長(zhǎng)了會(huì)導(dǎo)致頁(yè)面卡頓,這篇文章給大家介紹vue3 setup語(yǔ)法糖下的定時(shí)器的使用與銷毀的知識(shí),感興趣的朋友一起看看吧2024-02-02
vue項(xiàng)目使用微信公眾號(hào)支付總結(jié)及遇到的坑
這篇文章主要介紹了vue項(xiàng)目使用微信公眾號(hào)支付總結(jié),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
Vue移動(dòng)端用淘寶彈性布局lib-flexible插件做適配的方法
這篇文章主要介紹了Vue移動(dòng)端用淘寶彈性布局lib-flexible插件做適配的操作方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
vue-cli項(xiàng)目如何使用vue-resource獲取本地的json數(shù)據(jù)(模擬服務(wù)端返回?cái)?shù)據(jù))
這篇文章主要介紹了vue-cli項(xiàng)目如何使用vue-resource獲取本地的json數(shù)據(jù)(模擬服務(wù)端返回?cái)?shù)據(jù)),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
如何使用vuejs實(shí)現(xiàn)更好的Form validation?
如何使用vuejs實(shí)現(xiàn)更好的Form validation?這篇文章主要介紹了vue-form插件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Element-UI?el-table對(duì)循環(huán)產(chǎn)生的空白列賦默認(rèn)值方式
這篇文章主要介紹了Element-UI?el-table對(duì)循環(huán)產(chǎn)生的空白列賦默認(rèn)值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue3中reactive變量重新賦值無(wú)法響應(yīng)的3種處理方法
這篇文章主要給大家介紹了關(guān)于Vue3中reactive變量重新賦值無(wú)法響應(yīng)的3種處理方法,在Vue3中可以使用reactive函數(shù)將一個(gè)普通對(duì)象轉(zhuǎn)換為響應(yīng)式對(duì)象,需要的朋友可以參考下2023-08-08
laravel5.3 vue 實(shí)現(xiàn)收藏夾功能實(shí)例詳解
這篇文章主要介紹了laravel5.3 vue 實(shí)現(xiàn)收藏夾功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-01-01
Vue無(wú)限滑動(dòng)周選擇日期的組件的示例代碼
這篇文章主要介紹了Vue無(wú)限滑動(dòng)周選擇日期的組件的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07

