Is there a straight-forward way to select the rows within a group in jqGrid?
I am working with a jqGrid which is grouped based on state or province. Each row within the group has a che开发者_StackOverflow中文版ckbox. I need to place a checkbox in the group header which allows the user to select/deselect all of the row checkboxes within that group.
In looking at the jqGrid-generated HTML, I don't see any classes or IDs which are group-related on the rows within a group. I also don't see a way to add such a class using the column options or grouping configuration.
Any suggestions?
We have solved this same problem with subgrids, and the solution may be adaptable to a grouping scenario. We used custom formatters to add styling and data attributes for both the header link/checkbox and the subgrid checkboxes. Then we have a link or checkbox in the header row with an event on it that uses jQuery to select all the checkboxes in the subgrid with the appropriate data attribute and style. Finally, the grid load complete event adds the click event handler for the "check all" link.
Here's an example custom formatter for the subgrid checkbox column. Note the data-groupingId attribute which stores a value that is used to relate the header row to the subgrid rows.
function checkBoxColumnFormatter(cellvalue, options, rowObject) {
return "<input type=\"checkbox\"" + data-groupingId=\"" + rowObject.GroupingId + "\" class=\"subgridCheck\" />";
}
Here's an example custom formatter for the "check all" column. Note the data-groupingId attribute which stores a value that is used to relate the header row to the subgrid rows.
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's the load completion event that hooks up the click events for the "check all" links created in the above formatter.
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's the method that does the work.
function checkSubGridRows(groupingId) {
$("#GridId .subgridCheck[data-groupingId=\"" + groupingId + "\"]").not(":disabled").each(
function () {
$(this).attr("checked", "checked");
});
}
This has worked very well for us. All things considered, when things get to be complicated like this, I'd rather have a client-side model to receive data from the JSON web service and be the authoritative source for the jqGrid. I think this would ultimately be preferable to having the jqGrid grab the data itself and swallow the actual data objects, which makes it difficult or impossible to get at the data directly for reference or processing. However, creating and managing a client-side model/view separation is not a trivial undertaking. So this works as a quick alternative. But beware, because this can get out of hand FAST.
We had solved the issue without using subgrids. Please check the following whether it matches your requirement.
HTML Code Starts from 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 Begins 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;
}
}
}
}
精彩评论