Vue3.0 uses the vue-grid-layout plug-in to implement drag layout

Vue3.0 uses the vue-grid-layout plug-in to implement drag layout

1. Plugins

First, the plugin we chose is vue-grid-layout

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 demo , the page was blank and the console prompted that no subcomponents were found.

Change your thinking and use global import components instead of local import components.

3. Implementation

const 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! !
Here, after dragging, it is not determined whether each row is filled and some modules will be covered after dragging, resulting in blank areas, as shown below:

think:

We need to add a check to see if each row is fully filled.

4. Verification function

import { 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.
Check in the callback function after each drag is completed

    /**
     * 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:
  • Vue implements multi-column layout drag
  • Vue3 draggable left and right panel split component implementation
  • Implementation of Vue code splitting (codesplit)
  • Vue implements drag-and-drop split layout

<<:  Introduction to Semantic HTML Tags

>>:  Summary of flex layout compatibility issues

Recommend

MySql 8.0.16-win64 Installation Tutorial

1. Unzip the downloaded file as shown below . 2. ...

Implementation of mounting NFS shared directory in Docker container

Previously, https://www.jb51.net/article/205922.h...

Simple web page code used in NetEase blog

How to use the code in NetEase Blog: First log in...

Introduction to Vue3 Composition API

Table of contents Overview Example Why is it need...

Steps to install MySQL using Docker under Linux

As a tester, you may often need to install some s...

Realize three-level linkage of year, month and day based on JavaScript

This article shares the specific code for JavaScr...

JS uses map to integrate double arrays

Table of contents Preface Simulating data Merged ...

Reasons and solutions for not being able to detect array changes in Vue2

Table of contents Workaround Why can't I moni...

How to implement Ajax concurrent request control based on JS

Table of contents Preface Ajax serial and paralle...

Linux service monitoring and operation and maintenance

Table of contents 1. Install the psutil package S...

Comprehensive explanation of CocosCreator hot update

Table of contents Preface What is Hot Change Coco...

Meta viewport makes the web page full screen display control on iPhone

In desperation, I suddenly thought, how is the Sin...

SVG button example code based on CSS animation

The specific code is as follows: <a href="...

How to implement the singleton pattern in Javascript

Table of contents Overview Code Implementation Si...