假设 Items 和 ItemTypes 具有数字主键 ItemID 和 ItemTypeID。每个项目都分配有一个项目类型。
我有一个 JQGrid 来编辑项目。当不在编辑模式下时,我想查看 ItemType 的名称,而不是 ItemTypeID:
TYPE | TITLE -----------+-------------------- Category A | Item 1 Category A | Item 2 Category B | Item 3 Category B | Item 4
In edit mode, I want to see a dropdown that displays the ItemType text, but that returns the ItemTypeID to the server.
Here's what I have so far (using the ASP.NET wrapper for JQGrid):
<trirand:jqgrid id="Grid1" runat="server" ... >
<columns>
<trirand:jqgridcolumn datafield="ItemID" editable="false" visible="false" width="50" primarykey="true" />
<trirand:jqgridcolumn datafield="ItemTypeID" editable="true" edittype="DropDown" editorcontrolid="ItemTypes" />
<trirand:jqgridcolumn datafield="Title" editable="true" sortable="true" />
...
</columns>
</trirand:jqgrid>
<asp:sqldatasource runat="server" id="ItemTypesDatasource" connectionstring="<%$ ConnectionStrings:Main %>" selectcommand="Select ItemTypeID,Title from ItemTypes order by Title" />
<asp:dropdownlist runat="server" id="ItemTypes" datasourceid="ItemTypesDatasource" datavaluefield="ItemTypeID" datatextfield="Title" />
问题是,当不处于编辑模式时,它显示数字 ItemTypeID,而不是文本标签:
TYPE | TITLE -----------+-------------------- 100123 | Item 1 100123 | Item 2 100124 | Item 3 100124 | Item 4
有什么方法可以让 JQGrid 尊重 DataValueField 和 DataTextField 之间的区别吗? (使用 jQuery API 或 ASP.NET 插件。)
最佳答案
对于那些使用javascrpt而不是asp.net包装器的人来说,javascript方式是使用格式化程序和取消格式化程序:
柱模型:
editoptions:{value:'1:Type1;2:Type2;3:Type3;4:Type4;5:Type5'}, 格式化程序:showTextFmatter,unformat:unformatShowText,
我的格式化程序,你应该编写自己的格式化程序,如下所示:
function showTextFmatter (cellvalue, options, rowObject)
{
var vts={};
if(options.colModel.editoptions.values==undefined)
{
vtStr = options.colModel.editoptions.value.split(';');
for(var i =0;i<vtStr.length;i++)
{
kv = vtStr[i].split(':');
vts[kv[0]]=vtStr[i];
}
options.colModel.editoptions.values = vts;
}
return options.colModel.editoptions.values[cellvalue];
}
function unformatShowText (cellvalue, options)
{
return cellvalue.split(':')[0];
}
关于asp.net - JQGrid:用于从外部表中选择项目的下拉菜单 - DataValueField 与 DataTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1939783/