2

I have added check box to each row in cgridview, and a button Delete Selected to delete the selected rows, now what i want to do is until a check box is checked, disable the Delete Selected button, how do i do this.? Here is the code of my admin view:

<?php
$form = $this->beginWidget('CActiveForm', array(
        'id' => 'account-form',
        'action' => array('site/DeleteMultiple'),
        'enableAjaxValidation' => true,
));

 $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'assign-grid',
    'dataProvider'=>$model->search(),
    'selectableRows' => 2,
    'filter'=>$model,
    'columns'=>array(
        array(
            'id' => 'id',
            'class' => 'CCheckBoxColumn',
        ),
        array(
            'name'=>'username',
            'value'=>'$data->user->username',
        ),
        array(
            'name'=>'Firstname',
            'value'=>'$data->user->firstname',
        ),
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>
<div id="submit-button" style="display:none">
<?php 
echo CHtml::SubmitButton('Delete Selected', array('class' => 'button')); 
?>
</div>
<?php $this->endWidget();?>

2 Answers 2

3

Change your button as below

echo CHtml::SubmitButton('Delete Selected', array('class' => 'button','disabled'=>'true'));

Then add a JS function:

$("#assign-grid input[type=checkbox]").click(function()
{
   if($("#assign-grid").find("input:checked").length >0)
      $(".button").removeAttr("disabled");
   else
      $(".button").attr("disabled","true");
});
Sign up to request clarification or add additional context in comments.

2 Comments

when you click on any checkbox(line 1), 3rd line code will count the checkboxes which are all checked.
it is not working the button is being shown (disabled) but after clicking on any checkbox it is not getting enabled..what might be wrong?
2

just try this, create a jQuery / javascript function for show the div and call it in onclick of the check box

Like this

array( 'id' => 'id', 'class' => 'CCheckBoxColumn', 'onclick' => 'fn_onclick()' ),

2 Comments

It is giving an error Property "CCheckBoxColumn.onclick" is not defined.
please try this array( 'id' => 'id', 'class' => 'CCheckBoxColumn', 'htmlOptions'=>array('onclick'=> 'fn_onclick()')),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.