Search code examples
ajaxlaraveltoggleswitch

toggle switch custom doesn't work in datatable using laravel


I added a toggle switch custom to my datatable , using the following code :

          function getdata(Request $request)
{
    if(request()->ajax())
    {
        return datatables()->of(Casting::latest()->get())
            ->addColumn('action', function($data){
                $button = '<table><tr><td>';
                $button .= '<button type="button" name="edit" id="'.$data->id.'" class="edit btn btn-primary btn-sm">Modifier</button>';
                $button .= '</td><td>';
                 $button .= ' <div class="custom-control custom-switch">';
                $button .= ' <input type="checkbox" class="custom-control-input" id="switch1" name="example"';
                
                if ($data->status == 1) {

                    $button .= "checked";
                }

                $button .= '><label class="custom-control-label" for="switch1">Toggle me</label>
    </div>';
                $button .= '</td></tr></table>';
                return $button;
        })
        ->rawColumns(['action'])
        ->make(true);
    }
    return view('Casting.castingss');
}

But I get a checkbox instead of a toggle switch custom.

enter image description here

EDIT

Now I get That :

enter image description here

But I can switch just the first row thr other rows stay fixed


Solution

  • hi u should define the div before the switch button like this

    the css will be :

      <style>
    .switch {
      position: relative;
      display: inline-block;
      width: 60px;
      height: 34px;
    }
    
    .switch input { 
      opacity: 0;
      width: 0;
      height: 0;
    }
    
    .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #ccc;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    .slider:before {
      position: absolute;
      content: "";
      height: 26px;
      width: 26px;
      left: 4px;
      bottom: 4px;
      background-color: white;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    input:checked + .slider {
      background-color: #2196F3;
    }
    
    input:focus + .slider {
      box-shadow: 0 0 1px #2196F3;
    }
    
    input:checked + .slider:before {
      -webkit-transform: translateX(26px);
      -ms-transform: translateX(26px);
      transform: translateX(26px);
    }
    
    
    </style>
    

    and the code will be :

    <label class="switch">
      <input type="checkbox">
      <span class="slider"></span>
    </label>
    

    for exemple if u delete all the css i put in here and add let only the first

    .switch {
      position: relative;
      display: inline-block;
      width: 60px;
      height: 34px;
    }
    

    u will get only checkbox the u can edit it as u like with css :D i hope i helped u Good luck