0

我是 cakephp 的新手,我按照教程实现了带有自动完成功能的搜索框,一切顺利,View/Pages/home.ctp 中的搜索框按预期工作,但我真的需要将该搜索框放在我的查看/Terms/index.ctp,我找不到让它工作的方法,在这里我粘贴我生成的所有代码,希望有人能帮助我,并提前感谢:

文件:控制器/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());
    }
}

文件: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); ?>

文件: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']); ?>&nbsp;</td>
            <td><?php echo h($term['Term']['entry']); ?>&nbsp;</td>
            <td><?php echo h($term['Term']['lemma_tag']); ?>&nbsp;</td>
            <td><?php echo h($term['Term']['status']); ?>&nbsp;</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>&nbsp;</li>
            <li><?php echo $this->Html->link(__('New Term'), array('action' => 'add')); ?></li>
            <li>&nbsp;</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>&nbsp;</li>
    <li>
            <?php echo $this->Upload->view('Term', $term['Term']['id']); ?>
    </li>
    </ul>
</div>

解决方案

对于那些面临同样问题的人,@Salines 出色答案的第二部分是使它起作用的部分:echo $this->Form->input('word', array('id'=>'word','label' => 'Cerca entrada'));建议的第一个更改是不必要的。

4

1 回答 1

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 具有正确的 post/get url,但是当您在条款页面上时,javascript 生成的 url 例如 /terms/terms/autocomplete .

更新:将 id="word" 添加到您的输入字段

echo $this->Form->input('word', array('id'=>'word','label' => 'Cerca entrada'));
于 2014-06-02T11:50:34.220 回答