切换开关自定义在使用 laravel 的数据表中不起作用
toggle switch custom doesn't work in datatable using laravel
我使用以下代码向我的数据表添加了一个自定义切换开关:
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');
}
但我得到的是复选框而不是自定义切换开关。
编辑
现在我明白了:
但我可以只切换第一行,其他行保持不变
你好,你应该像这样在开关按钮前定义 div
css 将是:
<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>
代码为:
<label class="switch">
<input type="checkbox">
<span class="slider"></span>
</label>
例如,如果您删除我放在这里的所有 css,只添加第一个
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
你只会得到复选框,你可以用 css 随意编辑它:D 我希望我能帮助你祝你好运
我使用以下代码向我的数据表添加了一个自定义切换开关:
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');
}
但我得到的是复选框而不是自定义切换开关。
编辑
现在我明白了:
但我可以只切换第一行,其他行保持不变
你好,你应该像这样在开关按钮前定义 div
css 将是:
<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>
代码为:
<label class="switch">
<input type="checkbox">
<span class="slider"></span>
</label>
例如,如果您删除我放在这里的所有 css,只添加第一个
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
你只会得到复选框,你可以用 css 随意编辑它:D 我希望我能帮助你祝你好运