Welcome Guest Search | Active Topics | Sign In | Register

appending new item in grid with CheckBoxColumn and AllowNewItem set to true Options
William
Posted: Friday, December 18, 2009 5:25:19 AM
Rank: Newbie
Groups: Member

Joined: 12/18/2009
Posts: 4
Hi,

I want a grid with CheckBoxColumn(CheckBox enabled) and AllowNewItem set to true.
And the problem is when the new item is appended, I need the CheckBox to be disabled.
How can I achieve that?

Thanks in advance.
eo_support
Posted: Friday, December 18, 2009 8:46:37 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,200
Hi,

You won't be able to do that with CheckBoxColumn. However you should be able to do it with CustomColumn or StaticColumn. CustomColumn is basically an empty cell provided to you by the Grid, and you can do whatever you want inside the cell. You can also use a StaticColumn and then set the columns text to some raw HTML (or set DataFormat to some raw HTML) that includes a checkbox. Because it is your own checkbox, you will have full control of it.

Thanks!
William
Posted: Sunday, December 20, 2009 9:33:55 PM
Rank: Newbie
Groups: Member

Joined: 12/18/2009
Posts: 4
Hi,
Thanks for the reply. I've tried using a StaticColumn and set the DataFormat to "{0:<input type='checkbox' name='cbDel' />}" and it doesn't work. I've also searched the Live Demo but didn't find similar example. Could you show me what document or example should I read?

Thanks!
eo_support
Posted: Sunday, December 20, 2009 9:41:43 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,200
Hi,

You can not have your HTML inside the brackets. It should be "{0} <input ..... />". It's the same syntax as the standard .NET String.Format function.

Thanks!
William
Posted: Sunday, December 20, 2009 10:31:08 PM
Rank: Newbie
Groups: Member

Joined: 12/18/2009
Posts: 4
Hello,

I Set the DataFormat to "{0} <input ..... />" and it still doen't work. Then I tried setting the Text to "<input....>" and the checkbox showed up. But I still can't make it disappear when appending new items. Would you show me how to achieve this using both CustomoColumn and StaticColumn?

Thanks for the quick response.
eo_support
Posted: Sunday, December 20, 2009 10:47:12 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,200
Hi,

The key is the column replaces "{0}" with the cell value and then displays it. A few examples:

1. If your cell value is "abc" and your DataFormat is "<input ... /> {0} ", then the Grid will display "<input .... /> abc" for you. This creates a checkbox with text "abc" after it;

2. If your cell value is "checked" and your format is "<input checked='{0}' />", then the Grid will display "<input checked='checked' />" for you. This creates a checked checkbox for you;

3. If your cell value is "style='display:none'" and your format is "<input {0} />", then the Grid will display "<input style='display:none' .../>" for you. This actually hides the checkbox;

Thus by combining cell values and DataFormat, you can pretty much create whatever HTML you'd like. Because each cell can have different values, you can construct different HTML for different cells even though they have the same DataFormat.

When you initially populate the Grid from the server side, the Grid gets all cell values from your data source. Once it goes to the client side, you can use our client side API to modify cell values with this function:

http://doc.essentialobjects.com/library/1/jsdoc.public.gridcell.setvalue.aspx

CustomColumn is much more complicated (and much more powerful). You will want to go over the documentation and samples first to get an idea about how it works. Once you are familiar with the basics we will be happy to assist you if you still have any questions.

Thanks!
William
Posted: Monday, December 21, 2009 5:03:07 AM
Rank: Newbie
Groups: Member

Joined: 12/18/2009
Posts: 4
Hi,

I tried the "DataFormat" method as you said but it doesn't work. Is it possible that the properties of the grid or callbackpanel are not set correctly?

I also tried the "Text" method:
<eo:StaticColumn DataField="" DataFormat="" HeaderText="Delete" Text="&lt;input type='checkbox' disabled=false id='cbDel' /&gt;">
</eo:StaticColumn>

and then set all the checkbox except the last one to be enabled by calling the following function through ClientSideOnLoad of the grid:
function Set_initialGrid()
{
var grid = eo_GetObject('gridResult');
var i = 0;
for (i = 0; i < grid.getItemCount() ; i++)
{
document.all.cbDel2[i].disabled=false;
}
return;
}

I can access the checkbox. But they return to the original setting right away. Is there other things I should do?

eo_support
Posted: Monday, December 21, 2009 10:01:48 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,200
William wrote:
I tried the "DataFormat" method as you said but it doesn't work. Is it possible that the properties of the grid or callbackpanel are not set correctly?

We looked into this and noticed that you must have a cell value. The Grid does not apply DataFormat when it does not have a cell value.

William wrote:
I also tried the "Text" method:
<eo:StaticColumn DataField="" DataFormat="" HeaderText="Delete" Text="&lt;input type='checkbox' disabled=false id='cbDel' /&gt;">
</eo:StaticColumn>

and then set all the checkbox except the last one to be enabled by calling the following function through ClientSideOnLoad of the grid:
function Set_initialGrid()
{
var grid = eo_GetObject('gridResult');
var i = 0;
for (i = 0; i < grid.getItemCount() ; i++)
{
document.all.cbDel2[i].disabled=false;
}
return;
}
I can access the checkbox. But they return to the original setting right away. Is there other things I should do?

This is normal because whenever the Grid tries to "refresh" a cell, it refreshes based on Value, DataFormat and Text, in your case, based on your Text property. So when it does a refresh, it reloads Text and you lose every change you made. Thus it is important any change you made is made to the cell value. This also means you need to update the Grid cell value whenever user clicks on the checkbox. The Grid remembers and maintains cell values and considers everything else can be regenerated.

Another value that the Grid remembers and maintains on a per cell basis is the cell style. You can call overrideStyle to update the cell style. This can be useful for your situation. Below is a working sample for your scenario:

Code: CSS
DIV.normal input.normal
{
}

DIV.normal input.disabled
{
    display: none;
}

DIV.disabled input.normal
{
    display: none;
}

DIV.disabled input.disabled
{
}


Code: JavaScript
//This function is called after the Grid is loaded
//through ClientSideOnLoad
function initGrid()
{
    var grid = eo_GetObject("Grid1");
    var itemCount = grid.getItemCount();
    for (var i = 0; i < itemCount; i++)
    {
        //Get the cell object
        var cell = grid.getItem(i).getCell(0);
        
        //"Attach" the item index to the checkbox object
        cell.setValue("itemIndex='" + i + "'");
        
        //Disable/enable checkbox
        if (i == itemCount - 1)
            cell.overrideStyle("disabled");
        else
            cell.overrideStyle("normal");
    }
}

//This function is called when a checkbox is clicked
function checkboxClicked(cb)
{
    var grid = eo_GetObject("Grid1");
    
    //Get the item index from the checkbox
    var itemIndex = parseInt(cb.itemIndex);
    var cell = grid.getItem(itemIndex).getCell(0);

    //Format the new cell value. This new cell value
    //still contains the itemIndex, but also the new
    //checked state
    var cellValue = "itemIndex='" + itemIndex + "'";
    if (cb.checked)
        cellValue += " checked='checked'";

    //Update the cell value. This will refresh
    //the cell HTML if necessary
    cell.setValue(cellValue);
}


Code: HTML/ASPX
<eo:Grid ClientSideOnLoad="initGrid" ....>
    ....
    <Columns>
        <eo:StaticColumn DataFormat="
            &lt;input class=&quot;normal&quot; type=&quot;checkbox&quot; 
            {0} onclick=&quot;checkboxClicked(this);&quot; /&gt;
            &lt;input class=&quot;disabled&quot; type=&quot;checkbox&quot; 
            disabled=&quot;disabled&quot; /&gt;" >
         </eo:StaticColumn>
    </Columns>
</eo:Grid>


The only static column's DataFormat is set to:

Code: HTML/ASPX
<input class="normal" type="checkbox" {0} 
onclick="checkboxClicked(this)" />
<input class="disabled" type=checkbox" disabled="disabled" />


It actually generates two checkboxes per cell. One is enabled, and the other is disabled. If the cell's style is set to "normal", then rule "DIV.normal input.normal" is applied to the first checkbox and "DIV.normal input.disabled" is applied to the second checkbox. This results in the first checkbox being visible and the second one being invisible, thus rendering an enabled checkbox. If the cell's style is set to "disabled", then rule "DIV.disabled input.normal" is applied to the first checkbox and rule "DIV.disabled input.disabled" is applied to the second checkbox. This results in the first checkbox being invisible and the second one being visible, thus rendering a disabled checkbox.

The first checkbox also has an onclick handler which updates the cell value when clicked. It passes in "this" as the only parameter, which represents the checkbox itself. The code formats each cell's value to contains an itemIndex attribute so that later checkboxClicked can get the item index from the checkbox. It also updates "checked" attribute accordingly. For example, if the first item is clicked, the refresh HTML will be like this:

Code: HTML/ASPX
<input class="normal" type="checkbox" 
itemIndex="0" checked="checked" 
onclick="checkboxClicked(this)" />
<input class="disabled" type=checkbox" disabled="disabled" />


The second line the new "cell value".

Using CustomColumn for this particular scenario may result in cleaner code because CustomColumn calls your JavaScript to format cell HTML and passes you the cell object directly. However the underlying principle is the same. Changes are always made to the cell value and cell HTML is always regenerated from the cell value.

Hope this helps.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.