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

Summary of MySQL InnoDB locks

Table of contents 1. Shared and Exclusive Locks 2...

Split and merge tables in HTML (colspan, rowspan)

The code demonstrates horizontal merging: <!DO...

How to reduce image size using Docker multi-stage build

This article describes how to use Docker's mu...

Solution to the problem of mysql master-slave switch canal

After configuring VIP, the error message that app...

Why is the scroll bar on the web page set on the right?

Why are the scroll bars of the browsers and word ...

Style trigger effect of web page input box

<br />This example mainly studies two parame...

Detailed explanation of persistent storage of redis under docker

In this chapter, we will start to operate redis i...

MySQL installation tutorial under Centos7

MySQL installation tutorial, for your reference, ...

Flex layout achieves fixed number of rows per line + adaptive layout

This article introduces the flex layout to achiev...

Web design reference firefox default style

Although W3C has established some standards for HT...

Get / delete method to pass array parameters in Vue

When the front-end and back-end interact, sometim...

How to configure Nginx virtual host in CentOS 7.3

Experimental environment A minimally installed Ce...

Tutorial analysis of quick installation of mysql5.7 based on centos7

one. wget https://dev.mysql.com/get/mysql57-commu...

XHTML Getting Started Tutorial: XHTML Hyperlinks

It is no exaggeration to say that hyperlinks conne...

How to modify the time zone and time in Ubuntu system

On a Linux computer, there are two times, one is ...