0

I have datatables which is render image inside table but what I've done is just if there is image then render but if no image not yet have statement

here's my datatables

<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>

How do I add statement if no image in datatable like this in 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

1 Answer 1

7

You can use columns.defaultContent option to set default static content for a column.

<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>
Sign up to request clarification or add additional context in comments.

5 Comments

if you are getting image for each and every data, then it will not show? Are you getting any warnings?
I did not get any warning just like no defaultContent: there
what is the content of data in this line --> '<img src="{{ asset("/images/pengumuman/") }}/'+data+'"
it's images name from database
thank you very much @Prashant Pokhriyal this work like a charm!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.