QcheckBoxColumn select All
Wed, 04/13/2011 - 07:47
Hi, I would like know how to add an action when I checked the checkbox to select all checkbox that I have in a DataGrid
I've seen the source of the example about QCheckBoxColumn (http://examples.qcu.be/assets/_core/php/examples/dynamic/qcheckboxcolumn...) and I try it, now I have actions when I check some one in a row but I need have an action when I check 'Select All', how can I do it?
Thanks for any answer! :)

Hm, tricky. The 'checkall' checkbox is a QCheckbox but QCheckBoxColumn does not allow accessing it. I suppose what could work is creating a new class that inherits from QCheckBoxColumn that does allow setting an action on that checkbox.
and other way to achieve it's possible??
Hello,
There is a different way, but it is not very elegant to be honest. How shall I explain this: The QSelectColumn creates the 'select all' checkbox on rendering, but only if the select box does not exist already as a child control of your data grid. What you therefore do is creating a checkbox control with the correct control id, the javascript action that does the check all operation, and your new actions and add it as a child control to your data grid on 'form create'.
Doing this can be dangerous because if somebody changes something in the core framework to the check all box - like e.g. adding another action - it won't be reflected in your manually created check box. However, I tested it and I could add a new action to the 'select all' control (see code below).
Why do you want to add an action anyway?
<?php
...
//Create the select column
$colSelect = new QCheckBoxColumn(QApplication::Translate('Select All'), $this->batchTable);
// And we make sure to set HtmlEntities to "false" so that our checkbox doesn't get HTML Escaped
$colSelect->HtmlEntities = false;
$colSelect->PrimaryKey = "Oid";
//and in this case, we want to interceed whenever a checkbox is rendered so that we can assign
//an action to it
$colSelect->SetCheckboxCallback($this, 'chkSelected_Render');
$this->batchTable->AddColumnAt(0, $colSelect);
// Call the function that creates the new control. It needs the coliumn position for determining the correct controlId.
$chkControl = $this->GetSelectAllAt(0,$colSelect);
// Add a new action
$chkControl->AddAction(new QClickEvent(), new QJavaScriptAction('alert("test");'));
// Add your control as a new child control
$this->batchTable->AddChildControl($chkControl);
...
public function GetSelectAllAt($colIndex,$objCol){
$controlId = 'chkSelectAll' . $colIndex.$this->batchTable->ControlId ;
$chkSelectAll = new QCheckBox($this->batchTable, $controlId);
$chkSelectAll->Name = QApplication::Translate('Select All');
$strControlIdStart = 'chkSelect'.colIndex.$this->batchTable->ControlId.'n';
$strControlIdStartLen = strlen($strControlIdStart);
//Since a QDataGridColumn isn't a control, we can't include external js files, or have EndScripts
//so we'll just have to include all the code in the onclick itself
//hopefully this won't result in much duplication, since there shouldn't be too many
//of these on a single form
$strJavascript = "var datagrid = document.getElementById('{$this->batchTable->ControlId}');var selectAll = document.getElementById('{$chkSelectAll->ControlId}');var childInputs = datagrid.getElementsByTagName('input');for(var i = 0; i < childInputs.length; i++){var subid = childInputs[i].id.substring($strControlIdStartLen, 0);if(subid == '$strControlIdStart')childInputs[i].checked = selectAll.checked;}";
$chkSelectAll->AddAction(new QClickEvent(),new JavaScriptAction($strJavascript));
return $chkSelectAll;
}
?>
oh! thanks again! may be I'll try this way late, this morning I found other way to do it. In the function PreRender I see if some checkbox are selected with 'colSelect->GetSelectedIds()' and if this are more than 0, I show a qpanel with some options to do with the checkbox selected, now I would like try to know how many elements are selected in every page of my qdatagrid, because if I check All in first page, when I change of page, the checkbox are checked, any idea to do it?
thanks again for your help!
can't you just use GetControl($the_ID_of_the_checkall_box) to get the checkall control and then add actions to that?
edit: i grepped the source quickly and found this
<?php$controlId = 'chkSelectAll' . $colIndex.$this->objDataGrid->ControlId ;
?>
so you can build the ID with getting the column index, datagrid control ID and prepending 'chkSelectAll' to it, then you should be able to access the selectAll checkbox control.
Forget my comment below. You started the thread below yourself!!!
die();
getControl does not work because the checkbox is created on rendering.
Check this thread out regarding checking the checkboxes:
http://qcu.be/content/how-know-page-item-datagrid
Cheers
Helge