我尝试在从数据表中获取的 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 中的错误消息
类型错误:this.source 不是函数
.apply(实例,参数);