Our current website is powered by a Laravel blade template, showcasing furniture groups with multiple pieces of furniture in each group. The page is constructed using Laravel's foreach loops for both furniture groups generated by $orderformdata->pgroups and the items within the groups generated by $pgroup->pskus.
We used to have a search bar driven by Angular that enabled live filtering on the page – meaning if you typed 'sofa', anything not related to a sofa would instantly disappear. I am now looking to implement a similar live search functionality without a submit button or action, rather than autocomplete.
I lack expertise in JavaScript but have a JSON encoded variable for these products. I'm considering utilizing JS or JQuery to filter through my foreach loops as an alternative. Below are snippets of the existing JavaScript code on the page, showcasing the JSON object and the non-functional search JS:
<script type = "text/javascript">
var orderFormData = <?php echo json_encode ($tempdata);?>;
</script>
<script>
function myFunction() {
var input, filter, table, tr, td, i;
input = document.getElementById("srch-term");
filter = input.value.toUpperCase();
table = document.getElementsByClassName("wrapper");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
Additionally, here is the blade section featuring the search bar and main group foreach loops:
<div class="uk-width-5-10">
<div class="md-input-wrapper search-form">
<form id="searchProducts">
<input type="text" class="md-input label-fixed" name="srch-term" id="srch-term" autofocus placeholder="Search Products"/>
<span class="md-input-bar"></span>
</form>
</div>
<?php
$tempdata = [];
$i = 0;
?>
@foreach ($orderFormData->pgroups as $pgroup)
<h3 style="font-size: 26px; padding: 10px 0;">{{ $pgroup->group_name }} - {{ $pgroup->group_code }}</h3>
<p class="uk-text-muted" style="font-size: 20px;" >{!! html_entity_decode($pgroup->group_desc) !!} <!--Group Description-->
<div class="uk-width-8-10">
<table class="uk-table" style="width: 100%; min-width: 768px;">
<thead>
<tr>
<th style="width: 10%; font-size: 20px;">Frame</th>
<th style="width: 20%; font-size: 20px;">Description</th>
<th style="width: 15%; font-size: 20px;">Cover/Color</th>
<th style="width: 15%; font-size: 20px;">Cover/Color</th>
<th style="width: 20%; font-size: 20px;">Quantity</th>
<th style="width: 15%; font-size: 20px; text-align: center;"><b>Price</b></th>
</tr>
</thead>
<tbody>
@foreach ($pgroup->pskus as $psku)
<?php $tempdata['sku-' . $i] = $psku ?>
<tr class="@if (isset($psku->quantity) && $psku->quantity > 0) {{ highlight }} @endif">
<td style="font-weight: 500; line-height: 30px; font-size: 14px;">{{ $psku->frame_fmt }}</td>
<td style="font-weight: 500; line-height: 30px; font-size: 14px;">{!! html_entity_decode($psku->frame_desc) !!}</td>
<td style="font-weight: 500; line-height: 30px; font-size: 14px;">{{ $psku->cover1_code }}/{{ $psku->color1_code }} {{ $psku->color1_desc }}</td>
<td style="font-weight: 500; line-height: 30px; font-size: 14px;">{{ $psku->cover2_code }}/{{ $psku->color2_code }} {{ $psku->color2_desc }}</td>
<td style="font-weight: 700; line-height: 30px; font-size: 14px;">
<span style="text-align: center; display: block; width: 100%;">${{ $psku->price }}</span>
</td>
</tr>
@if ($psku->avail_date)
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td style="text-align: right; font-weight: 700;">Available Date:</td>
<td style="color: #ff0000;">{{ \Carbon\Carbon::createFromFormat('dmY', $psku->avail_date)->toDateString() }}
</tr>
<?php $i++; ?>
@endforeach
</tbody>
</table>
</div>
</div>
@endforeach
I feel like my current approach with JS might be flawed, and I need guidance on initiating a suitable live filtering mechanism. I can experiment with different filtering methods once I understand how to begin. Any advice on implementing an effective JS live filter on this search bar/page setup would be highly appreciated.