yii来自派生列的cjuiautocomplete

时间:2014-03-20 16:15:40

标签: php yii autocomplete

我想在文本框中使用名称自动完成。但是,该名称会拆分为列first_namelast_name。我有以下代码。

查看

$this->widget('zii.widgets.jui.CJuiAutoComplete',array(
            'attribute'=>'CONSULTANT',
            'model'=>$invoices,

            'sourceUrl'=>array('SugarContacts/InvoicesNameList'),
            // additional javascript options for the autocomplete plugin
            'options'=>array(
                    'minLength'=>'2',
                    'select'=>"js:function(event, ui) { $('#Invoices_CONSULTANT').val(ui.item.id); getAddress(ui.item.id,'billing');}  "
            ),
            'htmlOptions'=>array(
                    'style'=>'height:20px;',
                    'id'=>"Invoices_CONSULTANT_search",
                    "size"=>"50",
                    'name'=>"Invoices[CONSULTANT]",
            ),
        ));

ccontroller

公共职能行动() {     返回数组(

        'ProjectsNameList'=>array(
                'class'=>'application.extensions.EAutoCompleteAction',
                'model'=>'Projects', //My model's class name
                'attribute'=>'PROJECT', //The attribute of the model i will search
        ),
        'ProjectsAreaList'=>array(
                'class'=>'application.extensions.EAutoCompleteAction',
                'model'=>'Projects', //My model's class name
                'attribute'=>'AREA', //The attribute of the model i will search
        ),
        'BidsContactList'=>array(
                'class'=>'application.extensions.EAutoCompleteAction',
                'model'=>'SugarContacts', //My model's class name
                'attribute'=>'first_name', //The attribute of the model i will search
        ),
        'BidNoList'=>array(
                'class'=>'application.extensions.EAutoCompleteAction',
                'model'=>'Bids', //My model's class name
                'attribute'=>'BIDNO', //The attribute of the model i will search
        ),
        'BidsClientRefList'=>array(
                'class'=>'application.extensions.EAutoCompleteAction',
                'model'=>'Bids', //My model's class name
                'attribute'=>'CLIENTREF', //The attribute of the model i will search
        ),
        'BidsAreaList'=>array(
                'class'=>'application.extensions.EAutoCompleteAction',
                'model'=>'Bids', //My model's class name
                'attribute'=>'AREA', //The attribute of the model i will search
        ),
        'OrdersconsultantsNameList'=>array(
                'class'=>'application.extensions.EAutoCompleteAction',
                'model'=>'SugarContacts', //My model's class name
                'attribute'=>'first_name', //The attribute of the model i will search
        ),
        'InvoicesNameList'=>array(
                'class'=>'application.extensions.EAutoCompleteAction',
                'model'=>'SugarContacts', //My model's class name
                'attribute'=>'name', //The attribute of the model i will search
        ),
);

}

模型

public function getName(){
        return $this->first_name . " " . $this->last_name;
    }

1 个答案:

答案 0 :(得分:0)

我必须修改eautocomplete动作类

 public function run()
    {
        if(isset($this->model) && isset($this->attribute)) {
            $criteria = new CDbCriteria();
            $criteria->compare($this->attribute, $_GET['term'], true);
            if ($this->model == "SugarContacts") {
                if($this->attribute == "name"){ //used on invoices
                    $criteria->select = "*, CONCAT(t.first_name, ' ', t.last_name) AS name";
                    $criteria->condition = "(CONCAT(t.first_name, ' ' , t.last_name) LIKE :ycp0 || accounts.name LIKE :ycp0) and length(t.id)<8"; //use id length to grab nav contacts
                    $criteria->join = " join sugarcrm6.accounts_contacts on t.id = sugarcrm6.accounts_contacts.contact_id" .
                     " join sugarcrm6.accounts on sugarcrm6.accounts_contacts.account_id = sugarcrm6.accounts.id"; // join tables to include company name in search
                }
                else
                    $criteria->addSearchCondition('last_name', $_GET['term'], true,"OR");


            }
            $model = new $this->model;
           // print_r($model);
           // print_r($criteria);
            //exit();
            foreach($model->findAll($criteria) as $m)
            {
                if ($this->model == "SugarContacts") {
                    if($this->attribute == "name"){ //used on invoices
                        $this->results[] = array(
                                //'label'=>trim($m->{$this->attribute}),
                                'value'=>trim($m->{$this->attribute}) . " " . " : " . trim($m->sugarAccountsContacts1->sugarAccounts->billing_address_street),
                                'id'=>$m->primaryKey
                        );

                    }else{
                        $this->results[] = array(
                                //'label'=>trim($m->{$this->attribute}),                            
                                'value'=>trim($m->{$this->attribute}) . " " . trim($m->last_name) . " : " . trim($m->primary_address_street), 
                                'id'=>$m->primaryKey 
                                );
                    }
                }else{
                    $this->results[] =  array(
                            'value'=>trim($m->{$this->attribute}), 
                            'id'=>$m->primaryKey 
                            ); 
                }

            }

        }


       if ($this->model != "SugarContacts" && !is_a($model,"Bids")) {
            $this->results = array_unique($this->results);
       }


        echo CJSON::encode($this->results);
    }