0

I'm using this auto-complete plugin with my new project. It's working fine. See image

But I want to populate these fields when I select the result.

Here is my code:

 var as = $("#query" + x + "").autocomplete({
        minChars: 1,
        maxHeight: 100,
        serviceUrl: 'index.php?r=purchaseorder/list',

    });

IN CONTROLLER

    public function actionList() {
    $criteria = new CDbCriteria;
    $criteria->select = array('id','description','unit','rate');
    $criteria->addSearchCondition("description", $_GET['query']);
    $criteria->limit = $this->limit;


    $items = Item::model()->findAll($criteria);
    $suggestions = array();
    $data = array();
    foreach ($items as $c) {
        $suggestions[] = $c->description;
        $data[] = $c->id;
    }
    header('Content-type: application/json; charset=utf-8');

    echo CJSON::encode(
            array(
                'query' => 'q',
                'suggestions' => $suggestions,
                'data' => $data
            )
    );
    exit;
}

grid jquery

  jQuery("#addrow").click(function() {

        jQuery(".item-row:last").after('<tr class="item-row">\n\
<td>\n\
<span id="delete' + x + '" style="cursor: pointer" class="icon-remove"></span>\n\
</td>\n\
<td class="item-code"><input autocomplete="off" name="code[]" id="code' + x + '" type="text" class="input-code"/></td>\n\
<td class="item-description"><input autocomplete="off" name="q" id="query' + x + '" type="text" class="input-description"/></td>\n\
<td class="item-unit"><input readonly autocomplete="off" name="unit[]" id="unit' + x + '" type="text" class="input-unit"/></td>\n\
<td class="item-qty"><input name="qty[]" autocomplete="off" value="0" id="qty' + x + '" type="text" class="input-qty"/></td>\n\
<td class="item-rate"><input readonly name="rate[]" autocomplete="off" value="125.25" id="rate' + x + '" type="text" class="input-rate"/></td>\n\
<td class="item-discount"><input name="discount[]" autocomplete="off" value="0.00" id="discount' + x + '" type="text" class="input-discount"/></td>\n\
<td class="item-total"><input name="total[]" readonly autocomplete="off" value="0.00" id="total' + x + '" type="text" class="input-amount"/></td>\n\
</tr>');

controller is already there

tshepang
  • 12,111
  • 21
  • 91
  • 136
Harsha Tharanga
  • 94
  • 4
  • 16

2 Answers2

1

I have done it like this... IN JQUERY....

var as = $("#query").autocomplete({
        minChars: 1,
        maxHeight: 100,
        serviceUrl: 'index.php?r=purchaseorder/list',
        onSelect: function(suggestion) { 
            var row = $(this).closest('tr');
            row.find('.input-code').val(suggestion.id).attr('readonly', 'readonly');
            row.find('.input-description').attr('readonly', 'readonly');
            row.find('.input-unit').val(suggestion.unit).attr('readonly', 'readonly');
            row.find('.input-rate').val(suggestion.rate).attr('readonly', 'readonly');
            row.find('.input-qty').focus();
        }       
    });

AND THEN IN CONTROLLER

    public function actionList() {
    $criteria = new CDbCriteria;
    $criteria->select = array('description', 'id','unit','rate');
    $criteria->addSearchCondition("description", $_GET['query']);
    $criteria->limit = $this->limit;

    $items = Item::model()->findAll($criteria);
    $suggestions = array();
    $x=0;
    foreach ($items as $c) {
        $suggestions[$x]['value'] = $c->description;
        $suggestions[$x]['id'] = $c->id;
        $suggestions[$x]['rate'] = $c->rate;
        $suggestions[$x]['unit'] = $c->unit;
        $x++;
    }
    header('Content-type: application/json; charset=utf-8');

    echo CJSON::encode(
            array(
                'suggestions' => $suggestions,
            )
    );
    exit;
}

thats it...!

Harsha Tharanga
  • 94
  • 4
  • 16
0

Sample code as shown below

$('#yourautoCompleteId').change(function(){
    var selecteddata=$(this).val();
    $.ajax({
        url: "'.$this->createUrl('Controller/yourMethod').'",
        data: {
            //special:specialisation,
            data   :selecteddata,
            },
            type:"GET",//you can also use POST method
            dataType:"html",//you can also specify for the result for json or xml
            success:function(response){
                //write the logic to get the response data as u need and set it to the fields 
                $("#dataId").val("SetHere");
                $('#quantityId').val("setHere");
             },
             error:function(){
                    //TODO: Display in the poll tr ifself on error   
                    alert("Failed request data from ajax page"); 
                }
        });
})

get the data in the controller using post and query with this data and send the result as u need and set to the fields as shown in the sample

Ninad
  • 1,871
  • 3
  • 15
  • 23
  • i will try this and give u feed back – Harsha Tharanga Jun 28 '13 at 11:28
  • thanks to you,this method works fine....(for now its ok) but if I can get the job done by DIVBRIDGE method,thats really what I want... – Harsha Tharanga Jun 28 '13 at 12:28
  • make use of the property onselect property https://github.com/devbridge/jQuery-Autocomplete provided by auto complete API – Ninad Jun 28 '13 at 12:43
  • http://www.fileconvoy.com/dfl.php?id=g600f15bb99dd540a999318482e0bb0c8000d75918 THIS IS THE CODE I HAVE TRIED...PLEASE HELP ME BRO... – Harsha Tharanga Jun 29 '13 at 03:53
  • you have missed "value" attribute for your code and unit fields in .js file and given hardcode value to unit price field 125.25 .Instead you use ajax and get the required value and and set to the value attribute of the fields thats it – Ninad Jun 29 '13 at 05:41
  • i tried that,when i pass json array(contains:code,description,unit,rate) from controller to devbadge method,it doesnt even display suggestions,also i cant access that json array from that method.iam not good at jqeury(may be thats why).main thing i want is display description value on suggestion list and populate other values when i select one suggestion.if you can give me a sample code grate(i know you have lot of works :) but plzzzz) – Harsha Tharanga Jun 29 '13 at 05:54
  • Go through the code provided there u may get some clue http://af-design.com/blog/2010/05/12/using-jquery-uis-autocomplete-to-populate-a-form/ – Ninad Jun 29 '13 at 07:02
  • Or else when u select the option from auto select u process and bring all the required data without using ajax and using select: property of autocomplete load the data as shown in the above thread – Ninad Jun 29 '13 at 07:16
  • Thanks Ninad... I did it... :) tkz a lot – Harsha Tharanga Jun 29 '13 at 12:24