forecho

把生命浪费在美好的事物上

Yii全选删除

2013年05月30日

目前功能还是能用,但是删除不是Ajax操作的。等待下次完善。 Views:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<form action="/member/msglog/deleteordmsg" method="post" id="deleteForm">
	<?php
	    $this->widget('zii.widgets.CListView', array(
	        'dataProvider'=>$dataProvider,
	        'itemView'=>'_view',
	        'emptyText'=>'暂时没有数据',
	       	'template'=>'{items}{pager}',
	        'pager'=>array('class'=>'CLinksPager'),
	        'itemsTagName'=>'table',
	        'itemsOptions'=>array('class'=>'list_table', 'width'=>'100%'),
	        'buttonCssClass'=>'bg btn_page',
	        'batchItemOptions'=>array('class'=>"filter f_l"),
	        'batchItem'=>array(
	            //删除
	            '<input id="allcheckbox1" onchange="checkAll(\'select-on-check\',\'allcheckbox1\');" class="chk select-on-check" type="checkbox" name="">全选
                 <button class="mem_bgx link_btn mr5" onclick="$(\'#deleteForm\').submit();" title="">删除选中</button>'			        ),
	    ));
	?>
</form>

_view.php文件:

1
2
3
4
5
6
<tr>
	<td width="6%"><input class="select-on-check chk f_l" type="checkbox" name="ids[]" value="<?php echo $data->id; ?>" /></td>
    <td width="15%"><?php echo $data->fromMember->name;?></td>
    <td width="58%"><p class="pub_tit02"><?php echo CHtml::link($data->title, array('/member/msgLog/inboxDetail', 'id'=>$data->id));?></p></td>
    <td><?php echo date("Y-m-d H:i:m", $data->inputtime);?></td>
</tr>

*jQuery验证是否有选择:**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script type="text/javascript">
//表单验证
$('#deleteForm').bind('submit',function(){
	var data=new Array();
	$("input:checkbox[name='ids[]']").each(function ()
    	{
		if($(this).attr("checked")=='checked')
		{
			data.push($(this).val());
		}
	});
	if(data.length > 0)
	{
	return true;
    }else{
        return false;
    }
})
</script>

jQuery全选:

1
2
3
4
5
6
7
/*全选
classs:被选框class
selfid:全选框id
*/
function checkAll(classs,selfid){
	$('.'+classs).attr('checked',$('#'+selfid).attr('checked')?'checked':false);
}

Controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//删除选中
public function actionDeleteordmsg()
{
	if (Yii::app()->request->isPostRequest)
	{
		$criteria= new CDbCriteria;
		$criteria->addInCondition('id', $_POST['ids']);
		MsgLog::model()->deleteAll($criteria);

		if(isset(Yii::app()->request->isAjaxRequest)) {
			$message = '删除成功。';
			$path = '../index/success';
		} else{
			$message = '删除失败。';
			$path = '../index/error';
		}
	}
	$this->render($path,array(
			'message'=>$message,
		));
}