Question

I added a custom field named product_type in Product admin. It works well, but now, I'm trying to add an autocomplete to this field.

Here is my code (in \mysite\admin\themes\default\template\controllers\products\informations.tpl)

<td style="padding-bottom:5px;">
    <input size="55" type="text" id="product_type" name="product_type" value="{$product->product_type|escape:html:'UTF-8'}" style="width: 130px; margin-right: 5px;" />
    <script type="text/javascript">
        $(document).ready(function() {
            $('#product_type').autocomplete({
                source: function(request, response) {
                    $.ajax({
                        url: "ajax.php",
                        dataType: "json",
                        data: { term: request.term, ajaxProductType: 1 },
                        success: function(data) { response(data); }
                    });
                },              
                minLength: 3,
                select: function( event, ui ) {
                    $(this).val(ui.item.value);
                }
            });
        });
    </script>
</td>

When I write in the field, nothing happen. The autocomplete is not called. No error in console. However, when I put an alert() in the document.ready function, it shows. So, why my autocomplete doesn't work?

Version : Prestashop 1.5.6

Thanks in advance for your help

Was it helpful?

Solution

I finally found the problem. The form was loaded by ajax.

So, I had to move the script in the file \prestashop\js\admin-products.js and put it inside the function product_tabs['Informations'] (because my field is under the tab Informations). I also changed the autocomplete script as below.

product_tabs['Informations'] = new function(){
    //... 
    $('#product_type').autocomplete('ajax.php', {
        delay: 100,
        minChars: 1,
        autoFill: true,
        max:255,
        matchContains: true,
        mustMatch:true,
        scroll:false,
        cacheLength:0,
        extraParams: {
            ajaxProductType : 1
        }
    }).result(function(event, item){
        $(this).val(item);
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top