如何识别在 cakephp 3.x 中选择的按钮?
How to identify button selected in cakephp 3.x?
我查看了答案 Which Submit Button was Clicked in CakePHP?。这种情况不适用于我,因为我对每个按钮都有相同的操作。
我想重复使用 bootstrap 模态,我想知道调用模态时选择了哪个项目。非常简单,我有一个 table 每个学校对象的成绩。当用户单击添加按钮时,我想调用模式并为该对象添加成绩。我想知道选择了哪个对象,因为我想为所有对象重用模态。我怎样才能在 cakephp 3.x 中做到这一点?
在老师想要添加成绩并按下 + 按钮后,如果我使用相同的成绩保存模式,我如何知道 he/she 选择了数学或英语? 。
okey,最简单的方法是在模式中有隐藏字段,其中包含一个主题。我认为这与cakephp没有太大关系。
示例应如下所示:
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>
确定按下哪个按钮的唯一方法是使用 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 没有 "modal takedown" ).
为了尽可能接近 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 语法,但您也可以使用 form[name=subject_id] 并去掉 id 子句
在 HTML 中的浏览器端创建您的按钮:
<button type="button" class="btn btn-primary" onclick="launch_dialog('MATH')">Add Math</button>
在 javascript 中的浏览器端,单击按钮时调用的函数,它在表单中设置主题 ID,然后启动 modal/form:
<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; ?>
我查看了答案 Which Submit Button was Clicked in CakePHP?。这种情况不适用于我,因为我对每个按钮都有相同的操作。
我想重复使用 bootstrap 模态,我想知道调用模态时选择了哪个项目。非常简单,我有一个 table 每个学校对象的成绩。当用户单击添加按钮时,我想调用模式并为该对象添加成绩。我想知道选择了哪个对象,因为我想为所有对象重用模态。我怎样才能在 cakephp 3.x 中做到这一点?
okey,最简单的方法是在模式中有隐藏字段,其中包含一个主题。我认为这与cakephp没有太大关系。 示例应如下所示:
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>
确定按下哪个按钮的唯一方法是使用 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 没有 "modal takedown" ).
为了尽可能接近 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 语法,但您也可以使用 form[name=subject_id] 并去掉 id 子句
在 HTML 中的浏览器端创建您的按钮:
<button type="button" class="btn btn-primary" onclick="launch_dialog('MATH')">Add Math</button>
在 javascript 中的浏览器端,单击按钮时调用的函数,它在表单中设置主题 ID,然后启动 modal/form:
<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; ?>