简体   繁体   English

如果没有子行,如何隐藏表标题行,但是如果单击按钮,如何再次显示标题行

[英]How to hide table header row if no child rows, but show header row again if button clicked

I have a table with existing jobs and have a button to add new rows to that table if the user wants to add another job. 我有一个包含现有作业的表,并且有一个按钮,如果用户想添加另一个作业,则可以向该表添加新行。 When the user first clicks on the Add New button, a header appears above the first row. 当用户首次单击“添加新”按钮时,标题将出现在第一行上方。 There is also the option of removing rows and this is where my problem comes in. 还有删除行的选项,这就是我的问题所在。

I want the header row to disappear if the user deletes all the rows that they added, but the problem is getting the header row to show up again when the user clicks the Add New button again. 如果用户删除他们添加的所有行,我希望标题行消失,但是问题是当用户再次单击“添加新”按钮时,标题行再次出现。

I tried setting the display to none, which worked fine, but when the user starts adding rows again, the header doesn't appear. 我尝试将显示设置为无,但效果很好,但是当用户再次开始添加行时,标题不会出现。 Setting visibility to hidden works too, but then you see an empty space where the header should be. 将可见性设置为隐藏也可以,但是随后您会看到标题应位于的空白区域。 I also tried adding an if statement to where the header row is displayed for the first time (setting it to table-row if set to none), but then the Add New didn't work at all. 我还尝试将if语句添加到第一次显示标题行的位置(如果设置为none则将其设置为table-row),但是Add New根本不起作用。

I cannot use jQuery because the page uses MooTools and they conflict. 我无法使用jQuery,因为该页面使用MooTools并且它们发生冲突。 I am very new at MooTools and fairly new to JavaScript, so if someone could point me in the right direction, I would be most grateful. 我是MooTools的新手,JavaScript的新手,所以如果有人能指出正确的方向,我将不胜感激。

$(document.body).addEvent('click:relay(.delete-row)', function (e, el) {
                try {
                    e.preventDefault();
                    .
                    .
                    .
                            if (document.getElementById('addNewJobHeader').style.display !== 'none' && el.get('data-row-id') === '1') {
                                document.getElementById('addNewJobHeader').setStyle('display', 'none');
                            }
                            rewriteeditingselections();
                        } else {
                            .
                            .
                            .
                        }
                    }
                } catch (e) {
                    .
                    .
                    .
                }
            });

            function showHeaderRow() {
                showHeaderRow = function() {};
                Elements.from(connectjobheadertemplate({})).inject($('linkedJobsBody'));
            }

            function addChildJobRow() {
                try {
                    lastrow++;
                    let cl = (lastrow % 2 ? 'odd' : 'even');
                    showHeaderRow();
                    var refNum = '<?php echo $this->MJob->getNewJobRef(232); ?>';
                    Elements.from(connectedjobtemplate({
                        rownum: lastrow,
                        cl: cl,
                        ref: refNum,
                        nysid: '',
                        dinNum: '',
                        warrantNum: ''
                    })).inject($('linkedJobsBody'));
                } catch (e) {
                    .
                    .
                    .
                }
            }

EDIT: Adding HTML: 编辑:添加HTML:

Table for existing jobs: 现有工作表:

        <form method="post" id="linkedJobsForm" action="###" name="linkedJobsForm">
            <table width="100%">
                <thead>
                <tr class="odd">
                    <th align="center">Chk</th>
                    <th>Ref #</th>
                    <th>Pages</th>
                    <th>Order Date</th>
                    <th>NYSID</th>
                    <th>DIN#</th>
                    <th>Warrant Number</th>
                </tr>
                </thead>
                <tbody id="linkedJobsBody">
                                            <tr id="connectjobrow0" class="tc ec" data-row-id="0" data-connected-job="44752">
                            <td width="50" align="center">
                                <input type="checkbox" name="copyid[]" value="44752"  />
                            </td>
                            <td>
                                <a href="###" id="jobReference[0]" name="connectedjob[0][jobReference]"title="###">###</a>                                </td>
                            <td>
                                <input style="background-color: transparent; border: none;" type="text"
                                    data-row-id="0" id="pages[0]"
                                    name="connectedjob[0][pages]" onkeyup="setPages(this.value, this.id)"
                                    value="53" class="pages" size="3"/>
                            </td>
                            <td>
                                <input style="background-color: transparent; border: none;" type="text"
                                    data-row-id="0" id="date[0]"
                                       name="connectedjob[0][orderDate]" onblur="reformatDate(this.value, this.id)"
                                       value="Dec 16, 2015" class="datefield orderDate" size="10">
                            </td>
                            <td>
                                <input style="background-color: transparent; border: none;" type="text"
                                    data-row-id="0" id="pages[0]"
                                    name="connectedjob[0][nysid]" class="nysid" onkeyup="setNYSId(this.value, this.id)"
                                       data-row-id="0" id="nysid[0]"
                                       name="connectedjob[0][nysid]" class="nysid"
                                    value="11991495H">
                            </td>
                            <td>
                                <input style="background-color: transparent; border: none;" type="text"
                                    data-row-id="0" id="pages[0]"
                                    name="connectedjob[0][dinNum]" class="dinNum" onkeyup="setDinNum(this.value, this.id)"
                                       data-row-id="0" id="dinNum[0]"
                                       name="connectedjob[0][dinNum]" class="dinNum"
                                    value="13R2708">
                            </td>
                            <td>
                                <input style="background-color: transparent; border: none;" type="text" onkeyup="setWarrantNum(this.value, this.id)"
                                    data-row-id="0" id="pages[0]"
                                    name="connectedjob[0][warrantNum]" class="warrantNum"
                                    value="0739791">
                            </td>
                        </tr>
                   </tbody>
            </table>

Buttons: 纽扣:

            <div style="float: right;"><button id="add-new-row" style="text-align:center;margin-top:5px;width:85px;" class="boxbutton">Add New</button>&nbsp;<button style="text-align:center;margin-top: 5px; width: 60px;" id="save" class="boxbutton">Save</button></div><br>            </form>

Templates: 范本:

        <script id="connectjobheadertemplate" type="text/plain">
            <table>
                <tr class="odd even" id="addNewJobHeader" style="display:table-row;">
                    <td colspan="7">
                        <table width="100%" style="border:0px;border-style:hidden;">
                            <thead>
                                <tr>
                                    <th style="width: 15%;">Name</th>
                                    <th style="width: 15%;">Ref #</th>
                                    <th style="width: 5%;">Pages</th>
                                    <th style="width: 15%;">Order Date</th>
                                    <th style="width: 15%;">NYSID</th>
                                    <th style="width: 15%;">DIN#</th>
                                    <th style="width: 15%;">Warrant Number</th>
                                    <th style="width: 4%;"></th>
                                </tr>
                            </thead>
                        </table>
                    </td>
                </tr>
            </table>
        </script>
        <script id="connectedjobtemplate" type="text/template">
            <tr id="childjobrow1" class="odd" data-row-id="1">
                <td colspan="7">
                    <table width="100%" style="border:0px;border-style:hidden;">
                        <tr>
                            <td style="width: 15%;">
                                <input class="childjobid" type="hidden" id="childjobid1" name="childjobid[1][transcribername]" value="0" />
                                <input type="hidden" id="transcriberid[1]" name="childjobid[1][transcriberid]" value="" />
                                <input data-row-id="1" id="assignee[1]" name="childjobid[1][transcribername]" class="" list="transcribers" autocomplete="off" value="" placeholder="Name" size="14" />
                            </td>
                            <td style="width: 15%;">
                                <input data-row-id="1" id="reference[1]" name="childjobid[1][reference]" value="" class="ref" size="14" style="background-color: transparent; border: none;" />
                            </td>
                            <td style="width: 5%;">
                                <input data-row-id="1" id="pages[1]" name="childjobid[1][pages]" value="" class="" style="" size="1"/>
                            </td>
                            <td style="width: 15%;">
                                <input data-row-id="1" id="orderDate[1]" name="childjobid[1][orderDate]" value="" class="" type="date" size="14" />
                            </td>
                            <td style="width: 15%;">
                                <input data-row-id="1" id="nysid[1]" name="childjobid[1][nysid]" value="" class="" size="14" />
                            </td>
                            <td style="width: 15%;">
                                <input data-row-id="1" id="dinNum[1]" name="childjobid[1][dinNum]" value="" class="" size="14" />
                            </td>
                            <td style="width: 15%;">
                                <input data-row-id="1" id="warrantNum[1]" name="childjobid[1][warrantNum]" value="" class="" size="14" />
                            </td>
                            <td style="width: 5%;align:center;">
                                <button data-row-id="1" id="0" class="delete-row" style="text-align:center;margin-top: 5px;width:30px;color: #fff;background-color: #b9534f !important;padding: 5px 10px;font-size: 12px;line-height: 1.5;border-style: none;">X</button>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </script>

What I did was to count the number of rows in the dynamically created table and set the display equal to none if there was one row in the table (the header row). 我要做的是计算动态创建的表中的行数,如果表中有一行(标题行),则将显示设置为无。 That made the header row disappear. 这使标题行消失了。

$(document.body).addEvent('click:relay(.delete-row)', function (e, el) {
                try {
                    e.preventDefault();
                    var childjobid = parseInt(el.id);
                    var rowid = el.get('data-row-id');
                    if (confirm('This will immediately DELETE the current line.\nAre you sure?')) {
                        if (childjobid === 0) {
                            $('childjobrow' + el.get('data-row-id')).remove();
                            var rowsInTable = document.getElementById('newChildJobTable').rows.length;
                            if (rowsInTable === 1) document.getElementById('addNewJobHeader').style.display = 'none';
                            if ($('addNewJobHeader').style.display === 'none') {
                                $('newChildJobTable').setStyle('display', 'none');
                            }
                            rewriteeditingselections();
                        } else {
                            .
                            .
                            .
                        }
                    }
                } catch (e) {
                    .
                    .
                }
            });

Then, in the function to add a new row, I checked if the value of the header row was set to none and, if so, set it to 'table-row'. 然后,在添加新行的函数中,我检查了标题行的值是否设置为none,如果是,则将其设置为“ table-row”。

function addChildJobRow() {
                try {
                    lastrow++;
                    let cl = (lastrow % 2 ? 'odd' : 'even');
                    showHeaderRow();
                    if (document.defaultView.getComputedStyle(document.getElementById('addNewJobHeader'), '').getPropertyValue("display") === 'none') {
                        $('addNewJobHeader').style.display = 'table-row';
                    }
                    var refNum = '<?php echo $this->MJob->getNewJobRef(232); ?>';
                    Elements.from(connectedjobtemplate({
                        rownum: lastrow,
                        cl: cl,
                        ref: refNum,
                        nysid: '',
                        dinNum: '',
                        warrantNum: ''
                    })).inject($('newChildJobTable'));
                    $('newChildJobTable').setStyle('display', 'table');
                } catch (e) {
                    .
                    .
                    .
                }
            }

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

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