如何从数据表中的服务器端jquery数据表获取图像src值?

时间:2015-07-11 05:44:38

标签: c# jquery datatables

我正在使用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" }
]
});

1 个答案:

答案 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" />'
  }
}
...