我有一个 jqGrid,其中有一些列,其中 1 列是从数据库填充的下拉列表(选择)。
我想要的是:当我不在带有下拉列表的编辑模式列时,只需显示必须从查询中获取的文本,而当我处于编辑模式时,它应该显示下拉列表。
就像这里一样:http://www.trirand.com/blog/jqgrid/jqgrid.html http://www.trirand.com/blog/jqgrid/jqgrid.html进入行编辑/输入tipyes
这是我的网格的代码:
<script type="text/javascript">
var lastsel;
$(document).ready(function () {
$.getJSON('@Url.Action("ConstructSelect")', function (data) {
setupGrid(data);
});
});
function setupGrid(data) {
jQuery(document).ready(function () {
jQuery("#list").jqGrid({
url: '@Url.Action("GetStoreList")',
datatype: 'json',
mtype: 'GET',
colNames: ['Butiks kategori', 'Butik Navn', 'By', 'Sælger'],
colModel: [
{ name: 'Id', index: 'Id', width: 50 },
{ name: 'Butiks Kategori', index: 'StoreId', width: 200, edittype: 'text', align: 'center', editable: false },
{ name: 'Buttiks Navn', index: 'StoreName', width: 200, edittype: 'text', align: 'center', editable: false },
{ name: 'Country', index: 'Country', width: 80, sortable: true, editable: true, edittype: "select", editoptions: { value: data }}],
onSelectRow: function (id) {
if (id && id !== lastsel) {
jQuery('#list').jqGrid('restoreRow', lastsel);
jQuery('#list').jqGrid('editRow', id, true);
lastsel = id;
}
},
editurl: '@Url.Action("GridSave")',
rowNum: 50000,
rowList: [5, 10, 20, 50],
pager: '#page',
sortname: 'Id',
sortorder: "desc",
viewrecords: true,
height: "500px",
imgpath: '/scripts/themes/base/images'
});
jQuery("#list").jqGrid('navGrid', "#page", { edit: false, add: false, del: false });
});
}
</script>
附:我一回家就链接代码
UPDATED:谢谢你的回答,我是jq新手,所以我犯了很多错误。但现在我回到了以前的位置,下拉列表没有填充数据。我按照你说的清理了代码,所以现在看起来像这样:
顺便提一句。 ConstructSelect 返回字符串列表
jQuery(document).ready(function () {
jQuery("#list").jqGrid({
url: '@Url.Action("GetStoreList")',
ajaxSelectOptions: { type: "POST", dataType: "json" },
datatype: 'json',
mtype: 'GET',
colNames: ['Butiks kategori', 'Butik Navn', 'By', 'Sælger'],
colModel: [
{ name: 'Kategori', index: 'Kategori', width: 50, key: false},
{ name: 'Navn', index: 'Navn', align: 'center', editable: false },
{ name: 'By', index: 'By', align: 'center', editable: false },
{ name: 'Sælger', index: 'Sælger', editable: true, edittype: "select",
editoptions: { dataUrl: '@Url.Action("ConstructSelect")',
buildSelect: function (data) {
var response = jQuery.parseJSON(data.responseText);
var s = '<select>';
if (response && response.length) {
for (var i = 0, l = response.length; i < l; i++) {
var ri = response[i];
s += '<option value="' + ri + '">' + ri + '</option>';
}
}
return s + "</select>";
}
}
}],
onSelectRow: function (id) {
if (id && id !== lastsel) {
jQuery('#list').jqGrid('restoreRow', lastsel);
jQuery('#list').jqGrid('editRow', id, true);
lastsel = id;
}
},
editurl: '@Url.Action("GridSave")',
rowNum: 50000,
rowList: [5, 10, 20, 50],
pager: '#page',
sortname: 'Id',
sortorder: "desc",
viewrecords: true,
height: "900px"
});
jQuery("#list").jqGrid('navGrid', "#page", {edit:false, add:false, del:false});
});
好的,需要稍作修改才能使其正常工作:
var response = typeof(data) === "string" ? jQuery.parseJSON(data.responseText):data;
显然我必须告诉 buildselect 你想要修改的数据是 String
但我仍然有一个问题,它从一开始就没有显示哪些卖家已被选择!
好吧,重启后它神秘地工作了......现在已经解决了