Custom Field Renderer - To do Simple Calculations



  • edited December 18, 2014
    I'm having the same issue, how can I create a new field and add it to a table via JavaScript?
  • Rob HatchRob Hatch 🛠️ 
    edited September 1, 2016
    Moshe - are you simply trying to display some calculation to the user in the interface?  Or are you trying to actually create a field and store values for that field back into the database?   The first one is pretty trivial (J explains it pretty well above)  the second may take some further doing. 
  • edited December 18, 2014
    I actually got a simple division working with J's code. I'm dividing Total by Volume to get a field called Rate. It's kind of a hack, but the only way I got this to work, is to add the Volume field to the table again, and then overwrite the label to be Rate, and use the Custom Field renderer to display the Math under the "Rate" field. My current issue, is how can I do this in a table, because for some reason it's populating all the Rate fields, with the first Row in the table because I'm only selecting the first row. var field = arguments[0]; var InvoiceLineItems = skuid.model.getModel("InvoiceLineItems"); var currentRecord = InvoiceLineItems.getFirstRow(); var volume = currentRecord.Volume__c; var netTotal = currentRecord.c2g__NetValue__c; var value = netTotal/volume; skuid.ui.fieldRenderers.TEXT[field.mode](field,value.toFixed(5)); So I tried using a jQuery each, like so: var $ = skuid.$; var field = arguments[0]; var SalesInvoice = skuid.model.getModel("SalesInvoice"); $.each(, function(i, row){ var total = row.c2g__InvoiceTotal__c; var volume = row.Volume__c; var value = total/volume skuid.ui.fieldRenderers.TEXT[field.mode](field, value.toFixed(5)); }); but I'm getting NaN displayed in every row, because "value" is null, any ideas?
  • edited December 18, 2014
    Sorry here's my updated code: var $ = skuid.$; var field = arguments[0]; var SalesInvoice = skuid.model.getModel("SalesInvoice"); $.each(, function(i, row){ var total = row.c2g__InvoiceTotal__c; var volume = row.Metered_Volume__c; var value = 0; if (volume !== 0){ value = total/volume; } skuid.ui.fieldRenderers.TEXT[field.mode](field, value.toFixed(5)); }); I'm getting '0' as Rate in every row. Remember that the actual name of the Rate field is also Metered_Volume__c, it's just overwritten with a custom label. Could that be interfering with the math?
  • edited December 18, 2014
    What I'm trying to ask, is, can I use a field renderer, to do some math, and run separately on every row in the table or not?
  • Rob HatchRob Hatch 🛠️ 
    edited September 1, 2016
    Moshe: I appologize.  I thought I answered your question yesterday and now I see no evidence of my reply.  It must have gotten lost in the ether of the internet.  Dang!

    You can absolutely do a field rendered that does math separately on every row in a table. 

    The snippent brings with it two arguments,  0 is the field and 1 is the Value for the field you selected from your model. 
    But the Field argument has a row extension.  This can be traversed to bring in data from other fields that are on that row.  Code might look like this: 
    var field = arguments[0]; var value1 = arguments[1]; var row = field.row; var value2 = row.FieldName;
    For a specific row in the table, value1 will be the field you selected from the model,  and value2 will be the secondary field you want to interact with.  Both values will be specific to the row of the table.   From there it would be trivial to do somthing like
    value = value1/value2;
    skuid.ui.fieldRenderers.DOUBLE[field.mode](field, value.toFixed(2)); 
    Hope this helps. 

  • edited December 18, 2014
    Yes! Thanks.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!