Post by CedSha » Thu May 22, 2025 2:59 am

Hello everybody,
I am learning with V4.1.0.3
In Trying to do an autocomplete in a form on admin side
Everything is going well to the step I have the correct JSON response.
I can even see JQuery building the list string while stepping inside
But then the list result is not displaying back in my input field...
Any idea that could push me in the good direction ?
Thanks for any suggestion ...

Code: Select all

   <form method="post" action="{{ start_url }}" class="d-flex align-items-center gap-2">
      <label for="input-model" class="form-label">Model</label>
      <input type="text" name="filter_model" id="input-model" value = "" class="form-control" data-oc-target="autocomplete-model" autocomplete="off" placeholder="Model..." style="width: 300px;">
      <button type="submit" class="btn btn-primary">{{ start_button }}</button>
    </form>
....
$('#input-model').autocomplete({
  source: function(request, response) {
    $.ajax({
      url: '{{ autocomplete_url }}&filter_model=' + encodeURIComponent(request),
      dataType: 'json',
      success: function(json) {
        console.log('Autocomplete AJAX response:', json);
        //console.log(item['model']);
        response($.map(json, function(item) {
          return {
            label: item['product_id'],
            value: item['model'],
          };
        }));
      },
      error: function(xhr, status, error) {
        console.error('AJAX Error:', status, error);
        console.error('Response Text:', xhr.responseText);
      }
    });
  },
  select: function(item) {
    //$(this).val(item['label']);
    $('#input-model').val(decodeHTMLEntities(item['label']));
    //$('#input-manufacturer-id').val(item['value']);
  }
});
Last edited by CedSha on Thu May 22, 2025 6:24 pm, edited 1 time in total.

Image


User avatar
New member

Posts

Joined
Mon Sep 18, 2023 1:01 am


Post by paulfeakins » Thu May 22, 2025 5:12 pm

CedSha wrote:
Thu May 22, 2025 2:59 am
In Trying to do an autocomplete in a form on admin side
Some forms in the OC admin already autocomplete - why don't you copy how OpenCart does it rather than create a new way?

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by CedSha » Thu May 22, 2025 5:39 pm

paulfeakins wrote:
Thu May 22, 2025 5:12 pm
CedSha wrote:
Thu May 22, 2025 2:59 am
In Trying to do an autocomplete in a form on admin side
Some forms in the OC admin already autocomplete - why don't you copy how OpenCart does it rather than create a new way?
Hi Paul,
Thanks for your feedback !
Yes the code I use is an extract of the existing one, but I should have missed something
and you know sometimes the answer is right under one's nose and one don't realize it !
that's why having a different view is always great !
Whatever I'll start it again from scratch and copy the full code for test...

Image


User avatar
New member

Posts

Joined
Mon Sep 18, 2023 1:01 am


Post by CedSha » Thu May 22, 2025 6:24 pm

spot it !
It was missing the destination for the drop-down after the input :drunk:

Code: Select all

<ul id="autocomplete-model" class="dropdown-menu"></ul>

Image


User avatar
New member

Posts

Joined
Mon Sep 18, 2023 1:01 am

Who is online

Users browsing this forum: No registered users and 4 guests