我是cakephp的新手,我已经按照教程实现了自动完成的搜索框,一切顺利,View / Pages / home.ctp的搜索框按预期工作,但我真的需要放我的View / Terms / index.ctp上的搜索框,我找不到让它工作的方法,在这里我粘贴了我生成的所有代码,希望有人可以帮助我,并提前感谢:
file:Controller / TermsController.php
<?php
App::uses('AppController', 'Controller');
class TermsController extends AppController {
public $helpers = array('Js'=>'Jquery', 'Form', 'Html');
public $components = array('Paginator', 'RequestHandler', 'Session');
public function getdata($id = null){
$this->Term->id = $id;
if(!$this->Term->exists()):
throw new NotFoundException("El terme no existeix");
endif;
$this->set('term', $this->Term->read(null, $id));
$this->layout = 'ajax';
}
public function autocomplete(){
$this->set('terms', $this->Term->find('all',
array('conditions' => array(
'Term.entry LIKE' => $this->request->query['term'].'%'),
'fields' => array('id', 'entry')
)
));
$this->layout = 'ajax';
}
public function index() {
$this->Term->recursive = 0;
$this->set('terms', $this->Paginator->paginate());
}
}
file:View / Pages / home.ctp(有效的那个)
<?php $this->layout = 'default';?>
<?php echo $this->Form->create(null,array());
echo $this->Form->input('word', array('label' => 'Cerca entrada'));
$this->Form->end('Cerca'); ?>
<div id="results"></div>
<script type="text/javascript">
var js = jQuery.noConflict();
(function($){
js(document).ready(function(){
js("#results").hide();
//Autocomplete
js( "#word" ).autocomplete({
source: "terms/autocomplete",
minLength: 3,
focus: function(event, ui){
js("#word").val(ui.item.Term.entry); return false;
},
select: function( event, ui ) {
js( "#word" ).val(ui.item.Term.entry);
var id = ui.item.Term.id;
$.ajax({
url: 'terms/getdata/'+id,
dataType: 'json',
success: function(data){
var html = '<div class="lemma_tag">';
html += '<h2>Lemes i etiquetes</h2>';
html += '<p>'+data.Term.lemma_tag+'</p>';
html += '<p><a href="terms/view/'+id+'">Més accions</a></p>';
html += '</div>';
js("#results").html(html);
js("#results").show('blind');
}
});
return false;
}
//select
}).data( "ui-autocomplete" )._renderItem = function(ul, item){
return $("<li></li>")
.data("ui-autocomplete-item", item)
.append("<a>" + item.Term.entry + "</a>")
.appendTo(ul)
};
//Autocomplete.end
});
})(jQuery);
</script>
档案:查看/条款/ autocomplete.ctp
<?php echo $this->Js->object($terms);?>
档案:查看/条款/ getdata.ctp
<?php echo $this->Js->object($term); ?>
file:View / Terms / index.ctp(这里它不起作用,我粘贴整个文件让你知道我想要实现的目标)< /强>
<div class="terms index">
<h2><?php echo __('Terms'); ?></h2>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('id'); ?></th>
<th><?php echo $this->Paginator->sort('entry', 'Entrada'); ?></th>
<th><?php echo $this->Paginator->sort('lemma_tag', 'Lemes i etiquetes'); ?></th>
<th><?php echo $this->Paginator->sort('status', 'Estat'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
<?php foreach ($terms as $term): ?>
<tr>
<td><?php echo h($term['Term']['id']); ?> </td>
<td><?php echo h($term['Term']['entry']); ?> </td>
<td><?php echo h($term['Term']['lemma_tag']); ?> </td>
<td><?php echo h($term['Term']['status']); ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $term['Term']['id'])); ?>
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $term['Term']['id'])); ?>
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $term['Term']['id']), array(), __('Are you sure you want to delete # %s?', $term['Term']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
));
?> </p>
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li>
</li>
<li> </li>
<li><?php echo $this->Html->link(__('New Term'), array('action' => 'add')); ?></li>
<li> </li>
<li>
<?php $this->layout = 'default';?>
<?php echo $this->Form->create(null,array());
echo $this->Form->input('word', array('label' => 'Cerca entrada'));
$this->Form->end('Cerca'); ?>
<div id="results"></div>
<script type="text/javascript">
var js = jQuery.noConflict();
(function($){
js(document).ready(function(){
js("#results").hide();
//Autocomplete
js( "#word" ).autocomplete({
source: "terms/autocomplete",
minLength: 3,
focus: function(event, ui){
js("#word").val(ui.item.Term.entry); return false;
},
select: function( event, ui ) {
js( "#word" ).val(ui.item.Term.entry);
var id = ui.item.Term.id;
$.ajax({
url: 'terms/getdata/'+id,
dataType: 'json',
success: function(data){
var html = '<div class="lemma_tag">';
html += '<h2>Lemes i etiquetes</h2>';
html += '<p>'+data.Term.lemma_tag+'</p>';
html += '<p><a href="terms/view/'+id+'">Més accions</a></p>';
html += '</div>';
js("#results").html(html);
js("#results").show('blind');
}
});
return false;
}
//select
}).data( "ui-autocomplete" )._renderItem = function(ul, item){
return $("<li></li>")
.data("ui-autocomplete-item", item)
.append("<a>" + item.Term.entry + "</a>")
.appendTo(ul)
};
//Autocomplete.end
});
})(jQuery);
</script>
</li>
<li> </li>
<li>
<?php echo $this->Upload->view('Term', $term['Term']['id']); ?>
</li>
</ul>
</div>
解决方案:
对于那些面临同样问题的人来说,@ Saline的优秀答案的第二部分是使其有效的部分:echo $this->Form->input('word', array('id'=>'word','label' => 'Cerca entrada'));
建议的第一个更改是不必要的。
答案 0 :(得分:1)
尝试替换这行代码:
source: "terms/autocomplete",
...
url: 'terms/getdata/'+id,
在此:
source: "<?php echo $this->Html->url(array('controller' => 'terms','action' => 'autocomplete'),true);?>",
url: "<?php echo $this->Html->url(array('controller' => 'terms','action' => 'getdata'),true) .'/';?>" +id,
要理解差异,如果您在javascript代码中放置了静态路径"terms/autocomplete"
,那么从您的主页javascript有正确的帖子/获取网址,但是当您在条款页面上时,会生成javascript url eg / terms / terms / autocomplete。
更新:将id =“word”添加到您的输入字段
echo $this->Form->input('word', array('id'=>'word','label' => 'Cerca entrada'));