0

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

2
  • which js are you using for switch custom .also show if it has jquery for toggle Commented Jun 6, 2021 at 14:35
  • @JohnLobo , I'm using that w3schools.com/bootstrap4/… Commented Jun 6, 2021 at 14:47

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your help, it works for me but I didn't get the CSS that I wanted, but it works, Now I can switch all the rows of Datatable

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.