Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LongSu's avatar

How to access to Alpinejs data from javascript in livewire or something like that?

Hello all. How are you?

I want to get help from you. Here is my workflow.

I have Company page in admin panel. This page has a datatable with Edit and Delete button on each row. For example, if I click Delete button, it shows a confirm modal, so I click Delete button on modal. Then, it calls delete() function of Company livewire component, and the delete() function deletes a company record by id. All is working fine already. ☝

But what I want to do is, after deleting a company record, I want to close a modal automatically. I am not sure how to do this. Here is my code snippets.

[In company.blade.php]
// Delete button,  this button is in for loop to draw datatable rows.
<button  type="button" class="border-0 text-red-700 p-2">Delete</button>

// Delete modal
<x-modal name="delete-company" :show="$errors->isNotEmpty()" focusable>
	...
</x-modal>

// Javascript code
@script
<script>
    // This listener will be fired from Company.php component
    $wire.on('company-deleted', (event) => {
        // Not sure how??  This is what I want to do.
		// Well, I thought, I can access to :show attribute of <x-modal> tag, but not sure.
    });
</script>
@endscript

[In Company.php component]   
class Company extends Component
{
	public function delete()
    {
        if ($this->companyId)
        {
            try
            {
                CompanyModel::where('id', $this->companyId)->delete();
                $this->dispatch('company-deleted');    // I have a listener in blade file.
            }
            catch (Exception $e)
            {
                $this->dispatch('company-delete-failed');
            }
        }
        else
        {
            $this->dispatch('company-delete-failed');
        }
    }
}

Actually, I don't think, this way is correct. I assure there will be more professional way. Please help me, thank you so much!

(Additional help required if someone knows) Is there a way to make a modal professionally that I can use with datatable? I tried to use this package. wire-elements/modal on github

But when opening a modal using this package, sometimes, it keeps refreshing a page or once I updated value, it doesn't update a datatable field automatically. Author mentioned like this.

Often you will want to update other Livewire components when changes have been made. For example, the user overview when a user is updated. You can use the closeModalWithEvents method to achieve this. public function update() { Gate::authorize('update', $this->user);

$this->user->update($data);

$this->closeModalWithEvents([
    UserOverview::class => 'userModified',
]);

}

But even if I tried to use closeModalWithEvents, datatable is not updated with the value what I changed on a modal. Please help me if anyone knows.

0 likes
2 replies
LongSu's avatar

This is modal component code.

@props([
    'name',
    'show' => false,
    'maxWidth' => '2xl'
])

@php
$maxWidth = [
    'sm' => 'sm:max-w-sm',
    'md' => 'sm:max-w-md',
    'lg' => 'sm:max-w-lg',
    'xl' => 'sm:max-w-xl',
    '2xl' => 'sm:max-w-2xl',
][$maxWidth];
@endphp

<div
    x-data="{
        show: @js($show),
        focusables() {
            // All focusable element types...
            let selector = 'a, button, input:not([type=\'hidden\']), textarea, select, details, [tabindex]:not([tabindex=\'-1\'])'
            return [...$el.querySelectorAll(selector)]
                // All non-disabled elements...
                .filter(el => ! el.hasAttribute('disabled'))
        },
        firstFocusable() { return this.focusables()[0] },
        lastFocusable() { return this.focusables().slice(-1)[0] },
        nextFocusable() { return this.focusables()[this.nextFocusableIndex()] || this.firstFocusable() },
        prevFocusable() { return this.focusables()[this.prevFocusableIndex()] || this.lastFocusable() },
        nextFocusableIndex() { return (this.focusables().indexOf(document.activeElement) + 1) % (this.focusables().length + 1) },
        prevFocusableIndex() { return Math.max(0, this.focusables().indexOf(document.activeElement)) -1 },
    }"
    x-init="$watch('show', value => {
        if (value) {
            document.body.classList.add('overflow-y-hidden');
            {{ $attributes->has('focusable') ? 'setTimeout(() => firstFocusable().focus(), 100)' : '' }}
        } else {
            document.body.classList.remove('overflow-y-hidden');
        }
    })"
    x-on:open-modal.window="$event.detail == '{{ $name }}' ? show = true : null"
    x-on:close-modal.window="$event.detail == '{{ $name }}' ? show = false : null"
    x-on:close.stop="show = false"
    x-on:keydown.escape.window="show = false"
    x-on:keydown.tab.prevent="$event.shiftKey || nextFocusable().focus()"
    x-on:keydown.shift.tab.prevent="prevFocusable().focus()"
    x-show="show"
    class="fixed inset-0 overflow-y-auto px-4 py-6 sm:px-0 z-50"
    style="display: {{ $show ? 'block' : 'none' }};"
>
    <div
        x-show="show"
        class="fixed inset-0 transform transition-all"
        x-on:click="show = false"
        x-transition:enter="ease-out duration-300"
        x-transition:enter-start="opacity-0"
        x-transition:enter-end="opacity-100"
        x-transition:leave="ease-in duration-200"
        x-transition:leave-start="opacity-100"
        x-transition:leave-end="opacity-0"
    >
        <div class="absolute inset-0 bg-gray-500 opacity-75"></div>
    </div>

    <div
        x-show="show"
        class="mb-6 bg-white rounded-lg overflow-hidden shadow-xl transform transition-all sm:w-full {{ $maxWidth }} sm:mx-auto"
        x-transition:enter="ease-out duration-300"
        x-transition:enter-start="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
        x-transition:enter-end="opacity-100 translate-y-0 sm:scale-100"
        x-transition:leave="ease-in duration-200"
        x-transition:leave-start="opacity-100 translate-y-0 sm:scale-100"
        x-transition:leave-end="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
    >
        {{ $slot }}
    </div>
</div>
LongSu's avatar

Okay, seems like there is no expert for livewire. No problem.

Please or to participate in this conversation.