Is there a direct way to select rows within a group in jqGrid?

I work with jqGrid, which is grouped by state or province. Each line within the group has a check box. I need to put a checkmark in the group header that allows the user to select / deselect all the checkboxes in this group.

When looking at the HTML code generated by jqGrid, I do not see the classes or identifiers associated with the groups in the rows within the group. I also see no way to add such a class using column options or grouping configuration.

Any suggestions?

+4
source share
2 answers

We solved the same problem with subgrids, and the solution can be adapted to the grouping scenario. We used custom formatting to add style and data attributes for header / checkbox flags and subheading flags. Then we have a link or flag in the title bar with an event on it, which jQuery uses to select all the flags in the subnet with the corresponding data attribute and style. Finally, the grid load completion event adds a click event handler for the check all link.

Here is an example of custom formatting for a sub-heading checkbox column. Notice the data-groupingId attribute, which stores the value that is used to associate the header line with the subnet lines.

function checkBoxColumnFormatter(cellvalue, options, rowObject) { return "<input type=\"checkbox\"" + data-groupingId=\"" + rowObject.GroupingId + "\" class=\"subgridCheck\" />"; } 

Here is an example of custom formatting for the check all column. Notice the data-groupingId attribute, which stores the value that is used to associate the header line with the subnet lines.

 function checkAllColumnFormatter(cellValue, options, rowObject) { var link = $("<a class=\"checkAll\" href=\"#\" data-groupingId=\"" + rowObject.Id + "\">Check All</a>"); var linkHtml = $("<div>").append(link.clone()).remove().html(); return linkHtml; } 

Here is the download completion event that triggers click events for the "verify all" links created in the above format.

 function mainGridLoadComplete(data) { $(".checkAll").click(function (e) { checkSubGridRows( $(this).attr("data-groupingId")); // Use the data attribute to specify the value that will be on all the *relevant* subgrid checkboxes. }); }, 

And finally, here is the method that does the work.

 function checkSubGridRows(groupingId) { $("#GridId .subgridCheck[data-groupingId=\"" + groupingId + "\"]").not(":disabled").each( function () { $(this).attr("checked", "checked"); }); } 

It worked very well for us. Whatever is considered when the situation becomes complicated, I would prefer the client-side model to receive data from the JSON web service and be an authoritative source for jqGrid. I think that ultimately it would be preferable if jqGrid captured the data and swallowed the actual data objects, which makes it difficult or impossible to get the data directly for reference or processing. However, creating and managing a model / view section on the client side is not a trivial task. Thus, it works as a quick alternative. But be careful, because it can get out of FAST control.

+1
source

We solved the problem without using subgrids. Please check the following if it meets your requirements.

HTML code starts here

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Jquery3._Default" %> <%@ Register Assembly="Trirand.Web" TagPrefix="trirand" Namespace="Trirand.Web.UI.WebControls" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link rel="stylesheet" type="text/css" media="screen" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.11/themes/redmond/jquery-ui.css" /> <script src="js/jquery-1.5.2.min.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" media="screen" href="themes/ui.jqgrid.css" /> <script src="js/trirand/i18n/grid.locale-en.js" type="text/javascript"></script> <script src="js/trirand/jquery.jqGrid.min.js" type="text/javascript"></script> <%--Html Code begins here --%> </head> <body> <form id="form1" runat="server"> <div> <script type="text/javascript"> /** * Format the column [AsOfDate]. * Places label for the columns in the grouped rows * places Checkbox in the Group header */ function formatAsOfDate(cellvalue, options, rowObject) { if (parseInt(options.rowId) > 0) { return "<label>" + cellvalue + "<label/>"; } else { return "<input type=checkbox value=" + cellvalue + " onclick='SelectCheckbox(this," + options.rowId + ")'/>" + cellvalue; } } /** * Format the column [DebtStatusId]. * Places label for the columns in the grouped rows * places Checkbox in the Group header */ function formatDebtStatusId(cellvalue, options, rowObject) { if (parseInt(options.rowId) > 0) { return "<label>" + cellvalue + "<label/>"; } else { return "<input type=checkbox value=" + cellvalue + " onclick='SelectCheckbox(this," + options.rowId + ")' />" + cellvalue; } } /** * To select/Deselect all the grouped rows based on the checkbox selected at group level. */ function SelectCheckbox(chkbox, groupingId) { var grid = jQuery("#<%= JQGrid1.ClientID %>"); var Rows = grid.find("TR"); $.each(Rows, function(i, item) { var label = $(item).find("label"); if (label.length > 0) { $.each(label, function(i, labelitem) { if (labelitem.innerText === chkbox.defaultValue) { var CheckBox = $(item).find("INPUT.cbox"); $(chkbox).click(function() { if ($(this).attr("checked")) { CheckBox.attr("checked", "checked"); } else { CheckBox.removeAttr("checked"); } }) } }) } }); } /** * To Edit the Selected Row(s). */ function Selectedrow() { var grid = jQuery("#<%= JQGrid1.ClientID %>"); var rowKey = grid.getGridParam("selarrrow"); if (rowKey) { grid.editGridRow(rowKey, grid.editDialogOptions, { reloadAfterSubmit: false }); } else { alert("No rows are selected"); } } </script> <span style="font-size: 140%"><b>Group grid by:</b> </span> <asp:DropDownList runat="server" ID="groupColumnDdl" AutoPostBack="true"> <asp:ListItem Text="DebtStatusID" Value="DebtStatusID" /> <asp:ListItem Text="AsOfDate" Value="AsOfDate"></asp:ListItem> </asp:DropDownList> <trirand:JQGrid ID="JQGrid1" runat="server" Height="200px" OnRowEditing="JQGrid1_RowEditing" AppearanceSettings-Caption="First Grid" MultiSelect="true"> <Columns> <trirand:JQGridColumn DataField="DebtID" PrimaryKey="True" /> <trirand:JQGridColumn DataField="SequenceNumber" Editable="true" /> <trirand:JQGridColumn DataField="DebtStatusID"> <Formatter> <trirand:CustomFormatter FormatFunction="formatDebtStatusId" /> </Formatter> </trirand:JQGridColumn> <trirand:JQGridColumn DataField="AsOfDate" DataFormatString="{0:d}"> <Formatter> <trirand:CustomFormatter FormatFunction="formatAsOfDate" /> </Formatter> </trirand:JQGridColumn> <trirand:JQGridColumn DataField="Alias" Editable="true" /> </Columns> <SortSettings InitialSortColumn="DebtID"></SortSettings> <EditDialogSettings CloseAfterEditing="false" /> <AppearanceSettings ShowRowNumbers="True" Caption="First Grid"></AppearanceSettings> <ToolBarSettings ShowEditButton="true" ShowRefreshButton="True" /> </trirand:JQGrid> <input type="button" onclick="Selectedrow()" value="Edit" /> <div style="display: none;"> <input type='checkbox' id="chkTest" runat="server" /> </div> </div> </form> </body> </html> 

Cs code starts here

  using System; using System.Collections; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using Trirand.Web.UI.WebControls; namespace Jquery3 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { JQGrid1.DataSource = GetData(); JQGrid1.DataBind(); GroupField gf = new GroupField(); //Dynamic Grouping based on the combo value selected switch (groupColumnDdl.SelectedValue) { case "DebtStatusID": gf = new GroupField { DataField = "DebtStatusID", HeaderText = "DebtStatus ID : {0}", GroupSortDirection = Trirand.Web.UI.WebControls.SortDirection.Asc, ShowGroupColumn = true, ShowGroupSummary = false }; break; case "AsOfDate": gf = new GroupField { DataField = "AsOfDate", HeaderText = "AsOfDate : {0}", GroupSortDirection = Trirand.Web.UI.WebControls.SortDirection.Asc, ShowGroupColumn = true, ShowGroupSummary = false }; break; } JQGrid1.GroupSettings.GroupFields.Add(gf); } protected void JQGrid1_RowEditing(object sender, Trirand.Web.UI.WebControls.JQGridRowEditEventArgs e) { var rows = e.RowKey; ArrayList ArrayOfIds = new ArrayList(rows.Split(new char[] { ',' })); for (int i = 0; i < ArrayOfIds.Count; i++) { DataSet ds = GetData(); DataTable dt = ds.Tables[0]; dt.PrimaryKey = new DataColumn[] { dt.Columns["DebtID"] }; DataRow rowEdited = dt.Rows.Find(ArrayOfIds[i]); rowEdited["SequenceNumber"] = e.RowData["SequenceNumber"]; rowEdited["DebtStatusID"] = e.RowData["DebtStatusID"]; rowEdited["Alias"] = e.RowData["Alias"]; } JQGrid1.DataSource = GetData(); JQGrid1.DataBind(); } protected DataSet GetData() { if (Session["EditDialogData"] == null) { string ConnectionString = "Data Source =192.168.0.20; Initial Catalog = LW_TTX_IMPL; User ID=Development;password=jk;"; DataSet ds = new DataSet(); SqlConnection sqlconn = new SqlConnection(ConnectionString); SqlDataAdapter sqlAdp = new SqlDataAdapter(); sqlAdp.SelectCommand = new SqlCommand("SELECT DebtID,SequenceNumber,DebtStatusID,AsOfDate,'Alias'+Alias Alias FROM Debt_Profile", sqlconn); sqlAdp.Fill(ds); Session["EditDialogData"] = ds; return ds; } else { return Session["EditDialogData"] as DataSet; } } } } 
+1
source

All Articles