我使用jquery-bootgrid来显示记录列表。
这些记录也有图像,但是图像不能在行中显示。
有人知道如何一排排地显示图像吗?
发布于 2015-07-28 15:36:33
你必须使用格式化器。下面是一个例子。在这里,数据是通过ajax从数据库加载的:
<table id="gridStudents" class="table table-condensed table-hover table-striped">
<thead>
<tr>
<th data-column-id="StudentId" data-type="numeric" data-identifier="true">Student Id</th>
<th data-column-id="FirstName">First Name</th>
<th data-column-id="LastName">Last Name</th>
<th data-column-id="Photo" data-formatter="pix">Photo</th>
</tr>
</thead>
</table>然后,在您的javascript中执行以下操作(假设您有一个名为Student的控制器,以及两个名为getStudents和getPhoto的操作(Int StudentId),分别获取所有学生的照片,并根据他或她的StudentId获取特定学生的照片):
$(function () {
var jqxhr = $.ajax({
url: 'Student/getStudents',
type: 'POST'
});
jqxhr.done(function (data, textStatus, jqXHR) {
$("#gridStudents").bootgrid({
caseSensitive: false,
selection: true,
multiSelect: true,
formatters: {
"pix": function (column, row) {
return "<img src=\"Student/getPhoto/" + row.StudentId + "\" />";
}
}
}).bootgrid("append", data)
});
});下面是服务器端的样子。在这里,照片作为二进制数据存储在数据库中名为Photo的字段中,另一个名为ContentType的字段存储内容类型(image/jpeg、image/png等):
[Authorize]
public class StudentController : Controller
{
.... //your other stuff....
[HttpPost]
public JsonResult getStudents()
{
var data = db.Students.ToList();
return Json(data, JsonRequestBehavior.AllowGet);
}
public ActionResult getPhoto(int StudentId)
{
var student = db.Students.Find(StudentId);
return File(student.Photo, student.PhotoContentType);
}
}发布于 2015-03-16 08:01:51
您可以使用格式化程序将其转换为图像标记:
$("#grid-data").bootgrid({
formatters: {
"imageCol": function(column, row) {
return "<img src='" + row.id + "' />";
}
}
});其中"imageCol“是保存图像绝对路径的列。
https://stackoverflow.com/questions/29071722
复制相似问题