Magento2 – Show Checkbox Checked in Grid Edit Mode Based on Value

checkboxeditgridmagento2

I have 2 checkboxes

$fieldset->addField(
        'call_me_in_morning',
        'checkbox',
        array(
            'name' => 'call_me_in_morning',
            'label' => __('Call me in morning'),
            'title' => __('call me in morning'),
            'data-form-part' => $this->getData('career-form'),
            'onchange'   => 'this.value = this.checked ? 1 : 0;',
            
        )
    );
    $fieldset->addField(
        'call_me_in_aftenoon',
        'checkbox',
        array(
            'name' => 'call_me_in_aftenoon',
            'label' => __('Call me in aftenoon'),
            'title' => __('call me in aftenoon'),
            'data-form-part' => $this->getData('career-form'),
            'onchange'   => 'this.value = this.checked ? 1 : 0;',
            
        )
    );

enter image description here

Getting right values for both checkboxes.
You can see in following image
Getting value = '1' for 1st
and value = '0' for second

But when I'm going to edit row of grid then it remains unchecked.

What should I do to show checked whose value = '1'

Note : Highlited box in inspect element is for 2nd checkbox and for 1st is above it.

Best Answer

To achieve this functionality I had changed my html:

  <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <div class="checkbox checkbox-primary checkbox-inline">
      <input id="morning" type="checkbox" value="Morning" name="call_me[]">
      <label for="morning" class="checkbox-inline">Call me in the morning</label>
    </div>
    <div class="checkbox checkbox-primary checkbox-inline">
      <input id="afternoon" type="checkbox" value="Afternoon" name="call_me[]">
      <label for="afternoon" class="checkbox-inline">Call me in the Afternoon</label>
    </div>
  </div>

Then used below code:

  $fieldset->addField(
        'call_me',
        'checkboxes',
        array(
            'name' => 'call_me[]',
            'label' => __('Call Me In'),
            'title' => __('call Me In'),
            'values' => [
                ['value' => 'Morning','label' => 'Morning'],
                ['value' => 'Afternoon','label' => 'Aftenoon']
            ],
        )
    );

Now working fine.