如何将if语句添加到数据表中?

How to add if statement into datatables?

我有数据tables,它是 table 中的渲染图像,但我所做的只是如果有图像则渲染,但如果没有图像还没有声明

这是我的数据tables

<script type="text/javascript">
$(function() {
    $('#pengumuman-table').DataTable({
        processing: true,
        serverSide: true,
        responsive: true,
        ajax: '{!! route('pengumuman.data') !!}',
        columns: [
            { data: 'rownum', name: 'rownum' },
            { data: 'gambar', render: function(data)
                { return '<img src="{{ asset("/images/pengumuman/") }}/'+data+'" atl img style="width:200px; height:150px"/>' }
            },
            { data: 'nama_pengumuman', name: 'nama_pengumuman' },
            { data: 'created_at', name: 'created_at' },
            { data: 'action', name: 'action', orderable: false, searchable: false }
        ]
    });
});
</script>

如果数据table中没有图像,我如何在php

中添加语句
@if(isset($pegawai->foto) && !empty($pegawai->foto))
  <div align="center"> 
  <img src="{{ asset("/images/karyawan/$pegawai->foto") }}" alt="" img style="width:250px; height:260px">
  </div>
@else
  <div align="center"> 
  <img src="http://www.blogsaays.com/wp-content/uploads/2014/02/no-user-profile-picture-whatsapp.jpg" alt="" img style="width:250px; height:260px">
  </div>
@endif

您可以使用 columns.defaultContent 选项为列设置默认静态内容。

<script type="text/javascript">
  $(function() {
    $('#pengumuman-table').DataTable({
        processing: true,
        serverSide: true,
        responsive: true,
        ajax: '{!! route('pengumuman.data') !!}',
        columns: [
            { 
              data: 'rownum', 
              name: 'rownum' 
            },
            { 
              data: 'gambar', 
              render: function(data) { 
                if(data) {
                  return '<img src="{{ asset("/images/pengumuman/") }}/'+data+'" atl img style="width:200px; height:150px"/>' 
                }
                else {
                  return '<img src="http://www.blogsaays.com/wp-content/uploads/2014/02/no-user-profile-picture-whatsapp.jpg" alt="" img style="width:250px; height:260px">'
                }

              },
              defaultContent: '<img src="http://www.blogsaays.com/wp-content/uploads/2014/02/no-user-profile-picture-whatsapp.jpg" alt="" img style="width:250px; height:260px">'
            },
            { 
              data: 'nama_pengumuman', 
              name: 'nama_pengumuman' 
            },
            { 
              data: 'created_at', 
              name: 'created_at'
            },
            { 
              data: 'action', 
              name: 'action', 
              orderable: false, 
              searchable: false
            }
        ]
    });
  });
</script>