Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Rachel Santamaría on Oct 14, 2009 01:55
open dhtmlx forum
grid cell with numberFormat and value 0 is null

Hello!

I have a grid with a column "edn" and numberFomat:

----
    gridPrecios.setNumberFormat("0.00 €", 2);
----

I have a row with a value 0 in column "edn".
And this event onCellEdit:

----
    if (stage == 1 && this.editor && this.editor.obj) {
        this.editor.obj.select();
        return true;
    }
    if (stage == 2) {
        if (gridPrecios.cells(rowId, 2).getValue() == "") gridPrecios.cells(rowId, 2).setValue("0");
        alert (gridPrecios.cells(rowId, 2).getValue());    //shows empty string!!!!!!!!!
    }
    return true;
----

How can I have a value 0 in this cell and that it shows 0 in function getValue?

Thanks!
Bye
Answer posted by Alex (support) on Oct 20, 2009 05:45

Hello,

insetad of   if (gridPrecios.cells(rowId, 2).getValue() == "") gridPrecios.cells(rowId, 2).setValue("0");

you can use the following approach:

 if (gridPrecios.cells(rowId, 2).getValue() == "") return "0.0"; 

Please, see the http://www.dhtmlx.com/dhxdocs/doku.php?id=dhtmlxgrid:event_oneditcell page for more details.

Answer posted by Rachel Santamaría on Oct 20, 2009 07:31

This is a bad solution because I have to validate more cells and I can't return at this moment.

I need to put 0 in this cell and continue with calculations...

 

Answer posted by Alex (support) on Oct 20, 2009 07:51
The provided approach is the only way to set custom value right after the editor is closed.  What calculation do you want to execute ? Possibly it can be done before return command.
Answer posted by Rachel Santamaría on Oct 20, 2009 07:57

This is my code:

----

gridPrecios.attachEvent("onEditCell", function(stage, rowId, cellInd, newValue, oldValue){

  if (stage == 2) {
   if (gridPrecios.cells(rowId, 2).getValue() == "") gridPrecios.cells(rowId, 2).setValue("0"); //<----------------------------

   if (cellInd == 0 || cellInd == 1) {
    if (!validarEntero(gridPrecios.cells(rowId, cellInd).getValue())) {
     gridPrecios.cells(rowId, cellInd).cell.style.backgroundColor = "#FE8383";
     return false;
    } else {
     gridPrecios.cells(rowId, cellInd).cell.style.backgroundColor = "";
    }
   } else {
    if (isNaN(gridPrecios.cells(rowId, cellInd).getValue())) {
     gridPrecios.cells(rowId, cellInd).cell.style.backgroundColor = "#FE8383";
     return false;
    } else {
     gridPrecios.cells(rowId, cellInd).cell.style.backgroundColor = "";
    }
   }

   var celda0 = gridPrecios.cells(rowId, 0).getValue();
   var celda1 = gridPrecios.cells(rowId, 1).getValue();
   if (celda0 != "" && celda1 != "") {
    if (Number(celda0) >= Number(celda1))
     return false;
   }

   var error = false;
   if (celda0 != "" && celda1 != "") {
    gridPrecios.forEachRow(function(row){
     if (gridPrecios.cells(row, 0).getValue() != "" && gridPrecios.cells(row, 1).getValue() != "" && row != rowId) {
      if (celda1 >= Number(gridPrecios.cells(row, 0).getValue()) && celda0 <= Number(gridPrecios.cells(row, 1).getValue())) {
       gridPrecios.cells(rowId, 0).cell.style.backgroundColor = "#FE8383";
       gridPrecios.cells(rowId, 1).cell.style.backgroundColor = "#FE8383";
       error = true;
      } else {
       gridPrecios.cells(rowId, 0).cell.style.backgroundColor = "";
       gridPrecios.cells(rowId, 1).cell.style.backgroundColor = "";
      }
     }
    });
   }
   if (error == true) {
    return false;
   }
   var celda2 = ((gridPrecios.cells(rowId,2).getValue()=="")?"0":gridPrecios.cells(rowId,2).getValue());
   if (gridPrecios.cells(rowId,0).getValue() != "" && gridPrecios.cells(rowId,1).getValue() != "" && celda2 != "" && cellInd == 2) {
    gridPrecios.addRow("n"+contFila,",,");
    contFila ++;
   }
  }
  return true;
 });

----

Answer posted by Alex (support) on Oct 20, 2009 08:08

In this case you can get value once, put it in some variable and manipulate with it. There is no need to call setValue and getValue methods sevaral times.

Answer posted by Rachel Santamaría on Oct 20, 2009 08:20
I remember that if in the oncelledit event didn't return "true", something failure. Can I be?
Answer posted by Alex (support) on Oct 20, 2009 08:42

This event can return true(1), false(0) or some other value. Please, see details here:

http://www.dhtmlx.com/dhxdocs/doku.php?id=dhtmlxgrid:event_oneditcell