4

Extract color or icon from styled field/var

I needed to extract the icon and colour from a styled value. The color() and icon() functions work for the choice field, but not for styled.

When I looked up how to do this, I saw that styled is a JSON structure. If you write this code in the Ninox console :

var s := styled( "hello", "yellow", "blue", "rainbow");
debugValueInfo(s);

 it returns: styled({"text": "hello", "color": "blue", "background": "yellow", "icon": "rainbow"})

So, if you use the item() function, you can extract all the values: 

item(Styled, "color")

return : blue

but the returned value type is always styled: debugValueInfo(item(s, "color")) ; return styled("blue")

To retrieve a textual value, use the string() function because the text() function will use a formatting function that will always return the text of a styled variable. string() function return a text representation of the value as it is:

var s := styled( "hello", "yellow", "#125", "rainbow");
string(item(s, "color"));

return : blue

The debugger can help you see the type and content of variables.

I'm not sure if this tip is useful to anyone, but you never know 🤗.

11 replies

null
    • Database App Developer
    • vermau81
    • 4 mths ago
    • Reported - view

     

    Hello, I tried your above code in the console, but to my surprise, there is no result when I press the Run button. I have the macOS Ninox app.

    Is there problem with console in the macOS app?

    Below is my code that I tried in the script on the field:

    let f := formatJSON('Order Charges');
    let p := parseJSON(f);
    let allLineItemCharges := number(item(p, "text"));
    let orderTotalAmount := allLineItemCharges + 'Delivery Charges' + Adjustment;
    orderTotalAmount
    

    In the end the value returned for orderTotalAmount is either "null" or zero 'cuz the textbox shows $0.00 instead of $90.00 and the balance due from the customer is -$90.00 although it should be $0.00 as the customer has already made full payment of $90.00 already.

    There is another problem -- I tried to run a sample code in the console also, but nothing happens in the console. There is no output in the output window. I keep on clicking the "Run" button, but nothing happens.

     

      • Ninox developper
      • Jacques_TUR
      • 4 mths ago
      • Reported - view

       In the image, I notice that the last line of your code is `debug()`. Therefore, the result displayed comes from this function. Since `debug()` does not return anything, nothing is shown. To display the desired result, you should replace `debug()` with `orderTotalAmount` at the end of your code. 

      Of course, if you use the Ninext extension, you can view the result of the `debug()` function in the Ninext/Debug Tracer window.

      • Ninox developper
      • Jacques_TUR
      • 4 mths ago
      • Reported - view

       see Ninext Project / Debug tracer : https://forum.ninox.com/t/p8h78zv?r=60h06lj

      • Database App Developer
      • vermau81
      • 4 mths ago
      • Reported - view

       Thank you for the prompt reply.

      That thing worked in the console as advised by you. But my real code on the SaleOrder form is not working.

      let f := formatJSON('Order Charges');
      let p := parseJSON(f);
      let allLineItemCharges := number(item(p, "text"));
      let orderTotalAmount := allLineItemCharges + 'Delivery Charges' + Adjustment;
      orderTotalAmount
      

      The above code is implemented inside a script in OrderTotal formula field.

      • Ninox developper
      • Jacques_TUR
      • 4 mths ago
      • Reported - view

       What type of field is 'Order Charges'? Text or Formula? If it's a formula, what is it?

      • Database App Developer
      • vermau81
      • 4 mths ago
      • Reported - view

       It is a formula field. Script in the field is just two lines

      let allLineItemCharges := sum(OrderItems.Subtotal);
      styled(text(allLineItemCharges), "#F7F8FC")
      

      Attached is a sample of my database app.

      • Fred
      • 4 mths ago
      • Reported - view

      see my response in your other post.

      • Ninox developper
      • Jacques_TUR
      • 4 mths ago
      • Reported - view

       I see that you just want to change the colour of the text. Why not do this in the field configuration?
      But if you still want to retrieve the number value from a text field that also contains characters, you can use this formula :

      number(extractx(text('Order Charges'), "(\d+(\,\d+)?)"))
      

      extractx() retrieves only the digits and decimal separators (a comma in this case).

      Another solution is to use a non-visible intermediate formula containing only :sum(OrderItems.Subtotal)
      Then use this value to calculate ‘Order Charges’ and ‘Order Total’.

      • szormpas
      • 4 mths ago
      • Reported - view

        Hi, could we use the sting() function to get only the number, like below?

      string(sum(OrderItems.Subtotal))
      
    • Database App Developer
    • vermau81
    • 4 mths ago
    • Reported - view

    Hello everyone, first of all thank you everyone for all your help and support in this query. This was not a very big matter if Ninox could provide the right way to do the right thing.

    Using styled() for changing the background color of an element and then if this was not enough, it changes the property of the underlying value to a string.

    I wish if Ninox provides a simple .backgroundColor or .bgColor property for elements, there would be no need for all this drama. Also why just format (UI/UX) formula elements, why not text boxes and number boxes

    I tried the regex method also and it was super to extract numbers and currency leaving all the dust behind, however using styled starts a chain reaction between different elements (if you are using the styled values between different elements for some calculations).

    I will stay with multiple callbacks to the child table as that way it is just two lines of code in every formula field and no formatting is required as no styled is used.

    Alas!!

      • Fred
      • 4 mths ago
      • Reported - view

      From experience, if you are using the same formula in many locations, you may want to consider creating another formula field that does the bit of code. Why? Well if you need to change the code then you have to go to many fields to make the changes, versus just doing it in one field.

Content aside

  • 4 Likes
  • 4 mths agoLast active
  • 11Replies
  • 220Views
  • 4 Following