我在Laravel项目中一直使用Datatable,但有时当我刷新页面时,它会起作用,有时它不会
这是我在视图上的脚本
@section('script')
<script type="text/javascript">
$(function () {
var oTable = $('#tabel-stok').DataTable({
processing: true,
serverSide: true,
order: [[ 0 ,"desc"]],
ajax: {
url: '{{ url("data-stok") }}'
},
columns: [
{data: 'updated_at', name: 'updated_at'},
{data: 'nama_produk', name: 'nama_produk'},
{data: 'harga_satuan', name: 'harga_satuan'},
{data: 'jumlah_stok', name: 'jumlah_stok'},
{data: 'tambah', name: 'tambah', orderable: false, searchable: false},
{data: 'edit', name: 'edit', orderable: false, searchable: false}
],
});
});
</script>
@endsection
这是我的控制器
public function index()
{
return view('transaksi-masuk.transaksi-masuk');
}
public function dataStok()
{
$stok = Produk::all();
return Datatables::of($stok)
->addColumn('tambah', function ($stok) {
return '<a href="/transaksi-masuk/tambah/' . $stok->id_produk . '"><span class="label label-primary">TAMBAH</span></a>';
})
->addColumn('edit', function ($stok) {
return '<a href="/transaksi-masuk/edit/' . $stok->id_produk . '"><span class="label label-warning">EDIT</span></a>';
})
->make(true);
}
这是错误消息
DataTables warning: table id=tabel-stok - Ajax error. For more information about this error, please see http://datatables.net/tn/7
这有什么解决方案吗?谢谢
答案 0 :(得分:0)
现在有效。我试图从控制器中删除addColumn并像这样编辑我的脚本
columns: [
{data: 'updated_at', name: 'updated_at'},
{data: 'nama_produk', name: 'nama_produk'},
{data: 'harga_satuan', name: 'harga_satuan'},
{data: 'jumlah_stok', name: 'jumlah_stok'},
{
name: '',
data: null,
sortable: false,
searchable: false,
render: function (data) {
var actions = '';
actions += '<a href="/transaksi-masuk/tambah/:id"><span class="label label-primary">TAMBAH</span></a>';
actions += '<a href="/transaksi-masuk/edit/:id"><span class="label label-warning">EDIT</span></a>';
return actions.replace(/:id/g, data.id_produk);
}
}
感谢您帮助我:)