我正在使用jquery datatable并使用ajax调用填充该数据表并且其工作正常但现在我需要在列中显示图像但我不知道如何绑定这些图像。图像链接来自category_image中的后端
后端:
var displayedCategories = filteredCategories;
var result = from c in displayedCategories select new[]
{c.id, c.category_name, c.category_image,c.id};
return Json(new{
sEcho = param.sEcho,
iTotalRecords = lstAllCategories.Count,
iTotalDisplayRecords = 10,
aaData = result},JsonRequestBehavior.AllowGet);
前端:
$('#tblInterests').dataTable({
"bServerSide": true,
"sAjaxDataProp": "aaData",
"bProcessing": true,
"bLengthChange": false,
"sPaginationType": "full_numbers",
"bSort": true,
"aoColumns":[
{"sName": "id"},
{"sName": "category_name" },
{"sName": "category_image",
"bSearchable": false,
"bSortable": false,
"mRender": function (data) {
return '<img src="" width="50" height="50" />'
}
},
{"mData": "Id",
"bSearchable": false,
"bSortable": false,
"sWidth": "40px",
"mRender": function (data) {
return '<button class="btn btn-primary" type="button" >Edit</button>'
}
},
],
"ajax": "/Admin/InterestsJson",
"columns": [
{ "data": "id" },
{ "data": "category_name" },
{ "data": "category_image" },
{ "data": "id" }
]
});
答案 0 :(得分:2)
mRender
中的,data
(== category_image
)应设为src
图片:
...
{ "sName": "category_image",
"bSearchable": false,
"bSortable": false,
"mRender": function(data, type, full) {
return '<img src="'+data+'" width="50" height="50" />'
}
}
...