add a Row created callback to my DataTable

add a Row created callback to my DataTable

alfasino9alfasino9 Posts: 31Questions: 5Answers: 0

Hello,

I still investigate this incredible tool,

Now i want the Row created callback in my DataTable.

I have see this example -> https://datatables.net/release-datatables/examples/advanced_init/row_callback.html

In my datatable (http://comparador.masqueunaweb.com/buscador3.php) i wan't to add this to the "Precio" column.

The objective is to compaire the column "Precio" with the others "Precio Comp1", "Precio Comp2" and "Precio Comp3". (if Precio is < than the others 3 = GREEN, if Precio is > than the others 3 = RED)

Can i do that?

Can you provide me some ideas to get that started?

Thanks and best regards!

This question has accepted answers - jump to:

Answers

  • alfasino9alfasino9 Posts: 31Questions: 5Answers: 0

    up!

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    Answer ✓

    I built a test case for you to start with.
    http://live.datatables.net/rehaxuge/1/edit

    Took the data from your page and started the example. Wasn't sure exactly how you want the comparison so I set the "Precio" column to Green if precio < preciocom1 or precio < preciocom2 or precio < preciocom3. Maybe you want "and" instead. I didn't check for the Red condition. Will let you work out the rest :smile:

    Kevin

  • alfasino9alfasino9 Posts: 31Questions: 5Answers: 0
    edited October 2017

    That's really awesome @kthorngren , thank you so much.

    I have tried to do a if else if for the red highlight, but it doesn't work.

    `createdRow: function ( row, data, index ) {
    var precio = parseInt(data.precio); //convert to integer
    if (precio < parseInt(data.precioComp1) || precio < parseInt(data.precioComp2) || precio < parseInt(data.precioComp3) ) {
    { $('td', row).eq(3).css('color', 'Green');

              } elseif ( precio > parseInt(data.precioComp1) || precio > parseInt(data.precioComp2) || precio > parseInt(data.precioComp3) ) {
                $('td', row).eq(3).css('color', 'Red');
              } 
    
             }
            }`
    

    I'm pretty sure the syntax is not correctly.

    Can you help me to fix that also? :blush: Will be awesome that!

    Best regards

  • alfasino9alfasino9 Posts: 31Questions: 5Answers: 0

    Hello @kthorngren , i have just see in the example you have give me -> http://live.datatables.net/rehaxuge/1/edit

    Why the 9th row (Producto C...) have the green? Because the price of this one is not lower than the others. (14.70 > 9.84 < 188.95 > 42.50 )

    Best regards!!

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    Answer ✓

    I'm pretty sure the syntax is not correctly.

    This page shows how to use if else:
    https://www.w3schools.com/js/js_if_else.asp

    Why the 9th row (Producto C...) have the green? Because the price of this one is not lower than the others. (14.70 > 9.84 < 188.95 > 42.50 )

    I mentioned that I wasn't sure exactly what you wanted and used if/or and that you may want to change to if/and. If you change the or to and then you should get the result you want.

    Kevin

  • alfasino9alfasino9 Posts: 31Questions: 5Answers: 0

    Works!

    Thank you so much @kthorngren

This discussion has been closed.