0

how can i checkbox to be checked based on the value in the database .if the status(is_closed) is 1 then the checkbox to be checked

 echo $form->field($model, 'is_closed')->checkbox(['id'=>'checkbox-'.$value->action_status_id,'class'=>"input_class input_checkbox",'value'=>$value['is_closed'],
    'onClick'=> new yii\web\JsExpression("
        var tableUsed = 'status';
        var obj=$(this);
        var id = $(this).attr('id').split('-');
        var dInput= $(this).val();
        $.ajax({
           type : 'POST',
           data : {'id':id[1]},
           url: '".Yii::$app->getUrlManager()->createUrl("actions/setting-checkbox-update")."',
           success: function(data){
            var tableRow = obj.closest('tr'); 
            tableRow.find('.saving').show();
            setTimeout(function(){ tableRow.find('.saving').hide();}, 500);
            stat=JSON.parse(data)
            tableRow.find('.input_checkbox').prop('checked', stat.closed== 1);
          }  
        });
")])->label(FALSE);

$value['is_closed] have value 1 or 0 based on the database value

mplungjan
  • 169,008
  • 28
  • 173
  • 236
jane
  • 71
  • 1
  • 7
  • where are you trying to load this value is it the edit form? as Yii automatically assigns the value to the input if you have set it before sending the model to the view either by manually setting like `$model->is_closed=1` or if you are selecting the existing records in case of edit , it will set automatically if the value is saved in the database table. – Muhammad Omer Aslam May 14 '18 at 13:10
  • and you are using `$value` for the column `is_closed` ? isnt the value coming from the same model you are populating the form with `$model`? – Muhammad Omer Aslam May 14 '18 at 13:13
  • yes it is in the edit form...$model->is_closed=1 yes i tried that one its not working – jane May 14 '18 at 13:15
  • remove `'value'=>$value['is_closed']` from the `checkbox()` function – Muhammad Omer Aslam May 14 '18 at 13:18
  • i removed that 'value'=>$value['is_closed'] .Now all are checked if the is_closed is 0 also the checkbox is checked – jane May 14 '18 at 13:25
  • strange you need to add all the form fields of type checkboxes to see if they are also using the same column? to be checked – Muhammad Omer Aslam May 14 '18 at 13:48
  • yes same column – jane May 14 '18 at 14:39

0 Answers0