:: Forum >> Version 2 >>

"classic" select combo example - now on edit only!

More information on this topic is available in the documentation section: /aw.templates.combo/.

Same as previous example ( /javascript.forum.16874.1/classic-select-combo-example.html ), now the select appears only onCellClicked.
It all works but it's messy, not really reusable and column-wise, I still think I could do much better with a template, but I don't really know how to start.

<html>
    <
head>
        <
title>ActiveWidgets grid with classic "select" combobox on edit only</title>
        <
script src="/Test/ActiveWidgets/runtime/lib/aw.js" language="javascript"></script>
        <
link href="/Test/ActiveWidgets/runtime/styles/xp/aw.css" rel="stylesheet" />
    </
head>
    <
body>
        <
script language="javascript">
<!--
var 
grid = new AW.UI.Grid;

var 
myCells = [
    [
"a"1"EN"],
    [
"b"2"EN"],
    [
"c"3"FR"],
    [
"d"2"FR"],
    [
"e"1"EN"]
];

var 
myHeaders = ["text""combo""language"];

// assign cells and headers text
grid.setHeaderText(myHeaders);
grid.setCellData(myCells);

// set number of columns/rows
grid.setColumnCount(myHeaders.length);
grid.setRowCount(myCells.length);



// rows need to be bigger to fit the combos
grid.setRowHeight(25);

grid.setCellText(
    function(
colrow){ return getText(colrow, [1234], ["Option 1""Option 2""Option 3""Option 4"]) },
    
1);    // column 1 (combo) is a combo

grid.setCellText(
    function(
colrow){ return getText(colrow, ["EN""FR"], ["English""French"]) },
    
2);    // column 2 (language) is a combo




grid.onCellValueChanged = function(valuecolrow)
{
    
alert("value: " value ", col: " col ", row: " row);
}

var 
lastCell;
grid.onCellClicked = function(ecolrow)
{
    if (
lastCell && lastCell.col == col && lastCell.row == row)
        return;    
// same cell was clicked: do nothing
//console.debug("onCellClicked - col: " + col + ", row: " + row);
    
    
    
if (lastCell && (lastCell.col == || lastCell.col == 2))
    {    
// we had a combo cell selected before: was it changed?
        
var originalValue grid.getCellValue(lastCell.collastCell.row);
        var 
newValue grid.getCellTemplate(lastCell.collastCell.row).element().getElementsByTagName("select")[0].value;
//console.debug("had combo before - original value: %s - new value: %s", originalValue, newValue);
        
        // if value WAS changed, commit changes
        
if (newValue != originalValue)
            
grid.setCellValue(newValuelastCell.collastCell.row);
        
        
// why isn't the cell updated on setCellValue ?? 
        // I shouldn't have to do that manually...
        
if (lastCell.col == 1)
        {
            var 
text getText(lastCell.collastCell.row, [1234], ["Option 1""Option 2""Option 3""Option 4"]);
            
grid.setCellText(textlastCell.collastCell.row);
        }
        else if (
lastCell.col == 2)
        {
            var 
text getText(lastCell.collastCell.row, ["EN""FR"], ["English""French"]);
            
grid.setCellText(textlastCell.collastCell.row);
        }
    }
    
    
// current cell is now the last cell
    
lastCell = {colcolrowrow};
    
    
    
// change cell content to a combobox (it would be nice to open it automatically too...)
    
if (col == 1)
    {
        var 
text buildCombo(colrow, [1234], ["Option 1""Option 2""Option 3""Option 4"]);
        
grid.setCellText(textcolrow);
    }
    else if (
col == 2)
    {
        var 
text buildCombo(colrow, ["EN""FR"], ["English""French"]);
        
grid.setCellText(textcolrow);
    }
}




// This will get the text corresponding to cell(col, row)'s value.
function getText(colrowvaluestexts)
{
    var 
value grid.getCellValue(colrow);
    for (var 
0values.lengthi++)
        if (
values[i] == value)
            return 
texts[i];
}

// This will build a combo, populated with items specified in 'values' and 'texts' (must be arrays of same length),
// and select the proper item according to cell(col, row)'s value.
function buildCombo(colrowvaluestexts)
{
    var 
value grid.getCellValue(colrow);
    var 
select "<select>";
    
    
// Here we create the options, basically putting correct value and text,
    // and adding 'selected' if the current cell's value is the current option's value.
    
for (var 0values.lengthi++)
    {
        var 
option "<option value='%value%' %selected%>%text%</option>";
        
option option.replace("%value%"values[i]);
        
option option.replace("%text%"texts[i]);
        
option option.replace("%selected%", (value == values[i]) "selected" "");
        
select += option;
    }
    
    
select += "</select>"
    
return select;
}


document.write(grid);

//-->
        
</script>
    </
body>
</
html>
 
Ben
Wednesday, October 25, 2006



This topic is archived.

Back to /aw.templates.combo/

Documentation:

Forum search