我有一个问题,我需要在一行的每个单元格中显示带有一些细节的图像。为了实现这一点,我对单元格使用了自定义格式化程序。
单元格值将提供图像源。但是如何以自定义格式化程序方法获取信息名称和电话。
网格的客户端代码。
var functionsMapping = {
"abc": function (cellValue, opts, rowObject) {
var tbl = "<table><tr><td><a href='/home1/About'><img src='" + cellValue + "' alt='a' /></a></td></tr>";
tbl += "<table><tr><td>Name :aaa</td></tr>";
tbl += "<table><tr><td>Phone :8888</td></tr></table>";
return tbl;
}
};
$(document).ready(function () {
$.ajax({
type: "POST",
async: true,
url: "/home1/GetGridModel",
success: function (data) {
colMM = data.colmodel;
ColNN = data.colN;
for (i = 0; i < colMM.length; i++) {
cm = colMM[i];
if (cm.hasOwnProperty("formatter") && functionsMapping.hasOwnProperty(cm.formatter)) {
cm.formatter = functionsMapping[cm.formatter];
}
}
jQuery("#mygrid").jqGrid({
url: '/home1/GetImageGridData',
datatype: "json",
mtype: "POST",
colNames: ColNN,
colModel: colMM,
rowNum: 4,
width: 700,
height: '300',
pager: $("#pager")
});
}
});
});
控制器代码:
[HttpPost]
public JsonResult GetImageGridData(FormCollection frmcl)
{
int icol = 3;
// it gives array of data
object[] entity = this.FetchData();
string[] colnames = new string[icol];
for (int icount = 1, ilength = icol; icount <= ilength; icount++)
{
colnames[icount - 1] = "Col" + icount.ToString();
}
var eee = this.ToExpandoObject();
object[] data1 = null;
data1 = (from s in eee
select new
{
id = 1,
cell = GetColumns(s,colnames)
}).ToArray();
var jsonData = new
{
total = data1.Count() / 4,
page = 1,
records = data1.Count(),
rows = data1,
userData = "aaa"
};
return Json(jsonData);
}
private object[] GetColumns(object s, string[] colnames)
{
object[] row = new object[colnames.Length];
IDictionary<string, object> propertyValues = (IDictionary<string, object>)s;
int i = 0;
foreach (string col in colnames)
{
row[i++] = propertyValues[col].ToString();
}
return row;
}
public List<dynamic> ToExpandoObject()
{
var result = new List<dynamic>();
int icol = 3;
object[] entity = this.FetchData();
string imagefieldname = "ImageSrc1";
string[] colnames = new string[icol];
for (int irow = 0; irow < entity.Count(); irow++)
{
dynamic e = new ExpandoObject();
var d = e as IDictionary<string, object>;
for (int collength = 0; collength < icol; collength++)
{
d.Add("Col" + (collength+1).ToString() , entity[irow].GetType().GetProperty(imagefieldname).GetValue(entity[irow], null));
}
result.Add(e);
}
return result;
}
}