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

Asl*_*lam 0 jquery datatables datatables-1.10 laravel-5

我有数据表,它是表格内的渲染图像,但我所做的只是如果有图像然后渲染但如果没有图像还没有声明

这是我的数据表

<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>
Run Code Online (Sandbox Code Playgroud)

如果在 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
Run Code Online (Sandbox Code Playgroud)

Pra*_*yal 7

您可以使用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>
Run Code Online (Sandbox Code Playgroud)