Mamun Sardar
Mamun Sardar

Reputation: 2729

JQuery autocomplete with remote JSON datasource not working

I'm using this jquery plugin : JQuery Autocomplete. Problem I'm getting json data but it's not appearing on the autocomplete list. The JQuery code:

$( "#student-id" ).autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: "ajax/ajax_admin.php?auto_student=" + $( "#student-id" ).val(),
            dataType:"json",
            data: {
                featureClass: "P",
                style: "full",
                maxRows: 12,
                name_startsWith: request.term
            },
            success: function( data ) {
                response( $.map( data.students, function( item ) {
                    return {
                        label: item.id +" , "+ item.name,
                        value: item.id
                    }
                }));
            }
        });
    },
    minLength: 2,
    open: function() {
        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
    },
    close: function() {
        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
    }
});

The PHP Script is :

public function load_ajax_student_list($val)
{
    $val = "%".$val."%";
    $stmt = $this->conn->prepare("select * from student where studentAiubId like :value limit 0,5");
    $stmt->bindValue(':value', $val);
    $stmt->execute();
    if($stmt->rowCount() < 1)
        echo "";
    else
    {
        $result = $stmt->fetchAll();

        $output = array();
        foreach($result as $row)
        {
            if($row['mname']=="")
                $name = $row['fname']." ".$row['lname'];
            else
                $name = $row['fname']." ".$row['mname']." ".$row['lname'];
            $data["name"] = $name;
            $data["id"] = $row['studentAiubId'];
            $output["students"][] = $data;
        }
        echo json_encode($output);                
    }
}

If the call goes like this: ajax/ajax_admin.php?auto_student=10
The produced data from PHP script is :

{
    "students": [
        {"name":"Moh Mamun Sardar","id":"10-15987-1"},
        {"name":"Rehan Ahmed","id":"10-12451-2"},
        {"name":"Abid Hassan","id":"10-15412-1"},
        {"name":"Abir Islam","id":"10-11245-1"}
    ]
}

But Nothing showing on autocomplete. What I'm doing wrong?

Upvotes: 1

Views: 1769

Answers (2)

gprusiiski
gprusiiski

Reputation: 450

You have forgotten the "appendTo" property. In this propery you have to specify the selector of the element you wish the information to be appended to like this

appendTo: '.class' or appendTo: '#id'

You have to add this property to the initialization of the autocomplete as a sibling of source and etc ...

Upvotes: 0

rajesh kakawat
rajesh kakawat

Reputation: 10896

try something like this

$.map( data.students, function(item ) {
    return {
    label: item.name,
    value: item.id
});

it is minlength not minLength see casing

Upvotes: 3

Related Questions