【问题标题】:Cakephp 2.1 and Jquery UI autocomplete not workingCakephp 2.1 和 Jquery UI 自动完成功能不起作用
【发布时间】:2012-12-16 23:24:36
【问题描述】:

我尝试在 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(实例,参数);

【问题讨论】:

  • 这样做会使您的应用程序易受攻击。您从用户输入中提供自定义构建的查询参数。

标签: jquery cakephp user-interface autocomplete


【解决方案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())));
}

【讨论】:

  • 有时我使用不同的表,从控制器和模型响应是好的,我可以在 firebug 中看到,但没有下拉列表
  • 我是 cakephp 和 jquery 的新手,但我已经完成了 dms,其他所有工作都正常,我需要清除自动完成破冰船的绳索
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-25
  • 1970-01-01
  • 2020-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
相关资源
最近更新 更多