JqueryUI autocomplete does not refine search

1

When using the autocomplete of Jquery Ui I have had a small problem. I am performing an ajax search to feed a text field. In this field a discipline is searched for and the autocomplete mounts the auto-suggestion menu.

The search and presentation of the data works correctly. The problem is autocomplete does the search once or does not refresh the data refinement when one or more characters are added for example:

Eg,ifIsearchtheBANandBANKOFDATAandKANBANisdisplayedasresults.IfIaddonemorecharacterandformBAN,thesuggestionmenuisnothidden(sincenodatawasfound).IfIaddonemorecharacterandformBANCtheKANBANoptionisnothidden

Inshort,onthethirdcharacterthesearchisdone,however,whenanewcharacterisaddedtheauto-suggestionmenuisnotupdated.

Ihavethefollowingcode:

HTML

<inputtype="text" id="nm">

JQUERY

$("#nm").autocomplete({
    minLength:3, 
    source: function(request,response){
        $.post({
            url:'getDisciplina.php',
            dataType:'json',
            data:{'term':request.term},
        }).done(function(data){
            response($.map(data,function(item){
                return({
                    label: item.label,
                    value: item.label,
                    id:    item.value,
                });
            }));
        });
    },

getDiscipline.php

public function getDisciplina(){
    $data = (isset($_POST['term']))?$_POST['term']:false;

    $model = new DisciplinaModel;
    $rs = $model->getDisciplina($data);

    $arr = [];
    if($rs){
        foreach ($rs as $r){
            array_push($arr,["label"=>$r->descr,"value"=>$r->descr,"id"=>$r->id]);
        }
        print_r(json_encode($arr));         
    }
    else{
        print_r(false);
    }

//Retorna algo como:[{"label":"Banco de Dados I","value":"2"},{"label":"Kanban","value":"3"}]
    
asked by anonymous 23.09.2016 / 17:51

1 answer

0

I discovered the problem, the detail is that I was not handling the false return of my PHP as a JSON.

public function getDisciplina(){
    $data = (isset($_POST['term']))?$_POST['term']:false;

    $model = new DisciplinaModel;
    $rs = $model->getDisciplina($data);

    $arr = [];
    if($rs){
        foreach ($rs as $r){
            array_push($arr,["label"=>$r->descr,"value"=>$r->descr,"id"=>$r->id]);
        }
        print_r(json_encode($arr));         
    }
    else{
        array_push ($arr,["label"=>false,"value"=>false,"id"=>false]);
        print_r(json_encode($arr));
    }
}

With the correct return it was easy to manipulate the widget, however, it was necessary to close the auto-suggestion manually.

// Função Autocomplete do jquery ui
$( "#nm" ).autocomplete({
    minLength:3, 
    source: function(request,response){
        $.post({
            url:'/mvc/Disciplina/getDisciplina',
            assync:true,
            dataType:'json',
            data:{'term':request.term},
        }).done(function(data){
            if(data[0].value){
                response($.map(data,function(item){
                    return({
                        label: item.label,
                        value: item.value,
                        id:    item.id,
                    });
                }));
            }   
            else{
                $( "#nm" ).autocomplete('close');
            }
        });
    },
});

It would be interesting to have autocomplete close the auto-suggestion window automatically, but I do not intend to refactor this code now

    
23.09.2016 / 19:06