Visit our SharePoint Forum

SharePoint developer? Submit Yourself as Freelancer

Thursday, April 9, 2009

SPGridView Grouping - Select Whole Group


After enabling grouping in SPgridview, I had a requirenment to select all the items under a group by clicking on a checkbox box in a group header row. Since I couldn't find any way to craete a checkbox in groud header row, I did the the selection thing by creating SPMenufield on groupheader.
Code for creating SPMenuField "Select Group"(or Select Learning Obj in my case)
SPMenuField colMenu = new SPMenuField();
colMenu.HeaderText = "";
colMenu.TextFields = "Learning_x0020_Object_x0020_Name"; //
GroupName field, bind to a List column

colMenu.MenuTemplateId = "GroupSelectMenu";
colMenu.NavigateUrlFields = "id,Learning_x0020_Object_x0020_Name";
colMenu.NavigateUrlFormat = "do.aspx?p={0}&q={1}";
colMenu.TokenNameAndValueFields = "EDIT=id,NAME=Learning_x0020_Object_x0020_Name";
colMenu.MenuFormat = MenuFormat.ArrowOnHover;
colMenu.SortExpression = "Learning_x0020_Object_x0020_Name";
MenuTemplate GroupSelectMenu = new MenuTemplate();
GroupSelectMenu.ID = "GroupSelectMenu";

MenuItemTemplate SelectGroup = new MenuItemTemplate("Select Learning Object");
//Added Javascript to SelectGroup check all the boxes under the group.
SelectGroup.ClientOnClickScript = "SelectGroupFun('%NAME%','" + oGrid.ClientID.ToString() + "');";

GroupSelectMenu.Controls.Add(SelectGroup);
this.Grid.GroupMenu = colMenu;
this.Controls.Add(GroupSelectMenu);

Javascript To get check all the boxes in a group

function SelectGroupFun(ObjName,gridid)
{
var SPGridView = document.getElementById(gridid);
for (var i = 3; i SPGridView.rows.length; i++)
{
var GroupField = SPGridView.rows[i].cells[5];
// GroupName field
var ItemTempCheckbox = SPGridView.rows[i].cells[4]; // Checkbox field
if(GroupField != null && GroupField != 'undefined')
{
if(ObjName== GroupField .innerText)
{
if(ItemTempCheckbox.childNodes[0].type == 'checkbox' )
{
if(!ItemTempCheckbox.childNodes[0].checked)
{
ItemTempCheckbox.childNodes[0].checked = true;
} } }
} }
}
Get Selected Items
function SelectedItem(gridid)
{
var SPGridView = document.getElementById(gridid);
// create array of selected items
var myArray = new Array();
var x=0;

for (var i = 3; i SPGridView.rows.length; i++)
{
var GroupNameField = SPGridView.rows[i].cells[5];
var ItemTempCheckbox = SPGridView.rows[i].cells[4];
var Title = SPGridView.rows[i].cells[1]
if(GroupNameField != null && GroupNameField != 'undefined')
{
if(ItemTempCheckbox.childNodes[0].type == 'checkbox' && ItemTempCheckbox.childNodes[0].checked )
{
myArray[x] = Title.innerText+"_"+GroupNameField .innerTextx++;
} } }
//alert(myArray.length);
return myArray;
}

1 comments:

Sudheer said...

Hi,
Even i too have written the same thing but in my case in javascript it is throwing error as ';' missing in line 4,char 19

Regards,

Sudheer

SharePoint Programming