我查看了答案Which Submit Button was Clicked in CakePHP?。这种情况不适用于我,因为我对每个按钮都有相同的操作。
我想重用一个bootstrap模式,我想知道在调用模态时选择了哪个项目。很简单,我有一张表,每个学校对象都有成绩。当用户单击添加按钮时,我想调用模态并为该对象添加等级。我想知道选择了哪个对象,因为我想重用所有对象的模态。我怎么能在cakephp 3.x中做到这一点?
答案 0 :(得分:1)
function modalopen(subject) {
$('#modal #subject').val(subject);
$('#modal').modal('toggle');
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
</head>
<body>
<button type="button" class="btn btn-info" onclick="modalopen('english')">+</button>
<button type="button" class="btn btn-info" onclick="modalopen('math')">+</button>
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id=""></h4>
</div>
<div class="modal-body">
sub (will be hidden):<br>
<input type="text" name="subject" id ="subject" value="" placeholder="will be hidden"><br>
Mark:<br>
<input type="text" name="mark" id ="mark" value="" placeholder="mark">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
</body>
</html>
答案 1 :(得分:1)
确定按下哪个按钮的唯一方法是使用Javascript。这意味着不使用按钮上的基于html-tag-option的方法来启动模态,即:
<!-- Button trigger modal: CAN *NOT* USE THIS TECHNIQUE!! -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
我认为你的模态中的表单是一个蛋糕生成的表单,而提交按钮是一个正常的表单提交,它触发了页面的重绘,有效地杀死了模态(IE没有“模态删除”)。 / p>
为了尽可能接近Cake的范例,我会将它以隐藏的形式发送回服务器。
类似的东西:
在创建表单时,在ctp的蛋糕面上:
// HTML WRAPPER FOR MODAL
<?= $this->Form->create(yourentity); ?>
(your form stuff)
<?= $this->Form->hidden("subject_id",["id"=>"subject-id-field"]);
(end of form stuff including submit)
<?= $this->Form->end(); ?>
// HTML WRAPPER FOR MODAL
这将在您的表单中生成类似
的内容<input type="hidden" name="subject_id" id="subject-id-field"/>
我们需要在Javascript中抓取这个隐藏字段,所以我给它一个名字(特定于表单)和一个id(全局),因为我更喜欢用#id-syntax引用所有内容,但是你也可以使用form [name = subject_id]并删除id子句
在HTML的浏览器端,创建按钮:
<button type="button" class="btn btn-primary" onclick="launch_dialog('MATH')">Add Math</button>
在javascript的浏览器端,单击按钮时调用的函数,它在表单中设置主题ID,然后启动模式/表单:
<script>
function launch_dialog(subject) {
$("#subject-id-field").val(subject); // use the id of the hidden field
$("#your-modal").modal("show"); // or whatever you do to launch the modal
}
</script>
在表单目标函数的服务器端:
# in your controller php file
function formAction() {
if($this->request->data["subject_id"]=="MATH") { // use the name of the hidden field
// do math record
}
// etc
}
另一个注意事项 - 如果你的成绩记录确实有一个subject_id字段属于一个主题记录,你可以让按钮的onclick函数用该常量调用launch_dialog函数,然后你不需要服务器内的任何IF函数行动代码。只需确保使用原始记录生成id,例如:
在渲染之前的控制器中:
$this->set("subjects",$this->[entity]->Subjects->find("list");
在ctp文件中,类似于:
<?php foreach($subjects as $id=>$name): ?>
<button type="button" class="btn btn-primary"
onclick="launch_dialog(<?= $id ?>)">Add <?= $name ?></button>
<?php endforeach; ?>