hello In a Livewire component, I’m going to use the Flowbite tab component by JavaScript code. My problem is that the $requestStandard variable is updated by Livewire and a new tab is created on the page, but the javascript code is not applied to the initial tabs and the new tab does not work. In fact, the livewire hook is executed before the new tab is added to the page. Please guide me what change I need to make in the code so that I can apply the javascript code after inserting the contents of the new tab in the page?
I use Livewire 3.
Component Blade Code:
<div class="mb-10">
<div class="flex flex-col md:flex-row justify-center md:justify-start items-center mb-3 gap-2">
<div>Select Cluster:</div>
<div class="w-full md:w-1/4 flex flex-row items-center">
<select wire:model.live="cluster" name="cluster" id="cluster"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
<option value="">Select Cluster</option>
@if( count($clusters) && isset($clusters[0]['khushecode']) )
@foreach($clusters as $c)
<option value="{{$c['khushecode']}}">{{$c['khushename']}}</option>
@endforeach
@endif
</select>
@if( !count($clusters) )
<div class="flex flex-row gap-1 mx-2">
<a href="#" wire:click="reloadClusters" title="بارگذاری خوشهها">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2v1z"></path>
<path d="M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466z"></path>
</svg>
</a>
<div role="status" wire:loading.delay wire:target="reloadClusters">
<svg aria-hidden="true" class="inline w-4 h-4 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
</svg>
<span class="sr-only">Loading...</span>
</div>
</div>
@endif
</div>
<div>Select Group:</div>
<div class="w-full md:w-1/4 flex flex-row items-center">
<select wire:model.live="group" name="group" id="group"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
<option value="">Select Group</option>
@if($this->cluster && count($groups))
@foreach($groups as $g)
<option value="{{$g['groupid']}}">{{$g['groupname']}}</option>
@endforeach
@endif
</select>
@if( $cluster && !count($groups) )
<div class="flex flex-row gap-1 mx-2">
<a href="#" wire:click="updatedCluster" title="بارگذاری گروهها">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-clockwise" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2v1z"></path>
<path d="M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466z"></path>
</svg>
</a>
<div role="status" wire:loading.delay wire:target="reloadGroups">
<svg aria-hidden="true" class="inline w-4 h-4 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
</svg>
<span class="sr-only">Loading...</span>
</div>
</div>
@endif
</div>
</div>
<div class="flex flex-col md:flex-row justify-center md:justify-start items-center mb-3 gap-2">
<div>Select Standard:</div>
<div class="w-full md:w-2/3 flex flex-row items-center">
<select wire:model.live="standard" name="standard" id="standard"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
<option value="">Select Standard</option>
@if($this->group && count($standards))
@foreach($standards as $s)
<option value="{{$s['standardid']}}">{{$s['name']}}</option>
@endforeach
@endif
</select>
@if( $group && !count($standards) )
<div class="flex flex-row gap-1 mx-2">
<a href="#" class="text-decoration-none" wire:click="reloadStandards" title="loading standards">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-clockwise" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2v1z"></path>
<path d="M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466z"></path>
</svg>
</a>
<div role="status" wire:loading.delay wire:target="updatedGroup">
<svg aria-hidden="true" class="inline w-4 h-4 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
</svg>
<span class="sr-only">Loading...</span>
</div>
</div>
@endif
</div>
</div>
@if( $standard )
<div class="my-3 p-3 border border-secondary rounded-3 bg-neutral-300/30">
<div class="mb-3 flex flex-col md:flex-row justify-center md:justify-start">
<div class="me-4 font-bold">
Standard title:
</div>
<div>
{{$standardInfo['name']}}
</div>
</div>
<div class="mb-3 flex flex-col md:flex-row *:text-center *:md:text-start">
<div class="pe-4 font-bold">
Standard code:
</div>
<div class="me-4">
{{verta()->enToFaNumbers($standardInfo['ccode'])}}
</div>
<div class="pe-4 font-bold">
Standard time:
</div>
<div class="me-4">
{{verta()->enToFaNumbers($standardInfo['jam'])}} Hours
</div>
</div>
<div class="flex justify-center md:justify-start">
<button wire:click="addStandardToRequest"
class="text-white bg-purple-700 hover:bg-purple-800 focus:ring-4 focus:ring-purple-300 font-medium rounded-lg text-sm px-5 py-1.5 me-2 mb-2 dark:bg-purple-600 dark:hover:bg-purple-700 focus:outline-none dark:focus:ring-purple-800">
Add Standard
</button>
</div>
</div>
@endif
<div class="overflow-y-auto max-h-[130px]">
<ul class="flex flex-wrap" id="tabs-standard" role="tablist">
@foreach($this->requestStandards as $s)
<li class="transition duration-500 ease-in-out rounded-full hover:bg-purple-600 hover:text-purple-50 group text-purple-700"
id="standard-{{ $s['standardid'] }}-tab" data-tabs-target="#standard-{{ $s['standardid'] }}"
role="tab" aria-controls="standard-{{ $s['standardid'] }}" aria-selected="false">
<div class="flex flex-row items-center p-1">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16"
wire:click="removeStandardFromRequest('{{ $s['standardid'] }}')" style="cursor: pointer;"
onclick="return confirm('Are you sure?') || event.stopImmediatePropagation()">
<title>Delete from list</title>
<path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM5.354 4.646a.5.5 0 1 0-.708.708L7.293 8l-2.647 2.646a.5.5 0 0 0 .708.708L8 8.707l2.646 2.647a.5.5 0 0 0 .708-.708L8.707 8l2.647-2.646a.5.5 0 0 0-.708-.708L8 7.293 5.354 4.646z"/>
</svg>
<button class="px-1" type="button">
<span class="text-truncate d-inline-block align-self-center" style="max-width: 230px;">{{$s['name']}}</span>
</button>
</div>
</li>
@endforeach
</ul>
</div>
<div class="flex flex-col justify-center md:justify-start py-3">
@foreach($this->requestStandards as $s)
<div class="hidden" id="standard-{{ $s['standardid'] }}"
role="tabpanel" aria-labelledby="standard-{{ $s['standardid'] }}-tab">
<div class="flex-col justify-center border border-gray-500/50 rounded-md p-3 mb-3">
<div class="grid gap-4 grid-cols-1 md:grid-cols-3 mb-5">
<div>
<div class="pe-4 font-bold">
Standard title:
</div>
<div class="me-4">
{{$s['name']}}
</div>
</div>
<div>
<div class="pe-4 font-bold">
Standard code:
</div>
<div class="me-4">
{{verta()->enToFaNumbers($s['ccode'])}}
</div>
</div>
<div>
<div class="pe-4 font-bold">
Standard time:
</div>
<div class="me-4">
{{verta()->enToFaNumbers($s['jam'])}} Hours
</div>
</div>
<div>
<div class="pe-4 font-bold">
Cluster title:
</div>
<div class="me-4">
{{$s['cluster']}}
</div>
</div>
<div>
<div class="pe-4 font-bold">
Group title:
</div>
<div class="me-4">
{{$s['group']}}
</div>
</div>
</div>
<div class="flex justify-center md:justify-end">
<button wire:click="removeStandardFromRequest('{{$s['standardid']}}')"
class="bg-red-500 text-white px-5 py-1 rounded-md text-sm"
onclick="return confirm('Are you sure?.') || event.stopImmediatePropagation()">
Delete from list
</button>
</div>
</div>
</div>
@endforeach
</div>
@endif
</div>
@script
<script>
Livewire.hook('morph.updated', ({ el, component }) => {
tabElements = [
@php
$item = 0;
$first_item = 0;
@endphp
@foreach($requestStandards as $s)
@php
if($item == 0) $first_item = $s['standardid'];
$item++;
@endphp
{
id: "standard-{{ $s['standardid'] }}",
triggerEl: document.querySelector("#standard-{{ $s['standardid'] }}-tab"),
targetEl: document.querySelector("#standard-{{ $s['standardid'] }}"),
},
@endforeach
];
tabs = new Tabs(tabsElement, tabElements, options, instanceOptions);
initFlowbite();
})
const tabsElement = document.getElementById('tabs-standard');
let tabElements = [
@php
$item = 0;
$first_item = 0;
@endphp
@foreach($requestStandards as $s)
@php
if($item == 0) $first_item = $s['standardid'];
$item++;
@endphp
{
id: "standard-{{ $s['standardid'] }}",
triggerEl: document.querySelector("#standard-{{ $s['standardid'] }}-tab"),
targetEl: document.querySelector("#standard-{{ $s['standardid'] }}"),
},
@endforeach
];
const options = {
defaultTabId: "standard-{{ $first_item }}",
activeClasses:
'bg-cyan-600 text-cyan-50',
inactiveClasses:
'hover:bg-purple-600 hover:text-purple-50 group text-purple-700',
};
const instanceOptions = {
id: 'tabs-standard',
override: true
};
let tabs = null
if(tabElements.length)
tabs = new Tabs(tabsElement, tabElements, options, instanceOptions);
</script>
@endscript