C# 我想动态添加colModel。但是,jsonmap不起作用
我想动态添加colModel。但是,jsonmap不起作用 这是我的密码C# 我想动态添加colModel。但是,jsonmap不起作用,c#,jquery,json,asp.net-mvc,jqgrid,C#,Jquery,Json,Asp.net Mvc,Jqgrid,我想动态添加colModel。但是,jsonmap不起作用 这是我的密码 $(document).ready(function () { $('#search_btn').click(function () { $.ajax({ url: "/Search/SearchButton", type: "POST", cache: false,
$(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,
..............
............................}
languageGroup长度和名称是动态的您包含了错误的JSON数据。1) 有未关闭的引号请参见
“stringID:”blahblah,“product:”blahblah“
而不是“stringID:”blahblah“,”product:”blahblah“
2)JSON数据与服务器端代码不对应。服务器返回{“total”:1,“page”:1,“records”:xxx,“cell”:[…])
。网格中总共可以显示多少行(100、1000、10000、100000等)?使用哪个版本的jqGrid以及jqGrid的哪个分支(、商业版或旧版jqGrid中您是否可以包括一个列标题示例,您希望使用它?JSON数据示例允许在每行数据中使用不同的Key
。是languageGroup[i]。Key
是数据的一部分,还是在所有项中都有相同的值?使用columnNames.push(targetLang[i])
,其中targetLang
不是从服务器返回的数据的一部分。它看起来很奇怪。它与languageGroup[i]有一些关系。Key
?您使用jsonmap:'languageGroup.+i+'Value'
而不是至少jsonmap:'languageGroup.+i+'。Value'
(在值之前加上附加的
)。i
应以0开头
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;
[{"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"}.....]