CakePHP1.2での利用法
jquery.ajaxComboBoxについて、CakePHP1.2での利用法
【コントローラ】
var $components = array('RequestHandler'); function ajax_combobox(){ //Ajax通信以外は、ホームへ戻す if (!$this->RequestHandler->isAjax()){ $this->redirect('../',null,true); } $this->layout = 'ajax'; $data = array(); $db_table = h($this->params['url']['db_table']); $q_word = h($this->params['url']['q_word']); $page_num = h($this->params['url']['page_num']); $per_page = h($this->params['url']['per_page']); $field = h($this->params['url']['field']); $order_field = h($this->params['url']['order_field']); $order_by = h($this->params['url']['order_by']); $conditions = ($q_word) ? array($db_table.'.name LIKE'=>'%'.$q_word.'%') : null; //DBへ問い合わせる $rows = $this->$db_table->find('all',array( 'conditions'=>$conditions, 'fields' =>$field, 'order' =>$order_field . ' ' . $order_by, 'limit' =>$per_page, 'page' =>$page_num )); foreach($rows as $row) { $data['candidate'][] = $row[$db_table][$field]; } //候補の数 $data['cnt'] = $this->$db_table->find( 'count', array('conditions'=>$conditions) ); //JSON形式に変換 echo json_encode($data); //exit必要 exit; }
【ビュー】
<script type="text/javascript"> $(document).ready(function(){ var img_dir = '<?php echo $this->webroot . "img/img_jquery.ajaxComboBox/"; ?>'; //id=suggestに対してオートコンプリートを設定 $('#combobox1').ajaxComboBox( '<?php echo $html->url('ajaxComboBox'); ?>', { 'db_table' : 'Prefecture', 'img_dir' : img_dir, 'field' : 'name', 'order_field' : 'id', 'order_by' : 'ASC' } ); }); </script> <p> <label for="combobox1">都道府県</label> <?php echo $form->text( "Demo.prefecture", array("id"=>"combobox1") ); ?> </p>