CodeIgniter Forums
get ID in Autocomplete search using Jquery CI site? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: get ID in Autocomplete search using Jquery CI site? (/showthread.php?tid=24398)



get ID in Autocomplete search using Jquery CI site? - El Forum - 11-09-2009

[eluser]Zeeshan Rasool[/eluser]
Hello,
I have an issue that if we are using jquery auto complete script then how can we get ID for a value that is showing in list and we click on it . because if there is some same values like user names ... so i need to get ID to match the records from DB.
Thanks


get ID in Autocomplete search using Jquery CI site? - El Forum - 11-09-2009

[eluser]mjsilva[/eluser]
I've done something like this, the only difference is that I have a drop_down select with gives the user ability to search for VAT (nif) or Name.

If you have any doubts feel free to post it, I'll answer as soon as possible.


Jquery code in viewer:
Code:
var searchType = $("#searchType_drop :selected").val();
    var ajax_url = '';

    $("#searchType_drop").change(function(){
        searchType = $("#searchType_drop :selected").val();
    });

    $("#searchClient_input").focus(function(){
        switch(searchType)
        {
            case "name":
            ajax_url =  base_url + "/clients/ajax_get_clients_name";
            break;
            case "nif":
            ajax_url =  base_url + "/clients/ajax_get_clients_nif";
            break;
            default:
            ajax_url =  base_url + "/clients/ajax_get_clients_name";
        }

        $(this).autocomplete(
        ajax_url,
        {
            delay:10,
            minChars:2,
            matchSubset:1,
            matchContains:1,
            cacheLength:10,
            autoFill:false,
            formatItem: function(row) {
                return row[0]+'<br>'+'ID: '+row[1]+' - NIF:'+row[2];
            },
            formatResult: function(row){
                return row[0];
            }
        });

        $(this).result(function(event, data, formatted) {
            $("#clientID_input").val(data[1]);
        });

    });

My controler functions:
Code:
// ------------------------------------------------------------------------

    function ajax_get_clients_name()
    {
        //Validate login and permissions
        $this->permissions->chk_login();
        $this->yapp_log->do_log(3,'init', 'Class: '.__CLASS__.' Function: '.__FUNCTION__);

        $q = strtolower($_POST["q"]);
        if ($q)
        {
            $results = $this->clients_model->clientSearch_byName($q);
            if (is_array($results))
            {
                foreach ($results as $key => $result)
                {
                    echo $result['name'].'|'.$result['client_id'].'|'.$result['nif']."\n";
                }
            }
        }
    }

    // ------------------------------------------------------------------------

    function ajax_get_clients_nif()
    {
        //Validate login and permissions
        $this->permissions->chk_login();
        $this->yapp_log->do_log(3,'init', 'Class: '.__CLASS__.' Function: '.__FUNCTION__);
        $q = strtolower($_POST["q"]);
        if ($q)
        {
            $results = $this->clients_model->clientSearch_byNif($q);
            if (is_array($results))
            {
                foreach ($results as $key => $result)
                {
                    echo $result['name'].'|'.$result['client_id'].'|'.$result['nif']."\n";
                }
            }
        }
    }