GridClientSelectColumn - select all rows with enabled check boxes only

Thread is closed for posting
6 posts, 0 answers
  1. Telerik Admin
    Telerik Admin avatar
    1679 posts
    Member since:
    Oct 2004

    Posted 06 Jun 2007 Link to this post

    Requirements

    RadGrid for ASP .NET version

    RadControls for ASP .NET AJAX version
    4.6.2 and later


    2008.1.415 and later
    .NET version

    2.0 and later 

    Visual Studio version

    2005 and later

    Programming language C#, Javascript
    Browser support

    all supported by RadGrid for ASP .NET


    all browsers supported by RadControls for ASP .NET AJAX

    To convert code from posted projects Telerik online converter


    PROJECT DESCRIPTION

    The current scenario demonstrates how to disable specific checkboxes in GridClientSelectColumn so that the end-users won't be able to change their status on the client. By default the header checkbox, however, will still select all data rows, no matter whether the corresponding rows check box is enabled or not. For this purpose the OnRowSelecting client event is used to cancel the selection for each row where the check box is disabled.

    The code below demonstrates the functionality in question:

            <script type="text/javascript"
                function RowSelecting(rowObject) 
                { 
                    //get the input check box 
                    var inputCheckBox = rowObject.Control.getElementsByTagName("input")[0];  
                    if (inputCheckBox.disabled) 
                    { 
                        //cancel selection 
                        return false; 
                    } 
                } 
            </script> 
            <radG:RadGrid ID="RadGrid1" DataSourceID="AccessDataSource1" AllowMultiRowSelection="true" 
                runat="server" OnItemCreated="RadGrid1_ItemCreated"
                <MasterTableView Width="100%"
                    <Columns> 
                        <radG:GridClientSelectColumn UniqueName="ClientSelectColumn" /> 
                    </Columns> 
                </MasterTableView> 
                <ClientSettings ApplyStylesOnClient="True"
                    <Selecting AllowRowSelect="True"></Selecting> 
                    <ClientEvents OnRowSelecting="RowSelecting" /> 
                </ClientSettings> 
            </radG:RadGrid> 

        protected void RadGrid1_ItemCreated(object sender, Telerik.WebControls.GridItemEventArgs e) 
        { 
            //disable each odd item check box 
            if (e.Item.ItemType == GridItemType.Item) 
            { 
                GridDataItem item = (GridDataItem)e.Item; 
                CheckBox checkBox = (CheckBox)item["ClientSelectColumn"].Controls[1]; 
                checkBox.Enabled = false
            } 
        } 
  2. Telerik Admin
    Telerik Admin avatar
    1679 posts
    Member since:
    Oct 2004

    Posted 09 Jul 2007 Link to this post

    Since v4.6.2 of RadGrid GridClientSelectColumn no longer renders the additional literal control as part of each cell. So in order the code from this entry to function properly, one need to modify that a bit:

        protected void RadGrid1_ItemCreated(object sender, Telerik.WebControls.GridItemEventArgs e) 
        { 
            //disable each odd item check box 
            if (e.Item.ItemType == GridItemType.Item) 
            { 
                GridDataItem item = (GridDataItem)e.Item; 
                CheckBox checkBox = (CheckBox)item["ClientSelectColumn"].Controls[0]; //first control since v4.6.2 
                checkBox.Enabled = false
            } 
        } 

    Attachment added in the initial post.
  3. Telerik Admin
    Telerik Admin avatar
    1679 posts
    Member since:
    Oct 2004

    Posted 23 Feb 2008 Link to this post

    Find the project updated with Prometheus RadGrid in the first post as well. Instead of OleDb, the new demo works with SQL backend.
  4. Martin
    Martin avatar
    7 posts
    Member since:
    Dec 2007

    Posted 29 Feb 2008 Link to this post

    I, too, need this functionality in the Prometheus grid.  It only seems natural that the "Select All" checkbox in the header of a GridClientSelectColumn would only select the rows with enabled checkboxes.

    The example code above (GridClientSelectionOnEnabledCheckBoxes_WebUI.zip) does not work on the latest version of Prometheus!  Indeed, it selects the correct rows, but:
    1. The header checkbox does not toggle - it is always unchecked.
    2. Therefore, the rows can not be deselected by checking the header checkbox.
    Not really a viable solution to the problem.

    Any suggestions?
    Martin.
  5. Ves
    Admin
    Ves avatar
    2879 posts

    Posted 08 Mar 2008 Link to this post

    Hi Martin,

    Thanks for the note. Indeed, this issue is observed and it is related to a bugfix in RadGrid. In version 4.6.2 RadGrid used to lose synchronization for the header checkbox in GridClientSelect column, that said it was possible to have the checkbox checked when not all of the items are selected. The fix actually produced the issue in this specific scenario.

    In order to handle it, you will need to attach an event handler to the client-side click event of the header checkbox. Check if there is any item selected and set the checkbox to be checked, else uncheck it. You will also need to manually select the header checkbox in case all enabled checkboxes are checked. Here is the updated client-side code:

    <script type="text/javascript"
            var selecting; 
            var grid; 
            var checkBox; 
            function pageLoad() 
            {             
                checkBox = $get($get('<%= HiddenField1.ClientID %>').value) 
                $addHandler(checkBox, "click", onclickHandler); 
                grid = $find('<%=RadGrid1.ClientID %>'
                // access the grid items, so that they are created and we can use grid selected items count is calculated correctly, bug -- will be fixed in Q1 
                var tmp = grid.get_masterTableView().get_dataItems().length; 
                if (parseInt($get('<%= HiddenField2.ClientID %>').value, 10) + grid.get_masterTableView().get_selectedItems().length == grid.get_masterTableView().get_dataItems().length) 
                { 
                    checkBox.checked = true
                } 
            } 
            function onclickHandler() 
            { 
               if (grid.get_masterTableView().get_selectedItems().length > 0) 
               {         
                    checkBox.checked = true
               } 
               else 
               { 
                    checkBox.checked = false
               } 
            } 
            var selecting = true
            
            function RowSelecting(sender, args) 
            { 
                var id = args.get_id(); 
                var inputCheckBox = $get(id).getElementsByTagName("input")[0]; 
                if (!inputCheckBox || inputCheckBox.disabled) 
                { 
                    //cancel selection for disabled rows 
                    args.set_cancel(true); 
                } 
                // if no more unselected enabled rows left - check the header checkbox 
                else if (parseInt($get('<%= HiddenField2.ClientID %>').value, 10) + grid.get_masterTableView().get_selectedItems().length + 1 == grid.get_masterTableView().get_dataItems().length) 
                { 
                    checkBox.checked = true
                } 
            } 
            function RowDeselecting(sender, args) 
            { 
                    checkBox.checked = false
            } 
      
            </script> 

    You will find the updated project attached to the initial message.

    Best wishes,
    Ves
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  6. Martin
    Martin avatar
    7 posts
    Member since:
    Dec 2007

    Posted 10 Mar 2008 Link to this post

    Ves,
    Thank you - this works in my app.  I hope this issue will be fixed before you go production with Prometheus.  It is quite a bit of code to get around this bug.

    Martin.
Back to Top