Ask Your Question
0

What is the method for utilizing vuedraggable in a grid comprising n x n cells?

asked 2023-05-02 16:46:13 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-02 16:51:02 +0000

bukephalos gravatar image

The method for utilizing vuedraggable in a grid comprising n x n cells involves creating a data array of size n x n and linking each cell in the grid to a corresponding element in the data array.

Then, the vuedraggable component can be used to allow users to drag and drop items within the grid. The data array will be updated with the new order of the grid after each drag and drop event.

To implement this, we can use a v-for loop to iterate over the elements in the data array and create a draggable element for each cell in the grid. We can also set the grid's display using CSS to create the desired layout.

Additionally, we can use the vuedraggable's sortable prop to ensure that items are only allowed to be moved within the grid and not outside of it.

Example code:

<template>
  <div class="grid-container">
    <div v-for="(cell, index) in gridData" :key="index" class="grid-item" vuedraggable="sortable">
      {{ cell }}
    </div>
  </div>
</template>

<script>
import draggable from 'vuedraggable'

export default {
  components: {
    draggable
  },
  data() {
    return {
      gridData: [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
      ],
      sortable: {
        group: 'grid',
        disabled: false,
        animation: 150
      }
    }
  }
}
</script>

<style>
.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.grid-item {
  background-color: #ddd;
  border: 1px solid #000;
  text-align: center;
  padding: 10px;
}
</style>
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-02 16:46:13 +0000

Seen: 16 times

Last updated: May 02 '23