【问题标题】:yii cjuiautocomplete from dervied column来自衍生列的 yii cjuiautocomplete
【发布时间】:2014-04-27 15:37:28
【问题描述】:

我想在文本框中使用名称自动完成。但是,名称分为列,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;
    }

【问题讨论】:

    标签: php yii autocomplete


    【解决方案1】:

    我不得不修改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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 2015-09-20
      • 2013-03-12
      • 1970-01-01
      相关资源
      最近更新 更多