Datatables Using Codeigniter 4 - displaying image in a column
Datatables Using Codeigniter 4 - displaying image in a column

I am using Codeigniter 4.6.3
I have a datatable that is working fine - except that I have an image column that the file name for each row's data. I want to display this image instead of the file name.
I cannot find an example of how to display the image instead of the the file name.
I am using the following to display my table:
Controller:
$data_table->setTable(builder: $Tvguides->noticeTable())
->setDefaultOrder("IssDate", "ASC")
->setSearch(["Year", "CoverDescription","RegionDescription"])
->setOrder(["IssDate", "IssueSubSort"])
->setOutput(["RecordNO" , "IssueNo", "IssueSubSort", "IssDate","CoverDescription","Purch_Date","CoverPrice","RegionDescription","Cost","Year",
"CoverPicture"])
;
return $data_table->getDatatable();
Then in my View:
$(document).ready(function(){
$('#sample_table').DataTable({
"order":[],
"serverSide":true,
"ajax":{
url:"<?php echo base_url('/TV2/fetch_all'); ?>",
type:'POST'
}
});
});
As you can see - I have the CoverPicture column - but do not know how to tell the datatables function to display the actual image (with appropriate sizing controls, etc.
Thank you for any help you may provide.
Roger
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Replies
Hi Roger,
Use a data renderer - for example you might have something like:
See more about using custom function in the manual here.
Allan
Here is my new view code - however, the $data comes back as an undefined variable since I have to use PHP encoding to get the secure image path. What syntax should I have surrounding the ${data} to have it recognized by the javascript? Everything up to the actual file name in the img src is correct. Any thoughts?
Hmmm - aside from the fact that you need to use back ticks to have a template variable filled in, you are mixing PHP and Javascript there. The PHP will run first, then the Javascript on the client-side, but it looks like you are expecting the PHP to run after the Javascript variable has been filled in for each file.
I don't know what your
base_url
function is doing, but could you do:That way the rendered Javascript will look like:
Allan
Your solution worked perfectly! Thank you Allan.
Here is the script in case anyone else is needing it!