【问题标题】:Getting value of a column on click of a row in jqGrid单击jqGrid中的一行获取列的值
【发布时间】:2012-05-08 19:38:51
【问题描述】:

我正在使用 Asp.Net/C# ,在我的一个页面中,我使用 jqGridAdmin 显示用户列表,jqGrid 包含以下列

  1. 用户代码
  2. 名字
  3. 中间名
  4. 姓氏
  5. 电子邮件

这是我的标记

<cc1:JQGrid ID="ModifyAccountUserDetailsjqGrid"    AppearanceSettings-Caption="User Details"         runat="server" Width=800   DataSourceID=ModifyAccountDataSource>
    <Columns>
    <cc1:JQGridColumn HeaderText="User Code" ShowToolTip=false   PrimaryKey=true    DataField="UserCode"></cc1:JQGridColumn>
    <cc1:JQGridColumn HeaderText="First Name" ShowToolTip=false    DataField="FirstName"></cc1:JQGridColumn>
    <cc1:JQGridColumn HeaderText="Middle Name" ShowToolTip=false   DataField="MiddleName"></cc1:JQGridColumn>
    <cc1:JQGridColumn HeaderText="Last Name" ShowToolTip=false     DataField="LastName"></cc1:JQGridColumn>
    <cc1:JQGridColumn HeaderText="Email"  ShowToolTip=false        DataField="Email"></cc1:JQGridColumn>
    <cc1:JQGridColumn HeaderText="Contact No" ShowToolTip=false    DataField="ContactNo"></cc1:JQGridColumn>
    <cc1:JQGridColumn HeaderText="Division Name" ShowToolTip=false   DataField="DivisionName"></cc1:JQGridColumn>
    <cc1:JQGridColumn HeaderText="Last Name" ShowToolTip=false     DataField="BranchName"></cc1:JQGridColumn>
    </Columns> 
</cc1:JQGrid>

我需要的是,当管理员单击一行时,我想获取单击行的用户代码的值。我是 jqGrid 的新手,所以我不知道如何去对这个。 谁能指出我正确的方向。欢迎提出任何建议。

谢谢

【问题讨论】:

    标签: c# asp.net jqgrid jqgrid-asp.net


    【解决方案1】:

    最后为了得到我需要的东西,我从 Example 得到了很多帮助,所以解决方案是在 jqGrid 的 Rowselecting 事件上,我使用 jqGrid.SelectedRow 来获取我的单元格的值。

    例子:

    protected void ModifyAccountUserDetailsjqGrid_RowSelecting(object sender, Trirand.Web.UI.WebControls.JQGridRowSelectEventArgs e)
            {
                ModifyAccountUserDetailsjqGrid.SelectedRow;   
            }
    

    P.S Oleg 非常感谢您的慷慨帮助。非常感谢。

    【讨论】:

      【解决方案2】:

      首先,您应该选择符合您要求的最佳回调。通常是onSelectRow,但在某些其他情况下,另一个回调,如onCellSelectbeforeSelectRowondblClickRow 更好。

      在回调中,您将rowidid&lt;tr&gt; 行)作为第一个参数。您可以使用getCellgetRowDatagetLocalRow 来获取某个单元格的包含。例如

      onSelectRow: function (id) {
          // get data from the column 'userCode'
          var userCode = $(this).jqGrid('getCell', 'userCode');
          alert(userCode);
      }
      

      onSelectRow: function (id) {
          var localRowData = $(this).jqGrid('getLocalRow');
          alert(localRowData.userCode);
      }
      

      如果 jqGrid 有本地数据,最后一种方法是最好的(你使用 datatype: 'local' 或远程数据类型,如 datatype: 'json'loadonce: true 结合使用)。

      更新:在 cmets 中发布一些帖子并更新您的问题文本后,我看到您使用 jqSuite for ASP.NET WebForms 或其他一些基于 jqGrid 的商业产品免费的开源 JavaScript 库 jqGrid。我没用过jqSuite,不知道jqSuite应该如何实现JavaScript回调。

      我可以建议您使用新的 jqGrid 4.3.2 功能:jQuery like events。你可以做的就是这样的代码

      var $grid = jQuery("#<%= ModifyAccountUserDetailsjqGrid.ClientID %>");
      $grid.bind("jqGridSelectRow", function (id) {
          var userCode = $(this).jqGrid('getCell', 'userCode');
          alert(userCode);
      });
      

      var $grid = jQuery("#<%= ModifyAccountUserDetailsjqGrid.ClientID %>");
      $grid.bind("jqGridSelectRow", function (id) {
          var localRowData = $(this).jqGrid('getLocalRow');
          alert(localRowData.userCode);
      });
      

      “jqGridSelectRow”等事件的事件处理程序可以在创建网格之前或之后定义(但在创建id 等于&lt;%= ModifyAccountUserDetailsjqGrid.ClientID %&gt;&lt;table&gt; 元素之后)。此外,如果需要,您可以将更多定义为一个事件处理程序。这是非常实用的,您希望在您的项目中实现所有网格的一些通用操作。

      【讨论】:

      • 将尝试这个并通知您,非常感谢您的关注。
      • 我的 .js 文件中有一个问题,我只需要写 onSelectRow: function (id) { // 从 'userCode' 列获取数据 var userCode = $(this).jqGrid(' getCell', 'userCode');警报(用户代码); } ,或者其他的也需要
      • @freebird: onSelectRow 是 jqGrid 的选项。因此,您可以将它包含在使用 datatypeurlcolModel 的相同位置:$("#grid").jqGrid({datatype: 'local', colModel: [{'userCode'}], height: 'auto', gridview: true, onSelectRow: function (id) {var localRowData = $(this).jqGrid('getLocalRow');alert(localRowData.userCode);}})`
      • @freebird:您似乎不仅仅将 jqGrid 与 ASP.NET Web 窗体一起使用。你改用jqSuite for ASP.NET WebForms。因此,您使用 another product 作为您使用的问题的标签(比较 jqgridjqgrid-asp.net)。我自己不使用 jqSuite。我会简单地看一下 jqSuite 的文档,然后告诉你。
      • @freebird:不客气!我很高兴听到问题已解决。
      【解决方案3】:

      请通读 API。

      希望这就是你要找的东西:http://www.trirand.com/jqgridwiki/doku.php?id=wiki:events

      onSelectRow 事件

      【讨论】:

      • 但是我如何获得我的第一个单元格的值。这就是我需要的。谢谢
      • 我猜可以使用 getRow,getColumn,getCell 方法。我以前没有 JqGrid 的经验,但我相信,你将能够使用它并获取数据。
      【解决方案4】:

      以下代码可帮助您通过一些修改来获取您的案例中的用户代码

          <asp:SqlDataSource runat="server" ID="SqlDataSource1" 
          ConnectionString="<%$ ConnectionStrings:SQL2008_449777_fhsConnectionString %>" 
           SelectCommand="SELECT [OrderID], [RequiredDate], [ShipName],
            [ShipCity], [Freight] FROM [Orders]">    </asp:SqlDataSource>    
            <trirand:JQGrid runat="server" ID="JQGrid1" DataSourceID="SqlDataSource1" 
      

      Width="600px" >

      以下是 jquery,它为您提供列 ID

      <script type="text/javascript">
      function getSelectedRow() { 
             var grid = jQuery("#<%= JQGrid1.ClientID %>");
              var rowKey = grid.getGridParam("selrow");
              if (rowKey)
                  alert("Selected row primary key is: " + rowKey); 
                else
                  alert("No rows are selected");
            }
          function selectSecondRow() {
              var grid = jQuery("#<%= JQGrid1.ClientID %>");
              grid.setSelection('10249');
              } 
         </script>
      

      或查看以下网址:

      http://www.trirand.net/examples/grid/selection/selectedrow_client/default.aspx

      【讨论】:

      • 它有效,但它给了我行号的值,我需要列值。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      相关资源
      最近更新 更多