Home > Web Front-end > Vue.js > How to implement draggable layout in Vue?

How to implement draggable layout in Vue?

WBOY
Release: 2023-06-25 08:48:33
Original
7899 people have browsed it

With the continuous development of Web front-end technology, more and more developers are beginning to use the SPA (Single Page Application) architecture to build applications. As one of the most popular web front-end frameworks currently, Vue provides a rich component library and tools to make it easier to build SPA applications. Vue also provides some convenient solutions when it comes to implementing draggable layouts.

1. Using Vue-Grid-Layout

Vue-Grid-Layout is a responsive grid layout system based on Vue. It provides a set of components that allow you to easily implement draggable, resizable grid layouts while supporting gesture dragging and zooming on mobile devices. Using Vue-Grid-Layout is very simple. You first need to introduce it in the project:

npm install vue-grid-layout -S
Copy after login

Then use it in the Vue component, as follows:

<template>
  <vue-grid-layout :layout="layout" :col-num="12" :row-height="30" :is-draggable="true"
                   :is-resizable="true" :vertical-compact="false" :margin="[10, 10]"
                   :use-css-transforms="true" :auto-size="true">
    <vue-grid-item v-for="item in layout" :key="item.i" :x="item.x" :y="item.y" :w="item.w"
                   :h="item.h">
      <div class="grid-item-content">{{ item.i }}</div>
    </vue-grid-item>
  </vue-grid-layout>
</template>
<script>
import { VueGridLayout, VueGridItem } from 'vue-grid-layout';

export default {
  components: { VueGridLayout, VueGridItem },
  data() {
    return {
      layout: [
        { i: 'item1', x: 0, y: 0, w: 1, h: 2 },
        { i: 'item2', x: 1, y: 0, w: 1, h: 2 },
        { i: 'item3', x: 2, y: 0, w: 1, h: 2 },
        { i: 'item4', x: 3, y: 0, w: 1, h: 2 },
      ]
    }
  },
}
</script>
Copy after login

In the above code, we define A VueGridLayout component and binds the layout array to its layout property, where each item represents a grid item, including its id (i), x, y coordinates, and width (w) and height (h). We can also set the col-num attribute to specify the number of columns in the grid, the row-height attribute to set the height of each row, and the is-draggable and is-resizable attributes to specify whether the grid items can be dragged and resized respectively. Vue-Grid-Layout also provides various customization options that can be configured as needed.

2. Use Vue-Draggable

Vue-Draggable is another popular Vue plug-in, which allows you to implement draggable lists in Vue, such as drag-and-drop sorting and drag-and-drop changes. Parent etc. It is very suitable for implementing a drag-and-drop task panel similar to Trello. Using Vue-Draggable is also very simple. You need to introduce it first:

npm install vuedraggable --save
Copy after login

Then use it in the Vue component, as shown below:

<template>
  <draggable v-model="list" :options="dragOptions">
    <div v-for="(item, index) in list" :key="item.id">
      <h3 class="title">{{ item.title }}</h3>
      <p class="content">{{ item.content }}</p>
    </div>
  </draggable>
</template>
<script>
  import draggable from 'vuedraggable';

  export default {
    components: { draggable },
    data() {
      return {
        list: [
          { id: 1, title: 'Title 1', content: 'Content 1' },
          { id: 2, title: 'Title 2', content: 'Content 2' },
          { id: 3, title: 'Title 3', content: 'Content 3' },
          { id: 4, title: 'Title 4', content: 'Content 4' },
          { id: 5, title: 'Title 5', content: 'Content 5' },
        ],
        dragOptions: {
          group: {
            name: 'task',
            pull: 'clone',
            put: false
          },
          sort: false,
          animation: 150,
        },
      }
    },
  }
</script>
Copy after login

In the above code, we define a draggable component , and bind the list array to its v-model, each item represents a list item, including its id, title and content. We can also set various custom options through the options attribute, such as the group attribute to set drag and drop grouping, the sort attribute to set whether sorting is enabled, the animation attribute to set the animation duration, etc.

Summary

It is common to use Vue to implement draggable layout. Vue-Grid-Layout and Vue-Draggable are two good solutions. Vue-Grid-Layout is suitable for grid layout, which can realize draggable and resizable grid layout, and is suitable for building various complex interactive applications. Vue-Draggable is mainly used for drag-and-drop lists, and is very effective in implementing drag-and-drop task panels like Trello. Choosing the right solution depends on the specific use case, both are simple and flexible to use.

The above is the detailed content of How to implement draggable layout in Vue?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template