【发布时间】:2021-06-03 22:55:58
【问题描述】:
我有以下数据表:
在我的控制器中,我有这个获取数据的功能:
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-switch custom-switch-secondary-inverse mb-2 custom-switch-small">
<input class="custom-switch-input" name="switchS4" id="switchS4" type="checkbox">
<label class="custom-switch-btn" for="switchS4"></label>
</div>';
$button .= '</td></tr></table>';
return $button;
})
->rawColumns(['action'])
->make(true);
}
return view('Casting.castingss');
}
以下代码是我的脚本:
<script type="text/javascript">
$(document).ready(function(){
$('#datatableRows').DataTable({
processing: true,
serverSide: true,
ajax:{
url: "{{ route('castingss.getdata') }}",
},
columns:[
{
data: 'casting_photo',
name: 'casting_photo',
render: function(data, type, full, meta){
return "<img src={{ URL::to('/') }}/castingimages/" + data + " class='list-thumbnail responsive border-0 card-img-left' />";
},
orderable: false
},
{data:'casting_name',
name: 'casting_name',
render:function(data,type,full,meta){
return "<a href='profile'>" + data + "</a>";
}
},
{
data: 'casting_cin',
name: 'casting_cin'
},
{
data: 'casting_phone',
name: 'casting_phone'
},
{
data: 'action',
name: 'action',
orderable: false
}
]
});
});
</script>
问题在于,在我的数据库表中,我的状态列等于 1,当我在 m 数据表中显示数据时,自定义开关始终处于停用状态。
编辑
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-switch custom-switch-secondary-inverse mb-2 custom-switch-small">';
$button .= '<input class="custom-switch-input" name="switchS4" id="switchS4" data-id = "'.$data->id.'" type="checkbox"';
if ($data->status == 1) {
$button .= 'checked';
}
$button .= '><label class="custom-switch-btn" for="switchS4"></label></div>';
$button .= '</td></tr></table>';
return $button;
})
->rawColumns(['action'])
->make(true);
}
return view('Casting.castingss');
}
我添加这个脚本只是为了测试它是否有效:
<script type="text/javascript">
$(document).ready(function(){
$('switchS4').change(function () {
alert('I am here');
/**/
});
});
</script>
但我可以只切换数据表的第一行并显示警报不显示
【问题讨论】: