The Materialize accordion feature seems to be incompatible with the Algolia template

I'm currently facing an issue where the product description using Algolia in a Materialize accordion does not trigger when clicked.

Below is the code snippet from my view:

    <div class="row">
     <div id="hits-container"></div>
    </div>

 <script src="https://cdn.jsdelivr.net/instantsearch.js/1/instantsearch.min.js"></script>
 <script src="<?php echo js_url('jquery.min')?>"></script>
 <script src="<?php echo js_url('initialization')?>"></script>
 <script src="<?php echo js_url('pluginAlgolia')?>"></script>
 <script src="<?php echo js_url('materialize.min')?>"></script>
 <script>
  $(document).ready(function(){
   $('select').material_select();
   $('.collapsible').collapsible();
  });
 </script>

The following code represents the hits widgets in pluginAlgolia.js:

search.addWidget(
 instantsearch.widgets.hits({
  container: '#hits-container',
  hitsPerPage: 6,
  templates: {
    empty: noResultsTemplate,
    item: hitTemplate
   }
  })
 );

Additionally, here is the template found in initialization.js:

var hitTemplate =
 '<div class="col s12 m6 l4">' +
  '<article class="hit">' +
   '<div class="product-picture-wrapper center">' +
    '<div class="product-picture"><img src="{{image}}" /></div>' +
   '</div>' +
   '<div class="product-name"><h4>{{{_highlightResult.designation.value}}}</h4></div>' +
     '<ul class="collapsible" data-collapsible="accordion">' +
      '<li>'+
        '<div class="collapsible-header">Description</div>' +
        '<div class="collapsible-body">' +
          '{{{_highlightResult.description.value}}}' +
        '</div>' +
       '</li>' +
      '</ul>' +
     '<div class="product-price">${{prixttc}}</div>' +
    '</article>' +
   '</div>';

Answer №1

Make sure to call the .expand() method after your initial DOM load.

When searchTool.js updates your template, it inserts new elements into the DOM that have not had the .expand() method applied to them.

To handle this issue, utilize the refresh event from searchTool.js to apply the .expand() method to these fresh DOM additions:

searchTool.on('refresh', function () {
  $('.expandable').expand();
});

This will update all existing elements as well as newly added ones. It is crucial to avoid calling the expand method twice on the same element, as it may cause undesired behavior.
To prevent this, focus on targeting only those elements that have not yet had the method applied by potentially adding a distinguishing attribute or class via expand.
Alternatively, you can manually include it like so:

searchTool.on('refresh', function () {
  $('.expandable:not(.expanded)').expand().addClass('expanded');
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Notifying with Jquery Alert after looping through routes using foreach loop

I am trying to create a jQuery alert message that displays after clicking on a dynamically generated button in the view using a foreach loop. The issue I am facing is that only the first button in the loop triggers the alert, while the subsequent buttons ...

Prevent the submit button from being clicked again after processing PHP code and submitting the form (Using AJAX for

I've set up a voting form with submit buttons on a webpage. The form and PHP code work fine, and each time someone clicks the vote button for a specific option, it gets counted by 1. However, the issue is that users can spam the buttons since there i ...

Spring MVC applications might experience slow loading times when loading RequireJS libraries

Recently, I integrated RequireJS into my Spring MVC application to manage dependencies for various libraries, including jQuery and jQuery UI. Although I have successfully implemented it, I am facing an issue whenever the page is loaded or refreshed. Initia ...

The earlier polity of the web page no longer retains the JQuery callback method upon refreshing

Whenever I hit the refresh button on Chrome before my Ajax request succeeds, the callback function complete: function(data){ doSomething()} does not execute. function startMockServer() { $.ajax({ url: "/mocks/[[${id}]]/start", type: "P ...

When sending an AJAX request to a URL, can I verify in the PHP page whether it is a request or if the page has been accessed?

In order to enhance security measures, I need to prevent users from accessing or interacting with the php pages that will be utilized for ajax functionality. Is there a method available to determine if a page has been accessed through an ajax request or b ...

When a link is clicked, submit a form and send it to several email addresses simultaneously

My form has been styled using the uniform jQuery plugin. Instead of using an input type submit for submitting the form, I have utilized a link and added some effects to it to prevent it from being formatted with uniform. <form> <ul> ...

Having trouble retrieving JSON data using jQuery and Handlebars?

Hey there, I'm new to JSON and all the talk about JavaScript templating engines for front-end dynamic changes. However, I'm struggling to make progress. Can anyone help me out? The JSON file is named data.json { users: [{ userna ...

A versatile multi-select jQuery widget featuring the ability to choose options within a specific opt

I am on the hunt for a cool jQuery tool that has the following characteristics: It enables you to create multiple groups, such as: Group 1 - Sub 1 1 - Sub 1 2 - Sub 1 3 Group 2 - Sub 2 1 Group 3 - Sub 3 1 - Sub 3 2 By clicking on Group 1, for instance, ...

Waypoint function malfunctioning when overflow:hidden is applied

CodePen If you exclude the overflow property from the CSS selector .wrapper in the CodePen example, the waypoints functionality will work properly. However, it does not function correctly when using the overflow set to hidden either horizontally (x) or ...

Is it possible to toggle between thumbnails and a larger image in a jQuery gallery?

Hello, I am new to website development and I would like to create a jquery based photo gallery for my personal website. One feature that really catches my eye is this one (for example): The gallery has a large thumbnail grid where you can click on a thumb ...

Having trouble modifying a value in a form and submitting it to the following jsp page

I'm encountering an issue with changing the value in a hidden input element before submitting data to another JSP file (db.jsp) for processing. The value should be different depending on which button is clicked, but it always remains as the default va ...

What is the best way to refine the results from an AJAX request in Datatables?

I have successfully configured a Datatables plugin, set up a new table, and populated it with content using an AJAX call: var table= $("#mytable").DataTable({ ajax: "list.json", columns: [ {"data": "name"}, {"data": "location"}, ...

How to Ensure an Element Appears Above Another Despite Z-Index Troubles?

After conducting approximately 2 hours of research on this topic, I was unable to find clear answers or solutions. Hence, I have decided to address the issue here. The problem I'm facing is as follows: Due to the nature of HTML/CSS, it seems impossi ...

The system of jQuery Dialog UI is unable to recognize when the close icon is clicked

How can I detect if a user closes my dialog modal box using the 'X' button in the upper-right corner of the titlebar? I've been struggling to find a solution while using jQuery's Dialog UI plugin. Here is what I have tried so far: $(&a ...

Using AJAX, SQL and PHP to send data to a separate page for processing

This is the code I use to retrieve questions via ajax from a list of questions stored in a SQL database. <form id="reg-form3"> <ul class="nav nav-list primary push-bottom"> <? $db['db_host']="localhost"; ...

Run a Javascript function two seconds after initiating

My goal is to implement a delay in JavaScript using either setInterval or setTimeout, but I am facing an issue where the primary element is getting removed. Code that works fine without Javascript delay: const selectAllWithAttributeAdStatus = document. ...

generate a different identifier for ajax requests avoiding the use of JSON

The AJAX request functions in the following way: success: function(data) { $('#totalvotes1').text(data); } However, I need it to work with unique IDs as follows: success: function(data) { $('#total_' + $(this).attr("id")). t ...

What is the best way to package JS in a partial view from ASP.NET MVC4 when it is sent back through AJAX?

Currently utilizing the System.Web.Optimization framework for bundling and minifying JavaScript, along with @section Script{ } sections to organize all scripts at the bottom of the page. This includes jQuery resources. In one of my pages, there's an ...

Ways to determine in each() whether the current div contains a specific class or not

My task is to verify if the div contains the class 'completed', and then insert the text "Hello". However, my current code is displaying "Hello" even when the div does not have the class 'completed'. jssfiddle ...

I am interested in obtaining the latitude and longitude of a specific city

I need to relocate the Google Map to a particular city based on user selection from a dropdown menu. I must obtain the latitude and longitude of the chosen city. Once the city is selected, I will determine its coordinates using the following code: var c ...