我通过循环遍历会话数组中的项目在视图中创建了一个列表。我什至为每个迭代都放置了复选框。我需要将检查的值作为字符串或字符串数组输入到控制器中。我怎样才能做到这一点。这是视图部分。
addEventListener
然后我使用javascript部分触发了复选框,并且成功触发了复选框的检查值。但是我无法将其发送到控制器。
<?php
$session = Yii::$app->session;
$array = explode('\r\n', $session['cdr_val']) ;
?>
<div class="box-header with-border">
<h3 class="box-title">CDR Configurations</h3>
<?= Html::a('<span class="glyphicon pull-right glyphicon-transfer"></span>', ['#'], ['data-url' => Url::toRoute(['cdr/compare']), 'id' => 'btn-compare', 'title' => 'Compare']); ?>
</div>
<div class="box-body">
<!--form class="form-horizontal" name="form_blacklist_table" id=""-->
<div class="row">
<div class="box-body boxpad contSeperator">
<div class="col-md-12 col-sm-12 col-xs-12">
<table id="analyse-table" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th class="actions-fix-wd-1 text-center">Select</th>
<th>CDR</th>
<th class="actions-fix-wd-2 text-center">Action</th>
</tr>
</thead>
<tbody>
<?php if ($array[0]!=''){ ?>
<?php $j = 0 ?>
<?php do{ ?>
<tr>
<td class="text-center">
<!--?=
//$form->field($model, 'status')->checkbox(['value' => "$j",'encode'=>false,'label'=>null]) ?-->
<input name="checkbox1" id="checkbox1" type="checkbox" value="<?= $j ?>">
</td>
<td>
<div>
<?php
echo($array["$j"]);
?>
</div>
</td>
<td class="text-center actions">
<?= Html::a('<span class="glyphicon glyphicon-check"></span>', ['#'], ['data-url' => Url::toRoute(['cdr/analyze','id'=>$array["$j"]]), 'id' => 'btn-view', 'title' => 'analyze']); ?>
</td>
<?php $j++ ?>
</tr>
<?php }while($j<sizeof($array)); ?>
<?php } ?>
这是必要的控制器部分
$urlView1 = Url::to('cdr/compare');
$script1 = <<< JS
$( document ).ready(function() {
$(document).on('click', '#btn-compare', function(e) {
var items=document.getElementsByName('checkbox1');
var selectedItems="";
for(var i=0;i<items.length;i++){
if(items[i].type=='checkbox' && items[i].checked==true){
selectedItems+=","+items[i].value;
}
}
//console.log("{$urlView1}")
/*$.ajax({
url : "http://localhost:8080/index.php?r=cdr/compare",
type : 'post',
data: 'items=' + selectedItems,
success : function(data){
console.log(data);
$('#view-comp').attr("src", $(this).attr('data-url'));
$('#ViewModal').modal({show:true})
var data = JSON.parse(data);
},
});*/
$('#view-comp').attr("src", $(this).attr('data-url'));
$('#ViewComp').modal({show:true})
return false;
});
});
JS;
$this->registerJs($script1);
并且模型具有定义并返回的变量。
答案 0 :(得分:1)
尝试一下:
...
<?php if (!empty($array[0])): ?>
<?php foreach($array as $key => $value): ?>
<tr>
<td class="text-center">
<!--?=
//$form->field($model, 'status[]')->checkbox(['value' => "$key",'encode'=>false,'label'=>null]) ?-->
<input name="checkbox1[]" id="checkbox1" type="checkbox" value="<?= $key ?>">
</td>
<td>
<div>
<?= $value ?>
</div>
</td>
<td class="text-center actions">
<?= Html::a('<span class="glyphicon glyphicon-check"></span>', ['#'], ['data-url' => Url::toRoute(['cdr/analyze','id'=> $value]), 'id' => 'btn-view', 'title' => 'analyze']); ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
...
希望有帮助