我在CakePHP 3环境中,在该环境中,我在“文章”控制器中执行“编辑操作”。
控制器上的“第一功能”是标准编辑功能。
这会以表格的形式加载文章的现有数据,因此您可以轻松地对其进行编辑。
public function edit($id){
$article_categories = $this->Articles->Articlecats->find('list');
$this->set('article_categories', $article_categories);
$article = $this->Articles->get($id);
// if you are not just landing on page but making the save
if ($this->request->is(['post', 'put'])) {
// disable entity creation, not entirely sure what this does..
$this->Articles->patchEntity($article, $this->request->getData());
// Set the user_id from the session.
$article->user_id = $this->Auth->user('id');
if ($this->Articles->save($article)) {
$this->Flash->success(__('Your article has been updated.'));
return $this->redirect(['action' => 'index']);
}
// check if there are errors in editing and print them
if ($article->getErrors()){
// Entity failed validation
// print_r($article->getErrors());
$this->Flash->error(__('Please correct values and try again.'));
}
$this->Flash->error(__('Unable to update your article.'));
}
$this->set('article_pass', $article);
}
ArticlesController的edit方法中的视图显示输入字段,您可以在其中输入Article信息,然后将其保存到基础数据库中。在视图的底部,有一个“检查功能的按钮”按钮,该按钮通过Ajax调用访问ArticlesController上的方法。 ArticlesController上的函数editcategory()检索有关所选类别的信息,然后基于此信息返回渲染视图。
单击此按钮后,我将类别和类别信息加载到屏幕上的新表格中。
<?php echo $this->Html->script('http://code.jquery.com/jquery.min.js'); ?>
<!-- File: templates/Articles/edit.php -->
<h1>Edit Article</h1>
<?php
echo $this->Form->create($article_pass);
echo $this->Form->control('user_id', ['type' => 'hidden']);
echo $this->Form->control('title');
echo $this->Form->control('body', ['rows' => '3']);
echo $this->Form->select('articlecat_id', $article_categories, ['id' => 'article_cat']);
echo $this->Form->button(__('Save Article'));
echo $this->Form->end();
$csrfToken = $this->request->getParam('_csrfToken');
// self written element in View
// echo $this->articlecatCreate->create();
?>
<button id="edit_category">Button to check functionality</button>
<div id="ajax_data"></div>
<script>
var csrf_token = <?php echo json_encode($csrfToken) ?>;
$(window).load(function() {
// get article category data from controller method through ajax call
// populate the form with them
$('#edit_category').click(function(){
var targeturl = "http://wampprojects/composer_cakephptut/articles/editcategory";
var category_selected = $('#article_cat').children("option:selected").val();
console.log(category_selected);
// need to check if this statement works??
if ($('#article_cat').children("option:selected").val()){
$.ajax({
type: 'post',
url: targeturl,
headers: {'X-CSRF-Token': csrf_token},
data: {category: category_selected},
success: function(result){
console.log(result);
// create a form with a POST action that links to database
$('#ajax_data').html(result);
}
});
}else{
console.log("no cat selected");
}
});
});
</script>
Articles控制器中的“ editcategory()”方法。
public function editcategory(){
$this->loadModel('Articlecats');
$category_id = $this->request->getData('category');
$articlecat_data = $this->Articlecats->get($category_id);
$this->set('articlecat_pass', $articlecat_data);
debug($articlecat_data);
$this->render('element/articlecatcreate', 'ajax');
}
在ArticlesController的editCategory()方法中,我检索有关所选类别的信息。然后,我渲染一个依赖于我检索到的数据的助手视图,称为“ articlecatcreate”。
<h1>Edit ArticleCategory</h1>
<div>
<?php echo $articlecat_pass['id'] ?>
<?php echo $articlecat_pass['title'] ?>
<?php
// you miss a primary key, it does try to save to the right table
echo $this->Form->create($articlecat_pass);
echo $this->Form->control('user_id', ['type' => 'hidden']);
echo $this->Form->control('title');
echo $this->Form->button(__('Save Article'));
echo $this->Form->end();
?>
</div>
它将通过“ $('#ajax_data')。html(result);”方法正确地将“文章类别”数据加载到视图的新部分中,该新部分已添加到现有视图中。在ajax成功函数中。
保存要尝试编辑的类别时,出现以下错误。
错误:“在表“ articlecats”中找不到具有主键[NULL]的记录”
因此,添加到视图中的新创建的表单试图保存在正确的表和模型上,但是没有以某种方式将密钥加载到表单的create方法中。
我该如何进行?
找到的解决方案
我找到了在form-> create方法$ options参数中设置控制器和操作的解决方案。
因此,您渲染的元素的代码必须为:
<h1>Edit ArticleCategory</h1>
<div>
<?php
// you miss a primary key, it does try to save to the right table
echo $this->Form->create($articlecat_pass, ['url' => ['controller' => 'Articlecats', 'action' => 'edit']]);
echo $this->Form->control('user_id', ['type' => 'hidden']);
echo $this->Form->control('title');
echo $this->Form->button(__('Save Article'));
echo $this->Form->end();
?>
</div>
最佳答案
您实际上可以使用javascript创建新的widgits或任何新的DOM节点。考虑一下我在附近的一个随机项目的jQuery代码片段:
$(this).replaceWith(
'<a class="remove" id="' + $(this).attr('id')
+ '" href="' + webroot + 'shop/remove/' + $(this).attr('id')
+ '" title="Remove item">'
+ '<img src="' + webroot + 'img/icon-remove.gif" alt="Remove" />' +
'</a>'
);
您可以轻松获得对第二种形式的引用:
$('form#id_of_form')
但是,我仍然发现,当Cake只是一个ajax调用时,在jQuery和javascript中进行的所有此类麻烦对于构建填充表单都有些乏味。
因此,您的成功功能可能类似于
$('form#id_of_form').replaceWith(new_form_from_ajax_call);
关于javascript - CakePHP 3.8根据用户选择更改表单数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59930051/