2

データテーブルから取得したcakephp 2.1データにオートコンプリートを実装しようとしましたが成功しません助けてください

デフォルトのレイアウトで

    echo $this->Html->script('jquery-1.8.3');
    echo $this->Html->script('jquery-ui-1.9.2.custom');

ビューファイルで

 $('.TextBox').keydown(function (e){
   var FieldId =  $(this).attr('id');
   var ColName =  $(this).attr('title');
   var table = document.getElementById("dbTable").value;
   var TableName = table + "_docs";
   var TextValue = ""

   if (e.which >= 32 || e.which < 127) {
      var c = String.fromCharCode(e.which);
      TextValue = $(this).val() + c;
      }

  if(TextValue != ""){
              $.ajax({
              type:'POST',
              url:"../AutoSearch/" + TableName + "/" + ColName + "/" + TextValue ,
              success:function(response){
              var data = response.split("|");
              $('#' + FieldId).autocomplete(data);
              }
        });
       }
  });

コントローラーで

public function AutoSearch($table,$col,$text){
       $this->autoRender=false;
       if($this->RequestHandler->isAjax()){
         Configure::write('debug', 0);
         if(trim($text) != ""){
           $info = "";
           $info = $this->Template->getAutoComplete($table,$col,$text);
           }
         else
         {
             $info = "";
         }
         return $info;
      } 
    }

モデルで

     public function getAutoComplete($table,$col,$text){
          $sql = "select " . $col . " from " . $table . " where " . $col . " Like '%" . $text . "%'";
          $ret = $this->query($sql);
          $rText = "";
          foreach($ret as $val){
               if($rText == ""){
                     $rText = $val[$table][$col] . "|";}
                  else {
                $rText = $rText . $val[$table][$col] . "|";}
            }
              return $rText;
    }

firebug のエラー メッセージ

TypeError: this.source は関数ではありません

.apply(インスタンス、引数);

4

1 に答える 1

1

出発点として、デフォルトのjQueryオートコンプリートhttp://jqueryui.com/autocomplete/を使用して、このようなものを調べることをお勧めします。

ビューで

$('.TextBox').autocomplete({
        source: function(request, response) {
            $.ajax({
               type:'POST',
               url:"/your_controller/your_action/",
               data: {
                    column: 'col',
                    search: request.term // request.term will have value in field
                },
               success:function(response){
                    // to see what you are getting to browser
                    // console.log(response);
                    response( $.map( response, function( item ) {
                        // depending on what you send, return object you need
                        // label will be shown in list, value will be set when selected
                        return {
                            label: item.name,
                            value: item.id
                        }
                    }));
                 }
               }
            });
        }
    });

コントローラー内

public function your_action() {
    // to see what you are getting to controller
    // debug($this->request->data);
    exit( json_encode($this->YourModel->find('list', array('conditions' => array())));
}
于 2013-01-02T16:10:22.497 に答える