
<script>

    function lazySelect2Ajax(ele, route, isMultiple = false, defaultValue = null, dataParams = {}) {
        ele.select2({
            allowClear: true,
            multiple: isMultiple,
            ajax: {
                url: route,
                dataType: 'json',
                delay: 100,
                data: function (params) {
                    return {
                        search: params.term || '',
                        page: params.page || 1,
                        ...dataParams
                    };
                },
                processResults: data => ({
                    results: data.items,
                    pagination: { more: data.more }
                }),
                cache: true
            },
            placeholder: `<?php echo e(__("Pleace select")); ?>`
        });

        if (defaultValue != null) {
            $.ajax({
                url: route,
                data: { id: defaultValue, ...dataParams },
                dataType: 'json',
                success: function (data) {
                    if (data.items.length > 0) {
                        if (!isMultiple) {
                            let item = data.items[0];
                            let option = new Option(item.text, item.id, true, true);
                            ele.append(option).trigger('change');
                        } else {
                            data.items.forEach(item => {
                                let option = new Option(item.text, item.id, true, true);
                                ele.append(option);
                            });
                            ele.trigger('change');
                        }
                    }
                }
            });
        }
    }

</script>

<?php /**PATH E:\xamp\htdocs\Gym Management System\resources\views/layouts/partials/scripts/_select2-scripts.blade.php ENDPATH**/ ?>