1. Plugins First, the plugin we chose is npm i vue-grid-layout --save Official website: https://jbaysolutions.github.... 2. Interlude After installing the dependencies, I found that the project could be started. According to the official website Change your thinking and use global import components instead of local import components. 3. Implementationconst layout = ref<LayoutItem[]>([ { x: 0, y: 0, w: 1, h: 1, i: 0 }, { x: 1, y: 0, w: 2, h: 1, i: 1 }, { x: 0, y: 1, w: 2, h: 1, i: 2 }, { x: 0, y: 2, w: 3, h: 1, i: 3 }, { x: 2, y: 1, w: 1, h: 1, i: 4 }, ]); <grid-layout :layout="layout" :col-num="3" :row-height="240" :is-draggable="true" :is-resizable="true" :is-mirrored="false" :maxRows="3" :vertical-compact="true" :margin="[10, 10]" :use-css-transforms="true" > <grid-item v-for="item in layout" :x="item.x" :y="item.y" :w="item.w" :h="item.h" :i="item.i" :key="item.i" @moved="onItemMoved" >{{ item.i }}</grid-item> </grid-layout> Effect: but! ! think: We need to add a check to see if each row is fully filled. 4. Verification functionimport { LayoutItem } from '../types/index'; import { cloneDeep } from 'lodash' /** * Check if the layout is legal* 1. Deep copy the array to avoid contaminating the original array* 2. Get the maximum value of y for traversal* 3. Get each y subarray and sort it in ascending order of x* 4. If the array length is 1, determine whether w is equal to the maximum x * 5. If the length of the array is not 1, traverse the array to determine whether the w of each element is equal to the x of the next element. Accumulate w to determine whether the sum is equal to the maximum x. * 6. If legal, return false * @param list * @returns */ export const verifyLayout = (list: Array<LayoutItem>): boolean => { let yList = list.map(item => { return item.y }); yList = yList.sort((a, b) => { return a - b }); console.log(list); const newArr = cloneDeep(list); let flag = false; const maxY = yList[yList.length - 1]; const maxX = 3; console.log(maxY); for (let i = 0; i <= maxY; i++) { let arr = newArr.filter((item: LayoutItem) => { return item.y === i; }); console.log(arr, arr.length); if (arr && arr.length > 1) { console.log('Multiple-------------------', i); let calValue = 0; arr = arr.sort((a: LayoutItem, b: LayoutItem) => { return ax - bx }) arr.forEach((childItem: LayoutItem, index: number) => { calValue += childItem.w; console.log('calValue--------------', calValue, index); if (index !== arr.length - 1 && calValue !== arr[index + 1].x) { flag = true; } if (index === arr.length - 1 && calValue !== maxX) { flag = true; } }) } else { console.log('There is only one------------------', i); if (arr[0].w !== maxX) { flag = true } } } console.log(flag); return flag; } The idea is my comment on the function. /** * Drag completion event* 1. Store the previous data in the history data* 2. Then store the data after the move in the nowlayout data*/ const onItemMoved = () => { console.log('moved--------------------') historyDataList.value.push(cloneDeep(nowLayoutData.value)); nowLayoutData.value = cloneDeep(layout.value); // const flag = verifyLayout(layout.value); // if (flag) { // goBack() // } }; const goBack = () => { console.log(historyDataList.value[historyDataList.value.length - 1]); layout.value = historyDataList.value[historyDataList.value.length - 1]; nowLayoutData.value = cloneDeep(layout.value); historyDataList.value.pop(); } In this way, every time we drag and drop, if the verification is not legal, we will roll back to ensure that every row is filled! ! ! ! This is the end of this article about Vue3.0 using the vue-grid-layout plug-in to implement drag-and-drop layout. For more related Vue3 using the vue-grid-layout plug-in to implement drag-and-drop layout content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Introduction to Semantic HTML Tags
>>: Summary of flex layout compatibility issues
Table of contents 1. Shared and Exclusive Locks 2...
The code demonstrates horizontal merging: <!DO...
This article describes how to use Docker's mu...
After configuring VIP, the error message that app...
Why are the scroll bars of the browsers and word ...
<br />This example mainly studies two parame...
In this chapter, we will start to operate redis i...
MySQL installation tutorial, for your reference, ...
This article introduces the flex layout to achiev...
Although W3C has established some standards for HT...
When the front-end and back-end interact, sometim...
Experimental environment A minimally installed Ce...
one. wget https://dev.mysql.com/get/mysql57-commu...
It is no exaggeration to say that hyperlinks conne...
On a Linux computer, there are two times, one is ...