0

我在我的新项目中使用了这个 自动完成插件。它工作正常。看图片

但是我想在选择结果时填充这些字段。

这是我的代码:

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

    });

在控制器中

    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;
}

网格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>');

控制器已经存在

4

2 回答 2

1

我已经这样做了...... 在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();
        }       
    });

然后在控制器中

    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;
}

而已...!

于 2013-06-30T12:35:00.363 回答
0

示例代码如下图

$('#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"); 
                }
        });
})

使用 post 获取控制器中的数据并使用此数据进行查询,并根据需要发送结果并设置为示例中所示的字段

于 2013-06-28T11:18:11.900 回答