在Javascript HTML

时间:2015-06-24 10:51:21

标签: javascript html datatables

好的,我有一个服务器端数据表,读取带子行的JSON格式。 如何在中间使用If / Else If / Switch语句?

<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>

我试过了:

    <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>

使用以下代码解决了问题:

 ((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>'+

似乎我只能&#34;打印&#34;使用条件运算符。

4 个答案:

答案 0 :(得分:1)

您可以尝试条件运算符(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>

答案 1 :(得分:0)

连接时不能使用if语句。您可以使用conditional operator这样的内容:

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

答案 2 :(得分:0)

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

用法:

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

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

答案 3 :(得分:0)

基本上你要返回一个字符串,所以你可以一点一点地构建它。我要强调的是,从JavaScript返回HTML作为字符串不是推荐的注入HTML的方法。

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

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

依旧......

然后返回字符串

 return returnString;

因此,如果您想添加if / if else

然后你可以做类似

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