Skip to content

Commit

Permalink
Added js for to support choices.js
Browse files Browse the repository at this point in the history
  • Loading branch information
balajidharma committed Nov 5, 2024
1 parent 38382da commit 29c6669
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
31 changes: 31 additions & 0 deletions resources/js/field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
if(window.Choices)
{
document.addEventListener('DOMContentLoaded', function() {
const elements = document.querySelectorAll('[data-choices="1"]');
elements.forEach(element => {
const removeItemButton = element.getAttribute('data-choices-removeItemButton') != 0;
const maxItemCount = element.getAttribute('data-choices-maxItemCount') || -1;
const choicesElement = new Choices(element, {
removeItemButton: removeItemButton,
maxItemCount: maxItemCount
});
const dataUrl = element.getAttribute('data-choices-url');
if (dataUrl) {
choicesElement.passedElement.element.addEventListener(
'search', async (event) => {
try {
const items = await fetch(`${dataUrl}?search=${event.detail.value}&autocomplete=1`);
const data = await items.json();
// Clear existing choices
choicesElement.clearChoices();

// Set new choices
choicesElement.setChoices(data, 'value', 'label', true);
} catch (err) {
console.error(err);
}
});
}
});
});
}
2 changes: 1 addition & 1 deletion resources/views/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
<?php if ($options['wrapper'] !== false) { ?>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
4 changes: 4 additions & 0 deletions src/FormBuilderServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public function boot()
$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/laravel-form-builder'),
], 'views');

$this->publishes([
__DIR__.'/../resources/js' => resource_path('js/form-builder'),
], 'js');
}
}

Expand Down

0 comments on commit 29c6669

Please sign in to comment.