Author Topic: Calculated column  (Read 8035 times)

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Calculated column
« on: February 06, 2014, 06:26:33 pm »
The last column of my table is a calculated field (the difference between the two previous columns).

When users add a row:
  • Is it possible to calculate this field automatically, disallowing users to edit?
  • Can I give this column a different style (eg: color)?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Calculated column
« Reply #1 on: February 06, 2014, 07:01:28 pm »
1) If it's for view purpose only, then you can use column.render callback to calculate the field. And keep column.editable to false to disallow editing.
http://paramquery.com/pro/api#option-column-render
http://paramquery.com/pro/api#option-column-editable

2) use column.cls
http://paramquery.com/pro/api#option-column-cls

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Calculated column
« Reply #2 on: February 06, 2014, 08:36:35 pm »
(I've attached the question, since it isn't being posted correclty here)

This is the definition of the column:

[code]colModel: [
{ title: "Desvio", width: 100, dataType: "float", align: "right", dataIndx: "desvio", cls:'coluna_calc', editable: false, render:function(ui){return "
« Last Edit: February 06, 2014, 08:39:51 pm by nuno.nogueira »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Calculated column
« Reply #3 on: February 06, 2014, 08:47:16 pm »
Please refer to this live example ( css tab) for adding cls in the column.

http://paramquery.com/pro/demos/selection_checkbox

Code: [Select]
render : function(ui){
  return (ui.rowData["revenue"] - ui.rowData["expenses"]);
}

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Calculated column
« Reply #4 on: February 06, 2014, 10:12:04 pm »
The calculation is working OK after refreshing the table, but I was trying to have the calculation ready whenever any of the numbers in the columns were updated. I guess I need an event for cell change AND a callback function for that.

As for adding the class to the column, no matter what I do, it doens't get added. This is what I have in colModel:

Code: [Select]
{ title: "Desvio", width: 100, dataType: "float", align: "right", dataIndx: "desvio", cls:'coluna_calc', editable: false}

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Calculated column
« Reply #5 on: February 06, 2014, 11:49:34 pm »
Use cellSave event to call refreshCell for the computed cell.

http://paramquery.com/pro/api#event-cellSave
http://paramquery.com/pro/api#method-refreshCell


Did you check whether class is injected in the column using DOM inspector.

EDIT:
============================================================
I see that class has been added in the column. You have to make change in css for it to take effect.

Code: [Select]
.pq-grid .coluna_calc {
  color: red;
}
« Last Edit: February 07, 2014, 10:55:19 am by paramquery »

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Calculated column
« Reply #6 on: February 07, 2014, 06:12:45 pm »
Yes, class has been added, thanks!  :D

I'm now checking cellSave and refreshCell

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Calculated column
« Reply #7 on: February 11, 2014, 05:16:15 pm »
(Here's the question in attahcment)

Hi,

I have to get back to the calculated column issue, sorry..

The question is, how do I trigger the function to calculate column "desvio", whenever cells in columns "atual" or "orcamento" are saved locally?

I assume the cellSave event has to included in these two columns, but how do I call the function?

This is what I have in colModel:

Code: [Select]
colModel: [
{ title: "Or

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Calculated column
« Reply #8 on: February 11, 2014, 05:44:28 pm »
just call refreshCell ( {dataIndx :'desvio', rowIndx:ui.rowIndx}) or refreshRow( {rowIndx: ui.rowIndx}) from cellSave event.

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Calculated column
« Reply #9 on: February 11, 2014, 06:31:11 pm »
Yes, calculated field in on, thanks!
 :)