Skip to content

Instantly share code, notes, and snippets.

@jesse-spevack
Created April 14, 2021 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesse-spevack/b0c83a55b3eef4021c13a8831c1d93f7 to your computer and use it in GitHub Desktop.
Save jesse-spevack/b0c83a55b3eef4021c13a8831c1d93f7 to your computer and use it in GitHub Desktop.
<div
data-controller="multiselect"
data-multiselect-selected-items-value=[]
>
...
</div>
import { Controller } from "stimulus"
export default class extends Controller {
static values = {
selectedItems: Array // https://stimulus.hotwire.dev/reference/values#types
}
updateSelected(customSelectedEvent) {
// add the selected item to the selected items array
this.selectedItemsValue.push(customSelectedEvent.detail.value)
}
updateDeselected(customDeselectedEvent) {
// Find the index of the deselected item in the selected items array
const index = array.indexOf(customDeselectedEvent.detail.value);
// If the deselected item is in the array
if (index > -1) {
// Remove the deselected item from the array
this.selectedItemsValue.splice(index, 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment