if statement

if statement

raspoteenraspoteen Posts: 16Questions: 8Answers: 0
edited September 2017 in Free community support

Hi, I want to check the Currency for each raw, and I tried this code but it is not working any more

               //Check the Buy Price Dollar Or Dinar
                    if ({data: "buy_currency"} == "1") {
                    return {data: "buy_price", render: $.fn.dataTable.render.number( ',', '.', 0, '$') },
                else return {data: "buy_price", render: $.fn.dataTable.render.number( ',', '.', 0) },
                     }

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    Where is that code executed? Could you give me some context? Are you getting an error shown in your Javascript console?

    Allan

  • raspoteenraspoteen Posts: 16Questions: 8Answers: 0
    edited September 2017

    this code is never executed. it's full with errors
    but i want the right code to add [$] to the price when the buy_currency field is "1"
    Note// my back end in Laravel

  • crwdzrcrwdzr Posts: 31Questions: 5Answers: 6
    edited September 2017

    i had to do something similar before but i didn't use $.fn.dataTable.render for any of my formatting, it looks like youre doing this in your column defines as a render function, will something like this work if so?

    {
        data: 'buy_currency',
        render: function(data, type, row){
            return (row.buy_currency == 1) 
                ? $.fn.dataTable.render.number( ',', '.', 0, '$' )
                : $.fn.dataTable.render.number( ',', '.', 0 )
        },
    }
    
  • raspoteenraspoteen Posts: 16Questions: 8Answers: 0

    @crwdzr your code looks great, but i want to check the currency in the (buy_currency) column then apply the dollar sign to the (buy_price) column ,,, you got me ?

  • crwdzrcrwdzr Posts: 31Questions: 5Answers: 6
    Answer ✓

    oops, my bad. in that case just plop it in the buy_price column define

    {
        data: 'buy_price',
        // add in any other column options you want too
        render: function(data, type, row){
            return (row.buy_currency == 1)
                ? $.fn.dataTable.render.number( ',', '.', 0, '$' )
                : $.fn.dataTable.render.number( ',', '.', 0 )
        },
    }
    

    when you use the render function like this you can compare the data arg (which is the cell's data) to the row arg (which contains the data for the entire row it belongs to), its really helpful once u start getting the hang of it. :)

    like i said though i've never used any $.fn.dataTable.render functions so I dont know if $.fn.dataTable.render.number will work in this context :#

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

    You can apply crwdzr's solution in the buy_price column. The row parameter contains all the data for that row. The columns.render docs explain what the function parameters.

    Kevin

  • raspoteenraspoteen Posts: 16Questions: 8Answers: 0

    crwdzr's code is right, but missing one thing, how to apply this code

    $.fn.dataTable.render.number( ',', '.', 0, '$' )
    

    to the buy_price field ??

  • crwdzrcrwdzr Posts: 31Questions: 5Answers: 6
    Answer ✓

    I was just looking at some examples of that render.number() helper cause this is something i'll start using, to use it inside a render function, just add .display(data) to the end of each render.number()

    return (row.buy_currency == 1)
                ? $.fn.dataTable.render.number( ',', '.', 0, '$' ).display(data) 
                : $.fn.dataTable.render.number( ',', '.', 0 ).display(data) 
    
  • raspoteenraspoteen Posts: 16Questions: 8Answers: 0

    @crwdzr it's Working, and It's Great, thanks so much
    @kthorngren and thank you too.

This discussion has been closed.