【问题标题】:JQGrid: Dropdown for selecting items from a foreign table - DataValueField vs DataTextFieldJQGrid:用于从外部表中选择项目的下拉列表 - DataValueField 与 DataTextField
【发布时间】:2009-12-21 12:13:47
【问题描述】:

假设 Items 和 ItemTypes 具有数字主键 ItemID 和 ItemTypeID。每个 Item 都分配有一个 ItemType。

我有一个 JQGrid 来编辑项目。当不处于编辑模式时,我想查看 ItemType 的名称,而不是 ItemTypeID:

类型 |标题 -----------+-------- 甲类 |项目 1 甲类 |第 2 项 B类 |第 3 项 B类 |第 4 项

在编辑模式下,我希望看到一个显示 ItemType 文本的下拉菜单,但它会将 ItemTypeID 返回给服务器。

这是我目前所拥有的(使用 JQGrid 的 ASP.NET 包装器):

<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,而不是文本标签:

类型 |标题 -----------+-------- 100123 |项目 1 100123 |第 2 项 100124 |第 3 项 100124 |第 4 项

有什么方法可以让 JQGrid 尊重 DataValueField 和 DataTextField 之间的区别? (使用 jQuery API 或 ASP.NET 插件。)

【问题讨论】:

    标签: asp.net jquery drop-down-menu jqgrid


    【解决方案1】:

    对于那些使用 javascrpt 而不是 asp.net 包装器的人,javascript 方式是使用格式化程序和取消格式化程序:

    列模型:

    editoptions:{value:'1:Type1;2:Type2;3:Type3;4:Type4;5:Type5'}, 格式化程序:showTextFmatter,取消格式化: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];  
        }  
    

    【讨论】:

      【解决方案2】:

      在这里找到了一个好的解决方案:http://www.trirand.net/forum/default.aspx?g=posts&t=168

      思路是处理网格上的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:One;2:Two;3:Three),或者您可以在您的数据中查找它。

      我已将所有这些逻辑包装到一个自定义列类(在 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
      

      您只需要通过 Grid 的 init 事件在此列上调用 DropdownColumn.Init(MyGrid)。希望这对某人有所帮助。

      【讨论】:

        【解决方案3】:

        Herb,问题在于您使用的是 datafield="ItemTypeID"。您必须将其更改为 CategoryTitle 之类的内容。

        这里也是一个例子http://www.trirand.net/examples/editing_data/edit_types/default.aspx

        【讨论】:

        • 请重新阅读问题 - 这并没有真正的帮助。问题是我想存储 ID(例如 ItemTypeID)但显示名称(例如 Title)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多