3

Ok so I have a Server side datatable reading JSON format with child rows. How can I have If / Else If / Switch statements in between?

<script type="text/javascript">

function format ( d ) {
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">',

  '<tbody>'+
    '<tr>'+
        '<td class="second-td-box">'+
           '<p class="at-a-glance">Basic information:</p>'+
           '<p><span class="subhead">Name: </span>'+d.RES_GUEST_FIRSTNAME+'</p>'+
        '</td>'+
    '</tr>'+ 
  '</tbody>'+
'</table>'+
</script>

I have tried:

    <script type="text/javascript">

function format ( d ) {
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">',

  '<tbody>'+
    '<tr>'+
        '<td class="second-td-box">'+ ''
            if (d.RES_GUEST_FIRSTNAME == "Alex") {
           '<p class="at-a-glance">Basic information:</p>'+
           '<p><span class="subhead">Name: </span>'+d.RES_GUEST_FIRSTNAME+'</p>'+
            }
        '</td>'+
    '</tr>'+ 
  '</tbody>'+
 '</table>'+
 </script>

Solved the problem using the following code:

 ((d.BOOKING_SOURCE_ID == 73844)?
        '<td class="second-td-box">'+
           '<p class="at-a-glance">Airbnb information:</p>'+
           '<p><span class="subhead">Confirmation: </span>'+d.CONFIRMATION+'</p>'+
           '<p><span class="subhead">Agency Income: </span>'+d.AIRBNB_AGENCY_INCOME+'</p>'+
           '<p><span class="subhead">Airbnb Income: </span>'+d.AIRBNB_INCOME+'</p>'+
           '<p><span class="subhead">Airbnb fees: </span>'+d.AIRBNB_FEES+'</p>'+
           '<p><span class="subhead">Airbnb per night: </span>'+d.AIRBNB_PER_NIGHT+'</p>'+ 
           '<span id="chart_div"></span>'
          :"")+
        '</td>'+


     ((d.BOOKING_SOURCE_ID == 73858)?
           '<td class="second-td-box">'+
           '<p class="at-a-glance">Booking.com information:</p>'+
           '<p><span class="subhead">Booking date: </span>'+d.BOOKING_DATE+'</p>'+
           '<p><span class="subhead">Booking currency: </span>'+d.BOOKING_CURRENCY+'</p>'+
           '<p><span class="subhead">Booking total: </span>'+d.BOOKING_TOTAL+'</p>'+
           '<p><span class="subhead">Booking comission: </span>'+d.BOOKING_COMISSION+'</p>'+
           '<p><span class="subhead">Booking policy: </span>'+d.BOOKING_POLICY+'</p>'+ 
           '<span id="chart_div"></span>'
            :"")+
        '</td>'+
        '</tr>'+

Seems that I can only "Print" using the Conditional operator.

5 Answers 5

1

You can try the conditional operator ( https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Conditional_Operator ):

<script type="text/javascript">

function format ( d ) {
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">',

  '<tbody>'+
    '<tr>'+
        '<td class="second-td-box">'+ '' +
           ((d.RES_GUEST_FIRSTNAME == "Alex")?
           '<p class="at-a-glance">Basic information:</p>'+
           '<p><span class="subhead">Name:</span>'+d.RES_GUEST_FIRSTNAME+'</p>'
            :"")+
        '</td>'+
    '</tr>'+ 
  '</tbody>'+
 '</table>'+
 </script>
Sign up to request clarification or add additional context in comments.

2 Comments

Solved with your help. Thanks!
If you want to do if/then/else, you can nest the conditional operator like this: (a=='1'?'text for 1':(a=='2'?'Text for 2':''))
1

If you are using `` this could be helpful for you.

`<div class="user-email">
   ${response[i].email ? '<i class="fas fa-envelope"></i>': ""}
   ${response[i].email}
</div>
<div class="user-phone">
    ${response[i].phone ? '<i class="fas fa-phone-alt"></i>': ""}
     ${response[i].phone}
</div>`

Comments

0

You cannot use if statements while concatenating with. You may use conditional operator something like this:

'string' + d.RES_GUEST_FIRSTNAME ? 'add string' : '' + 'another string';

Comments

0
return 'string ='+(d.RES_GUEST_FIRSTNAME == "Alex")?'yes':'no';

Usage:

(condition)? [value if true] : [value if false];

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

Comments

0

Essentially you are returning a string, so you can build this up bit by bit. I would highlight that returning HTML as string from JavaScript is not a recommended way to inject HTML.

var returnString = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">';

returnString += '<tbody>';
returnString += '<tr>';

and so on..

Then return the string

 return returnString;

So if you want to add in a if/ if else

then you can do something like

if (d.RES_GUEST_FIRSTNAME == "Alex") {
   returnString += '<p class="at-a-glance">Basic information:</p>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.