Created
April 14, 2021 22:22
-
-
Save jesse-spevack/b0c83a55b3eef4021c13a8831c1d93f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div | |
data-controller="multiselect" | |
data-multiselect-selected-items-value=[] | |
> | |
... | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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