1
問題: 我有一個jQuery
腳本在我的控制器的add方法的工作,但是當我添加搜索功能來編輯方法的下拉菜單無法正常工作;但我知道jQuery
腳本正在工作,因爲我得到的結果反饋回來。我可能忽略了一些簡單的東西,但我似乎無法發現它。jQuery的自動完成構件
控制器編輯方法:
public function edit($id = null) {
$this->loadModel('PsychproScreening');
$this->set('pidList', $this->PsychproScreening->find('list', array('fields' => array('pid','pid'),
'order' => 'pid ASC')));
if (!$this->PsychproConcomitantMed->exists($id)) {
throw new NotFoundException(__('Invalid psychpro concomitant med'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->PsychproConcomitantMed->save($this->request->data)) {
$this->Session->setFlash(__('The psychpro concomitant med has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The psychpro concomitant med could not be saved. Please, try again.'));
}
} else if(!$this->RequestHandler->isAjax()) {
$options = array('conditions' => array('PsychproConcomitantMed.' . $this->PsychproConcomitantMed->primaryKey => $id));
$this->request->data = $this->PsychproConcomitantMed->find('first', $options);
}
if ($this->RequestHandler->isAjax()) {
debug('here');
$this->search();
}
}
控制器搜索方法:
private function search(){
$this->loadModel('FdaMed');
$this->autoRender=false;
$meds=$this->FdaMed->find('all',array('fields' => array('med_id','non_proprietary_name', 'route_name', 'dosage_for_mname'),
'conditions'=>array('FdaMed.non_proprietary_name LIKE'=>'%'.$_GET['term'].'%')));
$i=0;
foreach($meds as $meds){
$response[$i]['value']=$meds['FdaMed']['med_id'];
$response[$i]['label']=$meds['FdaMed']['non_proprietary_name'].' ('.
$meds['FdaMed']['route_name'].'/ '.$meds['FdaMed']['dosage_for_mname'].')';
$i++;
}
echo json_encode($response);
}
編輯視圖
<?php include_once 'concomitant_meds_inc.php';
$this->Html->addCrumb('PsychPro', '/Psychpros');
$this->Html->addCrumb('PsychPro Concomitant Med', '/PsychproConcomitantMeds');
$this->Html->addCrumb('Edit Concomitant Med', '/PsychproConcomitantMeds/edit/'.$this->request->data['PsychproConcomitantMed']['id']);
?>
<div class="psychproConcomitantMeds form">
<?php echo $this->Form->create('PsychproConcomitantMed',array('action' => 'edit')); ?>
<fieldset>
<legend><?php echo __('Edit Psychpro Concomitant Med'); ?></legend>
<?php
echo $this->Form->input('pid', array('options' => $pidList, 'empty' => 'Choose a value'));
echo $this->Form->input('timept', array('options' => $tpt, 'empty' => 'Choose a value'));
echo '<h3>Current Medication: '.$this->request->data['PidConcomitantMed']['non_proprietary_name'].' Route: '.
$this->request->data['PidConcomitantMed']['route_name']." Dosage Type: ".
$this->request->data['PidConcomitantMed']['dosage_for_mname'].'</h3>';
echo $this->Form->input('med_name',array('type'=>'text','id'=>'med_name','label'=>'Medication Name (Route/Dosage Type)'));
echo $this->Form->hidden('med_id', array('id' => 'med_id'));
echo $this->Form->input('meds_taken');
echo $this->Form->hidden('id', array('id' => 'p_form_id'));
echo $this->Form->hidden('update_initials',array('value' => $this->Session->read('CoreUser.user_initials')));
echo $this->Form->hidden('update_time_stamp', array('value' => date("Y-m-d h:i:s")));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('PsychproConcomitantMed.id')), null, __('Are you sure you want to delete # %s?', $this->Form->value('PsychproConcomitantMed.id'))); ?></li>
<li><?php echo $this->Html->link(__('List Psychpro Concomitant Meds'), array('action' => 'index')); ?></li>
</ul>
</div>
<?php echo $this->Html->script('autocomplete_edit'); ?>
jQuery的自動完成
$(document).ready(function(){
// Defining a placeholder text:
$('#med_name').defaultText('Search for medications');
console.log($('#p_form_id').val());
var form_id = $('#p_form_id').val();
$('#med_name').autocomplete({
minLength : 3,
source : 'http://127.0.0.1/my_cakephp/PsychproConcomitantMeds/edit/'+form_id,
autoFocus : true,
select : function(event,ui){
//console.log(ui.item['value']);
$('#med_id').val(ui.item['value']);
//console.log($('#med_id'));
}
});
});
// A custom jQuery method for placeholder text:
$.fn.defaultText = function(value){
var element = this.eq(0);
element.data('defaultText',value);
element.focus(function(){
if(element.val() == value){
element.val('').removeClass('defaultText');
}
}).blur(function(){
if(element.val() == '' || element.val() == value){
element.addClass('defaultText').val(value);
}
});
return element.blur();
}
調試
我知道是工作如下:
- 使用內置的開發人員工具在鉻我從
jQuery
與jQuery
響應以{Value: , Label: }
的格式保存搜索結果的數組。 - 我知道這
jQuery
代碼的作品,因爲我有類似的代碼用於添加方法。如果你想看到代碼讓我知道,我會更新。唯一真正的區別是我需要獲取記錄的id並將其追加到jQuery
腳本中的URL末尾。 - 我在Response中得到了正確的url。它看起來像
../view/21?term=insulin
更新0:
一些調試後我發現問題出在哪裏發生。在add
方法的jQuery
腳本中,這些日誌將被打印,但edit
方法沒有。
$('#med_name').autocomplete({
minLength : 3,
source : 'http://127.0.0.1/my_cakephp/PsychproConcomitantMeds/edit/'+form_id,
autoFocus : true,
select : function(event,ui){
console.log(ui.item['value']);
$('#med_id').val(ui.item['value']);
console.log($('#med_id'));
}
});
所以我知道搜索功能正在工作,但select事件沒有被觸發。下拉菜單也沒有出現,我覺得很奇怪,因爲jQuery
顯然是在工作,因爲它得到了結果,它們只是無法顯示。