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

Add pre-selected option to selecto? #129

Open
quankhuc opened this issue Apr 25, 2023 · 4 comments
Open

Add pre-selected option to selecto? #129

quankhuc opened this issue Apr 25, 2023 · 4 comments
Labels
question Further information is requested

Comments

@quankhuc
Copy link

Environments

  • Framework name: NuxtJS
  • Framework version: Nuxt 3
  • Component name: vue3-selecto
  • Component version: 1.8.2
  • Testable Address(optional): nuxt-catoptric-project.vercel.app

Description

I wonder if there is a way I can pass in an array of the coordinate of each box and have it to be pre-selected. For example, if I have a selection area of 10 boxes, I could target a box with its id and make it to be selected. As I look at the documentation, I don't think this option is available yet.

For example, I can configure selecto so that the box "1" to be already selected at the beginning.
Screenshot 2023-04-25 at 12 57 27 AM

Thank you for making such an awesome package so far.

@daybrush daybrush added the question Further information is requested label Apr 25, 2023
@daybrush
Copy link
Owner

@quankhuc

selecto has setSelectedTargets method.

If you want it to be preselected, call setSelectedTargetes(pre-selected-targetes);.

@quankhuc
Copy link
Author

Would you mind providing a snippet of how you hook it up in Selecto component? Thank you!

@quankhuc
Copy link
Author

quankhuc commented Apr 27, 2023

I actually could not use the setSelectedTargers() so I decided to add selected class into the div by myself. Here is how I did it if you are curious:
https://github.com/quankhuc/nuxt-catoptric-project/blob/1ffef115505992475e17f352da6f3c1be4907d22/components/Dashboard/PanelModal.vue#L205

@daybrush
Copy link
Owner

daybrush commented May 2, 2023

@quankhuc

I used setSelectedTargets like this:

<script setup lang="ts">
import { onMounted, ref } from "vue";
import Selecto from "vue3-selecto";
import { SelectedTargets } from "selecto";

const selecto = ref<Selecto>();

function onSelect(e: SelectedTargets) {
  console.log(e);
  e.added.forEach(el => {
    el.style.background = "#f55";
  });
  e.removed.forEach(el => {
    el.style.background = "transparent";
  });
}
onMounted(() => {
  const result = selecto.value!.setSelectedTargets([document.querySelector("img")!]);

  onSelect(result);
});

const dragContainer = typeof window !== "undefined" && window as Window;
</script>

<template>
  <div>
    <a href="https://vitejs.dev" target="_blank">
      <img src="/vite.svg" class="logo" alt="Vite logo" />
    </a>
    <a href="https://vuejs.org/" target="_blank">
      <img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
    </a>
  </div>
  <Selecto ref="selecto" @select-end="onSelect" :drag-container="dragContainer" :selectable-targets="['img']"/>
</template>

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

No branches or pull requests

2 participants