Asp.net JQGrid:用于从外部表中选择项的下拉列表-DataValueField vs DataTextField
假设Items和ItemType具有数字主键ItemID和ItemTypeID。为每个项目分配一个ItemType 我有一个JQGrid来编辑项目。当不处于编辑模式时,我希望看到ItemType的名称,而不是ItemTypeID: TYPE | TITLE -----------+-------------------- Category A | Item 1 Category A | Item 2 Category B | Item 3 Category B | Item 4 类型|标题 -----------+-------------------- A类|项目1 A类|项目2 B类|项目3 B类|项目4 在编辑模式下,我希望看到一个下拉列表,显示ItemType文本,但将ItemTypeID返回给服务器 以下是我到目前为止所做的(使用JQGrid的ASP.NET包装):Asp.net JQGrid:用于从外部表中选择项的下拉列表-DataValueField vs DataTextField,asp.net,jquery,drop-down-menu,jqgrid,Asp.net,Jquery,Drop Down Menu,Jqgrid,假设Items和ItemType具有数字主键ItemID和ItemTypeID。为每个项目分配一个ItemType 我有一个JQGrid来编辑项目。当不处于编辑模式时,我希望看到ItemType的名称,而不是ItemTypeID: TYPE | TITLE -----------+-------------------- Category A | Item 1 Category A | Item 2 Category B | Item 3 Ca
...
问题在于,当不处于编辑模式时,它会显示数字ItemTypeID,而不是文本标签:
TYPE | TITLE
-----------+--------------------
100123 | Item 1
100123 | Item 2
100124 | Item 3
100124 | Item 4
类型|标题
-----------+--------------------
100123 |项目1
100123 |项目2
100124 |项目3
100124 |项目4
有没有办法让JQGrid尊重DataValueField和DataTextField之间的区别?(使用jQuery API或ASP.NET插件。)赫伯,问题是您使用的是datafield=“ItemTypeID”。你必须把它改成类似CategoryTitle的东西
这里还有一个例子在这里找到了一个很好的解决方案: 其思想是在网格上处理
CellBinding
事件,并查找与单元格包含的ID对应的文本
protected void JQGrid1_CellBinding(object sender, Trirand.Web.UI.WebControls.JQGridCellBindEventArgs e)
{
if (e.ColumnIndex == 1) // index of your dropdown column
{
e.CellHtml = LookupText(e.CellHtml);
}
}
LookupText
的实现取决于您的情况;您可以查看该列的EditValues
(例如1:1;2:2;3:3
),也可以在数据中查找该列
我已经将所有这些逻辑包装到一个自定义列类(在VB.NET中)中,该类还根据您给出的SQL命令填充下拉列表
Public Class JqGridDropDownColumn
Inherits Trirand.Web.UI.WebControls.JQGridColumn
Private _SelectCommand As String
'' /* The SQL command used to populate the dropdown. */
'' /* We assume that the first column returned contains the value (e.g. BudgetID) and the second column contains the text (e.g. Title). */
Public Property SelectCommand() As String
Get
Return _SelectCommand
End Get
Set(ByVal value As String)
_SelectCommand = value
End Set
End Property
Private _DropDownNullText As String
Public Property DropDownNullText() As String
Get
Return _DropDownNullText
End Get
Set(ByVal value As String)
_DropDownNullText = value
End Set
End Property
Private WithEvents Grid As JQGrid
Private DropDownValues As DataTable
Sub Init(g)
Grid = g
DropDownValues = ExecuteDataset(cs, CommandType.Text, Me.SelectCommand).Tables(0)
DropDownValues.PrimaryKey = New DataColumn() {DropDownValues.Columns(0)}
Me.EditValues = BuildEditValues(DropDownValues)
End Sub
'' /* Builds a string of the form "1:One;2:Two;3:Three" for use by the EditValues property. */
'' /* This assumes that Table consists of two columns corresponding to the Value (e.g. BudgetID) and Text (e.g. Title), in that order. */
Protected Function BuildEditValues(ByVal Table As DataTable) As String
Dim Result As String = ""
If Not String.IsNullOrEmpty(Me.DropDownNullText) Then
Result = String.Format(":{0}", Me.DropDownNullText)
End If
For Each Row As DataRow In Table.Rows
If Len(Result) > 0 Then Result &= ";"
Result &= Row(0) & ":" & Row(1)
Next
Return Result
End Function
Private Sub Grid_CellBinding(ByVal sender As Object, ByVal e As Trirand.Web.UI.WebControls.JQGridCellBindEventArgs) Handles Grid.CellBinding
'' /* Display the text (e.g. Title) rather than the value (e.g. BudgetID) */
If Grid.Columns(e.ColumnIndex) Is Me Then
e.CellHtml = LookupText(e.CellHtml)
End If
End Sub
Private Function LookupText(ByVal Value As String) As String
Dim MatchingRow As DataRow = DropDownValues.Rows.Find(Value)
If MatchingRow IsNot Nothing Then
Return MatchingRow(1) '' /* Column 1 is assumed to contain the text */
Else
Return ""
End If
End Function
End Class
您只需要从网格的
Init
事件调用该列上的DropdownColumn.Init(MyGrid)
。希望这对其他人有所帮助。对于那些使用javascrpt而不是asp.net包装器的人来说,javascript的方式是使用格式化程序和非格式化程序:
柱模型:
编辑选项:{value:'1:Type1;2:Type2;3:Type3;4:Type4;5:Type5'},
格式化程序:showTextFmatter,未格式化:未格式化showtext
我的格式化程序,您应该编写自己的格式,如下所示:
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];
}
函数showTextFmatter(单元格值、选项、行对象)
{
var-vts={};
if(options.colModel.editoptions.values==未定义)
{
vtStr=options.colModel.editoptions.value.split(“;”);
对于(var i=0;i请重新阅读该问题-这并没有真正的帮助。问题是我想存储ID(例如ItemTypeID),但显示名称(例如Title)
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];
}