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 export of entire or single table data

Export a single table mysqldump -u user -p dbname...

Summary of problems encountered when installing docker on win10 home version

Docker download address: http://get.daocloud.io/#...

Three ways to avoid duplicate insertion of data in MySql

Preface In the case of primary key conflict or un...

How to view and terminate running background programs in Linux

Linux task management - background running and te...

The process of deploying and running countly-server in docker in win10

I have just come into contact with and become fam...

Docker advanced method of rapid expansion

1. Command method Run the nginx service in the cr...

How to encapsulate WangEditor rich text component in Angular

The rich text component is a very commonly used c...

4 functions implemented by the transform attribute in CSS3

In CSS3, the transform function can be used to im...

Native JS to achieve drag photo wall

This article shares with you a draggable photo wa...

Interpreting MySQL client and server protocols

Table of contents MySQL Client/Server Protocol If...

Detailed troubleshooting of docker.service startup errors

Execute the following command to report an error ...

jQuery implements all selection and reverse selection operation case

This article shares the specific code of jQuery t...

SQL Practice Exercise: Online Mall Database User Information Data Operation

Online shopping mall database-user information da...