我想动态添加colModel。但是,jsonmap无法正常工作。
这是我的代码。
$(document).ready(function () {
$('#search_btn').click(function () {
$.ajax({
url: "/Search/SearchButton",
type: "POST",
cache: false,
contentType: 'application/json; charset=utf-8',
success: function (data) {
var search_word = $('#search_bar').val();
var baseLang = $('#baseLang_choice_single').val();
var targetLang = $('#targetLang_choice_multi').val();
var products = $('#product_choice_multi').val();
var columnNames = [];
var columnModels = [];
var languageLength;
var getVaueByName = function (cells, colName) {
var i, count = cells.length, item;
for (i = 0; i < count; i += 1) {
item = cells[i];
if (item.colname === colName) {
return item.value;
}
}
return '';
};
columnNames.push(baseLang);
if (targetLang == "") {
columnNames.push("yyy");
} else {
for (var i = 0; i < targetLang.length; ++i) {
columnNames.push(targetLang[i]);
}
}
columnNames.push('ID');
columnNames.push('Product');
columnModels.push({ name: 'languageGroup.0.Key', jsonmap: 'languageGroup.0.Value' }); //is that right?
if (targetLang == "") {
columnModels.push({ name: 'languageGroup.1.Key', jsonmap: function (obj) { return getVaueByName(obj.languageGroup, "yyy"); }, sortable: false }); //is that right?
}
else {
for (var i = 1; i <= targetLang.length; ++i) {
columnModels.push({ name: 'languageGroup.'+i+'Key', jsonmap: 'languageGroup.'+i+'Value', sortable: false }); //is that right?
}
}
columnModels.push({ name: 'ID', index: 'ID' });
columnModels.push({ name: 'Product', index: 'Product', width: 80, align: "center" });
$(function () {
jQuery("#search_datagrid").jqGrid("GridUnload").jqGrid({
url: '/Search/SearchDataGrid/',
datatype: "json",
contentType: "application/json; charset-utf-8",
mtype: 'POST',
postData: {
search_word: search_word,
baseLang: baseLang,
targetLang: targetLang,
products: products
},
jsonReader:{
repeatitems: false,
root:"rows"
},
jsonmap: function (item) {
//return item.languageGroup.Key; ????? is that right?
},
rowNum: 20,
rowList: [10, 20, 30, 50],
colNames: columnNames,
colModel: columnModels,
..............
............................}
控制器代码。
search.DosearchWord(....) return List< T > (
List< KeyValuePair< string,string > > languageGroup,
String ID,
String Product)
var json = search.DoSearchWord(search_word, baseLang, targetLang, products).ToList();
var jsonString = JsonConvert.SerializeObject(json);
var jsonData = new
{
total = 1,
page = 1,
records = search.DoSearchWord(search_word,baseLang,targetLang,products).ToList().Count,
rows = (
new
{
cell = json
})
};
var jsonResult = Json(jsonData, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
这里的json数据
[{"languageGroup":[{"Key":"yy","Value":"xx"},{"Key":"yyy","Value":"xxx"},{"Key":"yyyy","Value":"xxxx"},{"Key":"yyyyy","Value":"xxxxx"}],"stringID":"blahblah,"product":"blahblah"},{"languageGroup":[{"Key":"yy","Value":"xx"},{"Key":"yyy","Value":"xxx"},{"Key":"yyyy","Value":"xxxx"},{"Key":"yyyyy","Value":"xxxxx"}],"stringID":"blahblah,"product":"blahblah"}.....]
languageGroup长度和名称是动态的