locked
Hide Rows in HTML or CSS if one of the rows is spaces RRS feed

  • Question

  • User1203305613 posted

    Hi,

    How do we conditionally hide rows in HTML or CSS, if one of the value is space.
    For example in the attached screenshot we need to hide Address2 row if the Address 2 contains spaces.(one of the td has space hide the tr)

    <tr>
    <td>
    Address2
    </td>
    <td>
    [Address2]
    </td>
    </tr>

    Attaching the screenshot we need to hide all rows enclosed in red boxes.

    ==


    <table class="mce-item-table" width="242" style="height: 78px;" data-mce-style="height: 78px;">
    <tbody>
    <tr>
    <td>
    Address1
    </td>
    <td>
    [Address1]
    </td>
    </tr>
    <tr>
    <td>
    Address2
    </td>
    <td>
    [Address2]
    </td>
    </tr>
    <tr>
    <td>
    City
    </td>
    <td>
    [City]
    </td>
    </tr>
    <tr>
    <td>
    Zip
    </td>
    <td>
    [Zip Code]
    </td>
    </tr>
    </tbody>
    </table>

    ==

    Thanks,
    Kalyan

     

    Monday, July 22, 2019 6:58 AM

Answers

  • User665608656 posted

    Hi KALYANA,

    According to your description, you can achieve this function by using jquery with css.

    First, you should find this table's tr and loop them to find td in each tr, create a variable isHide which value is false before you loop td in each tr, then in the loop of td ,if the text of td contains space, set isHide to true. 

    After you complete loop td in each tr, judge the isHide is true or false, if it is true, then add a class named 'rowhide' to current tr to hide it.

    The rowhide class is written in css which controls tr to hide.

    It should be noted that the text in each td may has some line breaks behind them, you need to remove them before judge it contains any spaces.

    For more details, you could refer to the following code:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-3.3.1.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#table1").find('tr').each(function () { 
                    var isHide = false;
                    $(this).find('td').each(function () {
                        var message = $(this).text().replace(/(\r\n|\n|\r)/gm, "").trim();
                        if (message.indexOf(' ') !== -1) {
                            isHide = true;
                        }
                    });
                    if (isHide) {
                        $(this).addClass("rowhide");
                    }
                });
    
            })
        </script>
        <style type="text/css">
            .rowhide {
                display: none;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <table id="table1" class="mce-item-table" style="height: 78px; width: 242px;" data-mce-style="height: 78px;">
                <tbody>
                    <tr>
                        <td>Address1
                        </td>
                        <td>[Address1]
                        </td>
                    </tr>
                    <tr>
                        <td>Address2
                        </td>
                        <td>[Address 2]
                        </td>
                    </tr>
                    <tr>
                        <td>City
                        </td>
                        <td>[City]
                        </td>
                    </tr>
                    <tr>
                        <td>Zip
                        </td>
                        <td>[Zip Code]
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </body>
    </html>
    

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 23, 2019 2:50 AM
  • User-474980206 posted

    this should be done server side. suppress the row, if data is empty.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 23, 2019 9:52 PM

All replies

  • User665608656 posted

    Hi KALYANA,

    According to your description, you can achieve this function by using jquery with css.

    First, you should find this table's tr and loop them to find td in each tr, create a variable isHide which value is false before you loop td in each tr, then in the loop of td ,if the text of td contains space, set isHide to true. 

    After you complete loop td in each tr, judge the isHide is true or false, if it is true, then add a class named 'rowhide' to current tr to hide it.

    The rowhide class is written in css which controls tr to hide.

    It should be noted that the text in each td may has some line breaks behind them, you need to remove them before judge it contains any spaces.

    For more details, you could refer to the following code:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-3.3.1.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#table1").find('tr').each(function () { 
                    var isHide = false;
                    $(this).find('td').each(function () {
                        var message = $(this).text().replace(/(\r\n|\n|\r)/gm, "").trim();
                        if (message.indexOf(' ') !== -1) {
                            isHide = true;
                        }
                    });
                    if (isHide) {
                        $(this).addClass("rowhide");
                    }
                });
    
            })
        </script>
        <style type="text/css">
            .rowhide {
                display: none;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <table id="table1" class="mce-item-table" style="height: 78px; width: 242px;" data-mce-style="height: 78px;">
                <tbody>
                    <tr>
                        <td>Address1
                        </td>
                        <td>[Address1]
                        </td>
                    </tr>
                    <tr>
                        <td>Address2
                        </td>
                        <td>[Address 2]
                        </td>
                    </tr>
                    <tr>
                        <td>City
                        </td>
                        <td>[City]
                        </td>
                    </tr>
                    <tr>
                        <td>Zip
                        </td>
                        <td>[Zip Code]
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </body>
    </html>
    

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 23, 2019 2:50 AM
  • User-474980206 posted

    this should be done server side. suppress the row, if data is empty.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 23, 2019 9:52 PM