1

こんにちは、SAutoComplete (CAutoComplete を拡張) を使用しており、リストから値が選択されたときに何らかの作業を行う必要があります。

私はこのように使っています。

this->widget('application.components.SAutoComplete', array('width'=>200,
            'model'=>$cssAtapsClient, 'parseData'=>true, 'matchContains'=>true,
            'attribute'=>'suburb_id', 'data'=>$postCode, 'ddindicator'=>true,
            'max'=>50,
            'multipleSeparator'=>false,
            'options' => array(
            'select' => new CJavaScriptExpression('function(e, ui) { alert("hi"); }')
             ),
          )); ?>

jquery UIの自動完了で利用できるような選択オプションがないのはなぜですか?

選択の例は次のとおりです。

$("#auto_cp").autocomplete({
    minLength: 3,
    //source
    source: function(req, add) {
        $.getJSON("friends.php?callback=?", req, function(data) {
            var suggestions = [];
            $.each(data, function(i, val) {
                suggestions.push({
                    label: val.name,
                    zzz: val.zzz
                });
            });
            add(suggestions);
        });
    },
    //select
    select: function(e, ui) {
        alert(ui.item.zzz);
    }
});​

編集2

http://www.yiiframework.com/doc/api/1.1/CAutoComplete

コードはこのようなものです。

<?php

class SAutoComplete extends CAutoComplete
{
   public $ddindicator;
   /**
    *
    * @var boolean whether to parse the data assumes data uses an assoc array
    *  array(value => array(name, value, ...), ...). Only works with a model present
    */
   public $parseData;
   /**
    * @var boolean whether to raise the change event
    */
   public $raiseChangeEvent = false;
   /**
    * Initializes the widget.
    * This method registers all needed client scripts and renders
    * the autocomplete input.
    */
   public function init()
   {
      if ( !$this->max )
         $this->max = 50000;

      if ( $this->ddindicator )
         $this->alternateInit();
      else
         parent::init();
   }

   public function alternateInit()
   {
      list($name,$id)=$this->resolveNameID();
      $this->htmlOptions['id'] = $id.'_input';
      $this->minChars = 0;

      echo CHtml::openTag('div', array('class'=>'ac-input-dd'));
      echo CHtml::openTag('div', array('class'=>'ac-input-btn'));
      echo CHtml::closeTag('div');

      if($this->hasModel())
      {
         $htmlOpt = array();

         if ( $this->parseData )
         {
            $menu = $this->data;
            $key = $this->attribute;

            //Change if attribute is apart of a array. eg attribute[0]
            $pos1 = stripos($key, '[');
            $pos2 = stripos($key, ']');
            if($pos1!==false && $pos2!==false)
            {
               $key = str_replace (substr($key,$pos1,$pos2 - $pos1 + 1),'',$key);
               $htmlOpt['value'] = isset($this->model->$key) ? $this->model->$key : '';
            }

            $this->value = isset($menu[$this->model->$key][0]) ? $menu[$this->model->$key][0] : '';
            $this->data = is_array($menu) ? array_values($menu) : array('Error in data.');
         }

         echo CHtml::activeHiddenField($this->model, $this->attribute, array_merge(array('id'=>$id, 'name'=>$name), $htmlOpt));
         echo CHtml::textField('', $this->value, $this->htmlOptions);
      }
      else
      {
         echo CHtml::hiddenField($name, $this->value, array('id'=>$id));
         echo CHtml::textField($name.'_input',$this->value,$this->htmlOptions);
      }
      echo CHtml::closeTag('div');

      $this->methodChain = $this->methodChain.'.result(function(evt, data, formatted) { $("#'.
              $id.'").val(data ? data[1] : "")'.($this->raiseChangeEvent?'.change()':'').'; })'.
              '.parent().find(".ac-input-btn").mousedown(function(){'.
              'jQuery(this).parent().find(".ac_input").toggleResults();})'.
              '.mouseup(function(){jQuery(this).parent().find(".ac_input").focus();});';

      $this->registerClientScript();
   }

   public static function registerScript()
   {
      $cs = Yii::app()->getClientScript();
      $cs->registerCoreScript('jquery');
      $cs->registerCoreScript('bgiframe');
        TK::registerScriptFile('autocomplete');
      $cs->registerCssFile($cs->getCoreScriptUrl().'/autocomplete/jquery.autocomplete.css');
   }

   /**
    * Registers the needed CSS and JavaScript.
    * @since 1.0.1
    */
   public function registerClientScript()
   {
      // can cut this down once YII releases a fix for defect #38
      if ( Yii::app()->request->isAjaxRequest || $this->ddindicator )
      {
         $id=$this->htmlOptions['id'];

         $acOptions=$this->getClientOptions();
         $options=$acOptions===array()?'{}' : CJavaScript::encode($acOptions);

         $cs=Yii::app()->getClientScript();

         if($this->data!==null)
            $data=CJavaScript::encode($this->data);
         else
         {
            $url=CHtml::normalizeUrl($this->url);
            $data='"'.$url.'"';
         }

         if ( Yii::app()->request->isAjaxRequest )
         {
            echo '<script type="text/javascript">jQuery(document).ready('.
                    'function() {jQuery("#'.$id.'").autocomplete('.$data.','.$options.')'.
                    $this->methodChain.';});</script>';
         }
         else
         {
            SAutoComplete::registerScript();
            $cs->registerScript('Yii.CAutoComplete#'.$id,"jQuery(\"#{$id}\").autocomplete($data,{$options}){$this->methodChain};");
         }
      }
      else
         parent::registerClientScript();
   }
}
4

3 に答える 3

0

これを追加してみてください、

'methodChain'=>".result(function(event,item){ urFunction(); })",

良い1日を!

最終的には次のようになります。

$this->widget('application.components.SAutoComplete', array('width'=>200,
            'model'=>$cssAtapsClient, 'parseData'=>true, 'matchContains'=>true,
            'attribute'=>'suburb_id', 'data'=>$postCode, 'ddindicator'=>true,
            'max'=>50,
            'methodChain'=>".result(function(event,item){ urFucntion(); })",
         ));
于 2013-10-03T04:53:59.360 に答える