Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to move two table rows in one drag? #162

Open
web-mm opened this issue Jul 18, 2024 · 1 comment
Open

How to move two table rows in one drag? #162

web-mm opened this issue Jul 18, 2024 · 1 comment

Comments

@web-mm
Copy link

web-mm commented Jul 18, 2024

I have a condition where the second row act as an accordion of first row. When I drag any of these two rows, I want to drag both the rows and drop them together. How can I achieve it?

In addition to this, no row should be placed in between row and accordion.

<template>
  <VueDraggable
    v-model="list"
    target=".sort-target"
    animation="150"
    @start="onStart"
    @end="onEnd"
  >
    <table>
      <thead>
        <tr>
          <th>Id</th>
          <th>Name</th>
        </tr>
      </thead>
      <tbody class="sort-target">
        <template v-for="(item, index) in list" :key="item.id">
          <!-- Both tr should move in one drag -->
          <tr
            @click="toggleAccordion(item.id)"
            :class="{
              'row-selected': expandedRows[item.id],
            }"
          >
            <td>{{ item.id }}</td>
            <td>{{ item.name }}</td>
          </tr>

          <!-- No item should be dragged here -->

          <tr v-if="expandedRows[item.id]" class="accordion-row">
            <td colspan="2">
              <div>some content here {{ index }}</div>
            </td>
          </tr>
        </template>
      </tbody>
    </table>
  </VueDraggable>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { VueDraggable, DraggableEvent } from 'vue-draggable-plus';

const list = ref([
  {
    name: 'Joao',
    id: 1,
  },
  {
    name: 'Jean',
    id: 2,
  },
  {
    name: 'Johanna',
    id: 3,
  },
  {
    name: 'Juan',
    id: 4,
  },
]);

const expandedRows = reactive<Record<number, boolean>>({});

//function to expand and contract the accordion
const toggleAccordion = (id: number) => {
  expandedRows[id] = !expandedRows[id];
};

function onStart(event: DraggableEvent) {}
function onEnd(event: DraggableEvent) {}
</script>
<style scoped>
table {
  border-collapse: collapse;
  table-layout: fixed; /* Ensures the widths specified are respected */
}

th,
td {
  border: 1px solid #000;
  padding: 8px;
  text-align: left;
}
</style>

https://stackblitz.com/edit/vitejs-vite-vvgac6?file=src%2FApp.vue

@bufgix
Copy link

bufgix commented Aug 10, 2024

Did you find any solutions @web-mm ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants