我正在使用 CakePHP 2.x。我希望在 jquery ui 的模态对话框关闭(提交后)后,我的页面中的下拉列表使用新值刷新,而不是整个页面刷新。我怎样才能做到这一点?
在我的 ArticlesController.php 上
public function admin_add() {
//setting active link
$this->set('active', 'new_article');
if ($this->request->is('post')) {
$this->request->data['Article']['posted'] = date('Y-m-d H:i:s');
$this->Article->create();
if ($this->Article->save($this->request->data)) {
$this->Session->setFlash(__('The article has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The article could not be saved. Please, try again.'));
}
}
$categories = $this->Article->Category->find('list');
$this->set(compact('categories'));
}
在我的查看页面(admin_add.php)
<div>
<?php echo $this->Form->create('Article');?>
<?php
echo $this->Form->input('title');
echo $this->Form->input('author');
//THIS IS THE DROPDOWN PART
echo $this->Form->input('category_id');
//THIS IS THE PART FOR CALLING THE MODAL DIALOG TO ADD A NEW CATEGORY
echo '<div class="addNewCategory">';
echo 'add';
echo '</div>';
?>
<?php echo $this->Form->end(__('Submit'));?>
</div>
<!-- Modal Dialog for Adding New Category (THIS IS HIDDEN UNTIL GET CALLED AS MODAL DIALOG)-->
<div class="dialog-form" title='Create new Category'>
<?php echo $this->Form->create('Category');?>
<?php
echo $this->Form->input('name');
?>
<?php echo $this->Form->end(__('Submit'));?>
</div>
<script type="text/javascript">
$('.dialog-form').dialog({
autoOpen: false,
title: 'New Category',
modal: true,
height: 300,
width: 350,
close: function() {
}
});
$('.addNewCategory').click(function() {
$('.dialog-form').dialog('open');
});
$('.dialog-form .submit').click(function(e) {
var value = document.getElementById('CategoryName').value;
var data = {'data[Category][name]':value};
e.preventDefault();
$.ajax({
type:"POST",
url: "<?php echo $this->Html->url(array('controller' => 'categories', 'action' => 'addmodal', 'admin' => TRUE)); ?>",
data: data,
success: function() {
$('.dialog-form').dialog('close');
//WHAT SHOULD I DO IN HERE AFTER THE DIALOG CLOSED TO UPDATE THE DROPDOWN OR IF ITS NOT IN HERE PLEASE LET ME KNOW THE SOLUTION
}
})
});
</script>
以上是控制器和视图和脚本的一些代码。大写的评论是我的解释。谢谢