CakePHP中的自动填充数据字段

时间:2016-09-28 15:44:00

标签: javascript mysql cakephp cakephp-2.5

我是CakePHP的新手。我有一个CakePHP(2.5.1)应用程序,它有 streets 表(列id,street_name,house_no,区,城市)。我需要通过提取表数据来自动填充表单。应该是这样,用户将在表单的街道名称字段中键入街道名称,该字段包含字段 - 街道名称 House No 城市。从自动建议中选择街道名称后,其他字段中将填充相应的数据( House No District City )。

现在, streets 表中加载了超过25000个数据行,该表是城市的整个街道数据库。它是Windows Azure上的ClearDB数据库。 streets 表未通过外键与应用程序的其他表链接。我有街道模型,StreetsController,但视图位于不同的文件夹(app / View / User / add.ctp)。 app / Model / Street.php中的 Street 模型如下:

  class Street extends AppModel {

    public function getStreetNames($term = null) {
      if(!empty($term)) {
        $streets = $this->find('list', array(
          'conditions' => array(
            'street_name LIKE' => trim($term) . '%'
          )
        ));
        return $streets;
      }
      return false;
    }
  }

在app / Controller / StreetsController.php中,

class StreetsController extends AppController {

    public $components = array('RequestHandler');

    public function autocomplete($term){

    if ($this->request->is('get')) {
        $this->autoRender = false;
        $data = $this->Street->getStreetNames($term);
        $this->set(compact('data'));
        $this->set('_serialize', array('data'));

        echo json_encode($data);
        }
    }
  }

在app / webroot / js / View / Users / streetdata.js文件中,

  (function($) {
  $('#autocomplete').autocomplete({
        source: "/streetdata.json",
        minLength: 1
  });
})(jQuery);

输入表单位于不同的控制器中。在app / View / Users / streetdata.ctp文件中,

 <?php

      echo $this->Html->script('View/Users/streetdata', array('inline' => false));  

     ?>

<?php echo $this->Form->create('Street', array(
            'class' => 'form-horizontal', 
            'role' => 'form',
            'inputDefaults' => array(
                'format' => array('before', 'label', 'between', 'input', 'error', 'after'),

                'div' => array('class' => 'form-group'),
                'class' => array('form-control'),
                'label' => array('class' => 'col-lg-2 control-label'),
                'between' => '<div class="col-lg-3">',
                'style'=>array('width:200px; height:35px;'),
                'after' => '</div>',
                'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline')),
        ))); ?>

        <fieldset>
        <legend><?php echo __('Street data '); ?></legend>

        <?php
               echo $this->Form->input('Street.street_name', array(
                            'class' => 'ui-autocomplete',
                            'id' => 'autocomplete'));

               echo $this->Form->input('Street.house_no');
               echo $this->Form->input('Street.district');
               echo $this->Form->input('Street.city');
    ?>
    </fieldset>

    <?php echo $this->Form->end(__('Proceed')); ?>

在View / Layouts / default.ctp中,我添加了以下内容

    echo $this->Html->script('jquery.js');
    echo $this->Html->script('bootstrap.min');  
    echo $this->Html->script('https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js');
    echo $this->Html->script('https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', array('inline' => false));
    echo $this->Html->script('https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', array('inline' => false));

    echo $this->Html->css('https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css');
    echo $this->Html->css('font-awesome.min');
    echo $this->Html->css('bootstrap.min');
    echo $this->Html->meta('icon');
    echo $this->fetch('meta');
    echo $this->fetch('css');
    echo $this->fetch('script');

    echo $this->Html->charset();

但自动填充字段(街道名称和其他字段)无效。

我在Chrome浏览器的控制台中发现以下错误消息:

-Failed to load resource: the server responded with a status of 404 (Not Found)  http://localhost:26949/Users/js/jquery.js 
-Failed to load resource: the server responded with a status of 404 (Not Found)  http://localhost:26949/Users/js/bootstrap.min.js 
-Uncaught TypeError: Cannot read property 'offsetWidth' of undefined           bootstrap.min.js:6 

我正在努力寻找解决方案。有人能告诉我这里有什么问题吗?是因为.js文件的位置?我在这里遗失了什么?

0 个答案:

没有答案