May I know if it is possible to put pictures or images into the rows of DataTables (http://datatables.net/) and how does one goes in doing it?
-
[An experience I had was how to print, then I found the **stripHtml:** false as below](https://stackoverflow.com/questions/46063474/how-to-display-image-in-datatable/56198232#56198232) – Mauro Candido May 20 '19 at 03:15
-
See HTML DataTable Image as Table Element https://sourcecodezoneseven.blogspot.com/2019/07/html-datatable-image-as-table-element.html – CodeDezk Jul 16 '19 at 05:47
5 Answers
yes, simple way (Jquery Datatable)
<script>
$(document).ready(function () {
$('#example').dataTable({
"processing": true, // control the processing indicator.
"serverSide": true, // recommended to use serverSide when data is more than 10000 rows for performance reasons
"info": true, // control table information display field
"stateSave": true, //restore table state on page reload,
"lengthMenu": [[10, 20, 50, -1], [10, 20, 50, "All"]], // use the first inner array as the page length values and the second inner array as the displayed options
"ajax":{
"url": "@string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"))/Home/AjaxGetJsonData",
"type": "GET"
},
"columns": [
{ "data": "Name", "orderable" : true },
{ "data": "Age", "orderable": false },
{ "data": "DoB", "orderable": true },
{
"render": function (data, type, JsonResultRow, meta) {
return '<img src="Content/Images/'+JsonResultRow.ImageSrcDB+'">';
}
}
],
"order": [[0, "asc"]]
});
});
</script>

- 9,489
- 8
- 74
- 87
-
Hello can you share you controller code?. `"render": function (data, type, full, meta) { return '
'; }` can't seems get quite this code. – KiRa Mar 07 '17 at 05:43
-
@KiRa Not the same code, but you can find what you need, http://stackoverflow.com/questions/12596400/how-can-i-implement-jquery-datatables-plugin-using-c-asp-net-sql-server-side – Arun Prasad E S Mar 07 '17 at 05:46
-
I'm trying to understand that line and to apply it on my code.. I am looking on how to display an image into `Data Table`. – KiRa Mar 07 '17 at 05:52
-
1my image is located inside Content folder so , Content/, to test, if you paste the url in the browser, you can see the image. then the path is correct. You can also use relative path like I did. – Arun Prasad E S Mar 07 '17 at 06:02
-
-
1The example uses database for image address, the path is in 'ImageAddress' , I saved only image name with extension in the database, the folder location was hardcoded, the 'ImageAddress' has the partial path. – Arun Prasad E S Mar 07 '17 at 06:15
-
-
1Thanks.. Got it now. I search on how to add first an image into a folder then retrieve it. – KiRa Mar 07 '17 at 08:57
-
1Yes it's very easy { "render": function (data, type, JsonResultRow, meta) { return '
'+JsonResultRow.CountryName + ' [' +JsonResultRow.CountryKey + '] '; } }, – kamal.shalabe Jul 19 '17 at 22:31
-
1In my case, $('#img').attr("src", baseUrl+imgUploadPath+data.image); code outputs an image in
. How can do that in DataTable as you did? plz.... – Pasindu Jayanath Jun 03 '18 at 02:32
-
1
-
[edit: note that the following code and explanation uses a previous DataTables API (1.9 and below?); it translates easily into the current API (in most cases, just ditch the Hungarian notation ("fnRowCallback" just becomes "rowCallback" for example) but I have not done so yet. The backwards compatibility is still in place I believe, but you should look for the newer conventions where possible]
Original reply follows:
What Daniel says is true, but doesn't necessarily say how it's done. And there are many ways. Here are the main ones:
1) The data source (server or otherwise) provides a complete image tag as part of the data set. Don't forget to escape any characters that need escaping for valid JSON
2) The data source provides one or more fields with the information required. For example, a field called "image link" just has the Images/PictureName.png
part. Then in fnRowCallback you use this data to create an image tag.
"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
var imgLink = aData['imageLink']; // if your JSON is 3D
// var imgLink = aData[4]; // where 4 is the zero-origin column for 2D
var imgTag = '<img src="' + imgLink + '"/>';
$('td:eq(4)', nRow).html(imgTag); // where 4 is the zero-origin visible column in the HTML
return nRow;
}
3) Similar to above, but instead of adding a whole tag, you just update a class that has the image as a background. You would do this for images that are repeated elements rather than one-off or unique pieces of data.

- 10,749
- 5
- 53
- 72
-
I need to do the similar thing, I need to add image from the jquery-ui and then when I hover over it I need to display a tooltip with the data. Any suggestions on doing it? – user525146 Mar 26 '12 at 15:08
-
Almost the same as above. Most browsers will use title attribute for tooltip, so the "lowest hanging fruit" is to push the data into the title attribute for the cell or for the image. – Greg Pettit Mar 26 '12 at 15:38
-
Can you give me an example as I am still learning jQuery and datatables. I would really appreciate that. – user525146 Mar 26 '12 at 16:30
-
I could; do you have a sample of the JSON that you're sending back? Maybe it's best to start a new SO question? Not sure what the protocol is for questions that are similar but with different details. – Greg Pettit Mar 26 '12 at 16:38
-
I have started a new question here http://stackoverflow.com/questions/9864337/add-image-icon-as-a-row-value-in-datatables – user525146 Mar 26 '12 at 16:43
-
2) is a good option if you only want to download the images for the page showing and not the entire dataset. – RayLoveless Dec 14 '12 at 20:53
-
See HTML DataTable Image as Table Element https://sourcecodezoneseven.blogspot.com/2019/07/html-datatable-image-as-table-element.html – CodeDezk Jul 16 '19 at 05:48
You mean an image inside a column of the table?
Yes, just place an html image tag
like this
<img src="Images/PictureName.png">
instead of putting data (some string) into a column just put the above html tag....

- 36,833
- 10
- 119
- 200
-
hmmm, I think I wastedn 30min because I used href inside the img, instead of src.... ups – Stefan Feb 25 '13 at 15:58
Asp.net core DataTables
The following code retrieve the image from a folder in WWWroot
and the path in the DB field ImagePath
{
"data": "ImagePath",
"render": function (data) {
return '<img src="' + data + '" class="avatar" width="50" height="50"/>';
}
}

- 475
- 3
- 14
In case the Name of the picturefile is put together out of one or more informations in the table, like in my case:
src="/images/' + Nummer + Belegnummer + '.jpg"
you can make it that way:
var table = $('#Table').DataTable({
columnDefs: [
{
targets: 0,
render: getImg
}
]
});
function getImg(data, row, full) {
var Nummer = full[1];
var Belegnummer = full[4];
return '<img src="/images/' + Nummer + Belegnummer + '.jpg"/>';}
The picture is in the first column, so Targets = 0 and gets the Information from the same row. It is necessary to add the parameters data and row. It is not necessary to outsource it into a seperate function, here getImg, but it makes it easier to debug.

- 51
- 8