简体   繁体   English

如何用PHP隐藏带有空单元格的表行

[英]How to hide table row with empty cell with php

I have htmltopdf code from developers and there is a part with a table that consists of 'Question' and 'Answer', so if the answer is empty I need to hide the row. 我有来自开发人员的htmltopdf代码,并且其中的一部分表包含“问题”和“答案”,因此,如果答案为空,则需要隐藏该行。

Here is a part of my code: 这是我的代码的一部分:

$pdfHTML = '<table border="1"><tbody>';

        foreach($arAnswer as $items){

            $pdfHTML .= '<tr>';
                $pdfHTML .= '<td>';
                    $pdfHTML .= htmlspecialcharsEx($items['0']['TITLE']);
                $pdfHTML .= '</td>';
                $pdfHTML .= '<td>';
                    foreach($items as $item){
                        if($item['USER_TEXT']){
                            $pdfHTML .= htmlspecialcharsEx($item['USER_TEXT']);
                        }else if( ($answerText = trim($item['ANSWER_TEXT'])) ){
                            $pdfHTML .= htmlspecialcharsEx($answerText);
                        }
                        $pdfHTML .= '<br>';
                    }
                $pdfHTML .= '</td>';
            $pdfHTML .= '</tr>';
        }

        $pdfHTML .= '</tbody></table>';

You should just be able to add a empty check, for example: 您应该只能够添加一个empty支票,例如:

foreach ($arAnswer as $items) {
    $pdfHTML .= '<tr>';
    $pdfHTML .= '<td>';
    $pdfHTML .= htmlspecialcharsEx($items['0']['TITLE']);
    $pdfHTML .= '</td>';
    $pdfHTML .= '<td>';
    foreach ($items as $item) {
        if ($item['USER_TEXT']) {
            $pdfHTML .= htmlspecialcharsEx($item['USER_TEXT']);
        }
        if (!empty($item['ANSWER_TEXT'])) {
            $pdfHTML .= htmlspecialcharsEx($item['ANSWER_TEXT']);
        }
        $pdfHTML .= '<br>';
    }
    $pdfHTML .= '</td>';
    $pdfHTML .= '</tr>';
}

$pdfHTML .= '</tbody></table>';

Try this. 尝试这个。 You can check all items inside answers and then decide to add or not new line 您可以检查答案中的所有项目,然后决定是否添加新行

$pdfHTML = '<table border="1"><tbody>';

foreach ($arAnswer as $items) {
    $isAnswer = false;
    foreach ($items as $item) {
        if (trim($item['ANSWER_TEXT'])) {
            $isAnswer = true;
            break;
        }
    }
    if ($isAnswer) {
        $pdfHTML .= '<tr>';
        $pdfHTML .= '<td>';
        $pdfHTML .= htmlspecialcharsEx($items['0']['TITLE']);
        $pdfHTML .= '</td>';
        $pdfHTML .= '<td>';
        foreach ($items as $item) {
            if ($item['USER_TEXT']){
                $pdfHTML .= htmlspecialcharsEx($item['USER_TEXT']);
            }
            if (!empty($item['ANSWER_TEXT'])) {
                $pdfHTML .= htmlspecialcharsEx($item['ANSWER_TEXT']);
            }
            $pdfHTML .= '<br>';
        }
        $pdfHTML .= '</td>';
        $pdfHTML .= '</tr>';
    }

}

$pdfHTML .= '</tbody></table>';

You can do this add class , make it hide using jquery. 您可以执行此添加类,使其使用jquery隐藏。 like below example : 像下面的例子:

//PHP :
$pdfHTML = '';

foreach ($arAnswer as $items) {
    $pdfHTML .= '<tr>';
    $pdfHTML .= '<td>';
    $pdfHTML .= htmlspecialcharsEx($items['0']['TITLE']);
    $pdfHTML .= '</td>';
    $pdfHTML .= '<td>';
    foreach ($items as $item) {
        if ($item['USER_TEXT']) {
            $pdfHTML .= htmlspecialcharsEx($item['USER_TEXT']);
        }
        if (!empty($item['ANSWER_TEXT'])) {
            $pdfHTML .= htmlspecialcharsEx($item['ANSWER_TEXT']);
        }
//add empty div with class with to hide row

        if (empty($item['ANSWER_TEXT'])) {
            $pdfHTML .= '<div class="hide_table_row"></div>';
        }
        $pdfHTML .= '<br>';
    }
    $pdfHTML .= '</td>';
    $pdfHTML .= '</tr>';
}

$pdfHTML .= '</tbody></table>';

//jquery // jquery

$(document).ready(function(){
//hide row (parent tr)
  $(".hide_table_row").closest('tr').hide();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM