视图层
<style>
#tab tr td{
height:40px;
100px;
}
</style>
<form action="index.php?r=ceshi/index" method='post'>
<input name="_csrf" type="hidden" id="_csrf" value="<?= Yii::$app->request->csrfToken ?>">
<table id="tab">
<tr>
<td></td>
<td>显示顺序</td>
<td>站点名称</td>
<td>站点URL</td>
<td></td>
</tr>
<?php foreach($info as $key=>$v): ?>
<tr>
<input type="hidden" name="cid[]" value="<?php echo $v['id']; ?>" />
<td><input type="checkbox" name="check[]" class="ids" value="<?php echo $v['id'] ?>"></td>
<td><input type="text" name="order[]" value="<?php echo $v['order']; ?>" size="3" /></td>
<td><input type="text" name="title[]" value="<?php echo $v['title']; ?>" size="8" /></td>
<td><input type="text" name="url[]" value="<?php echo $v['url']; ?>" /></td>
<td></td>
</tr>
<?php endforeach; ?>
<tr>
<td></td>
<td colspan="4"><a href="javascript:;" onclick="jia(this)" >+添加友情链接</a></td>
</tr>
<tr>
<td><input type="checkbox" id="all" onclick="getAll(this)" />删?</td>
<td colspan="4">
<input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
<script>
//点击添加一行
function jia(obj){
var tr=$(obj).parent().parent();
//点击加一行
var html='<tr><td></td><td><input type="text" name="order[]" size="3" /></td><td><input type="text" name="title[]" size="8" /></td><td><input type="text" name="url[]" /></td><td><input type="button" value="删除" onclick="del(this)" /></td></tr>';
tr.before(html);
}
//点击删除当前行
function del(obj){
$(obj).parent().parent().remove();
}
//全选
function getAll(ts){
var ids = $('.ids');
//alert(ids.length);
for(var i=0;i<ids.length;i++){
if(ts.checked==true){
ids[i].checked=true;
}else{
ids[i].checked=false;
}
}
}
</script>
控制器层
<?php
namespace frontendcontrollers;
use Yii;
use frontendmodelsCeshi;
use yiidbQuery;
class CeshiController extends yiiwebController
{
public function actionIndex()
{
//判断是否为提交表单
if(Yii::$app->request->ispost){
//判断是否为删除操作
if(Yii::$app->request->post('check')){
$check = Yii::$app->request->post('check');
//var_dump($check);die;
$str = '';
for($i=0 ; $i<count($check) ; $i++){
$sql = "delete from ceshi where id=".$check[$i];
if(Yii::$app->db->createCommand($sql)->execute()){
$str++;
}
}
if($str!=''){
echo "<script>alert('删除成功'); location.href='index.php?r=ceshi/index'; </script>";
}
}else{
//执行修改和添加操作
//var_dump(Yii::$app->request->post());die;
$cid = Yii::$app->request->post('cid');
$orders = Yii::$app->request->post('order');
$titles = Yii::$app->request->post('title');
$urls = Yii::$app->request->post('url');
$flage = '';
foreach ($orders as $k=>$v){
//判断是否有ID传值
if(!empty($cid[$k])){
$order = $v;
$title = $titles[$k];
$url = $urls[$k];
$id = $cid[$k];
//echo $id;exit;
$res = Yii::$app->db->createCommand()->update('ceshi', [
'order' => $v,
'url' => $url,
'title' => $title
] , "id=$id")->execute();
//数据可能没被修改,只有成功一条就改变标记的值
if($res){
$flage = '1';
}
}else{
$order = $v;
$title = $titles[$k];
$url = $urls[$k];
$res = Yii::$app->db->createCommand()->insert('ceshi', [
'order' => $v,
'url' => $url,
'title' => $title
])->execute();
if($res){
$flage = '1';
}
}
}
//判断是否成功
if($flage=='1'){
echo "<script>alert('修改/添加成功'); location.href='index.php?r=ceshi/index'; </script>";
}else{
echo "<script>alert('无任何操作'); location.href='index.php?r=ceshi/index'; </script>";
}
}
}
$query = new Query();
//查询所有数据
$info = $query
->select('*')
->from('ceshi')
->orderBy(['order' => SORT_ASC])
->all();
return $this->render('index' , ['info' => $info]);
}
}
另一种表单,0-0
<?php
use yiihelpersHtml;
use yiiwidgetsActiveForm;
$form = ActiveForm::begin(['method'=>'post','action' => 'index.php?r=test/index']);
?>
<style>
#tab tr td{
height:40px;
100px;
}
</style>
<table id='tab'>
<tr>
<td><input type='checkbox' /></td>
<td>显示顺序:</td>
<td>站点名称:</td>
<td>站点地址:</td>
</tr>
<?php foreach ($data as $key=> $v):?>
<tr id='tr1'>
<input type="hidden" name="cid[]" value="<?php echo $v['id']; ?>" />
<td><input type='checkbox' name='check[]' class='ids' value="<?php echo $v['id']?>" /></td>
<td><input type='text' name='t_xu[]' value="<?= $v['t_xu']?>" size="3"/></td>
<td><input type='text' name='t_name[]' value="<?= $v['t_name']?>"size="8" /></td>
<td class='td1'><input type='text' name='t_url[]' value="<?= $v['t_url']?>" /></td>
</tr>
<?php endforeach; ?>
<tr id='tr2'></tr>
</table>
<input type='button' value='+' id='btn' />添加友情链接
<p><input type='checkbox' onclick='getAll(this)' />
删?
<input type='submit' id='btnok' value='提交'/></p>
<?php ActiveForm::end()?>
<?php $this->beginBlock('checkAgree');?>
$('#btn').on('click',function()
{
//var trhtml = "<td><input type='checkbox' name='id' /></td><td><input type='text' name='t_xu' /></td><td><input type='text' name='t_name'/></td><td><input type='text' name='t_url' /></td>";
//alert(trhtml);
//$('#tr2').append(trhtml);
var tab=document.getElementById('tab');
var n=document.getElementById('tr2').rowIndex+1;
var tr=tab.insertRow(n);
var td=tr.insertCell(0);
tr.innerHTML="<td></td><td><input type='text' name='t_xu[]' size='3' /></td><td><input type='text' name='t_name[]' size='8'/></td><td><input type='text' name='t_url[]' /><td><input type='button' value='删除此行' onclick='deltr(this)' /></td>";
});
function deltr(ts){
var tr = $(ts).parent().parent();
tr.remove();
}
function getAll(ts)
{
var ids = $('.ids');
//alert(ids.length);
for(i=0; i<ids.length; i++)
{
if(ts.checked == true)
{
ids[i].checked = true;
} else {
ids[i].checked = false;
}
}
}
<?php $this->endBlock(); $this->registerJs($this->blocks['checkAgree'], yiiwebView::POS_END);?>