简体   繁体   中英

How to hide and show elements in table row to allow in-line editing

I'm trying to implement in-line editing of table data that is generated via php.

Here's the php code that generates a row

<tr class="zipCodeRow">
<td>
    <a class="hideOnEdit zipCodeFormSubmit" name="' . $rowZipCodes['id'] . '" href="#">Edit</a>
    <input type="submit" class="hideOnLoad submitEditZipCodeRow zipCodeFormSubmit" id="submitEditZipCodeRow-' . $rowZipCodes['id'] . '" value="Save Edits" /><br />
    <input type="submit" class="hideOnLoad cancelEditZipCodeRow zipCodeFormSubmit" value="Cancel Edit" />
</td>
<td>
    <input type="checkbox" class="chkDeleteZipCodeRow zipCodeFormSubmit" name="deleteZipCodeRow[]" value"' . $rowZipCodes['id'] . '" />
</td>
<td>
    <label class="hideOnEdit">' . $rowZipCodes['state'] . '</label>
    <input type="text" class="hideOnLoad" size="20" maxlength="50" value="' . $rowZipCodes['state'] . '"
</td>
<td>
    <label class="hideOnEdit">' . $rowZipCodes['county'] . '</label>
    <input type="text" class="hideOnLoad" size="15" maxlength="50" value="' . $rowZipCodes['county'] . '"
</td>
<td>
    <label class="hideOnEdit">' . $rowZipCodes['city'] . '</label>
    <input type="text" class="hideOnLoad" size="20" maxlength="50" value="' . $rowZipCodes['city'] . '"
</td>
<td>
    <label class="hideOnEdit">' . $rowZipCodes['zipCodes'] . '</label>
    <textarea class="hideOnLoad">' . $rowZipCodes['zipCodes'] . '</textarea>
</td>

You can see by the classes that I want certain elements to hide on page load and those elements will be shown on Edit. This line of jQuery properly hides the "hideOnLoad" classes elements:

$('.hideOnLoad').hide();

Then, I have javascript to detect when the Edit link is clicked and I call this function:

function EditTableRow(linkClicked) {

var id = linkClicked.attr('name');

$('a.hideOnEdit').fadeOut(500);
$('input.hideOnEdit').fadeOut(500);
$('textarea.hideOnEdit').fadeOut(500);

$('input.hideOnLoad').fadeIn(500);
$('textarea.hideOnLoad').fadeIn(500);
}

I knew as soon as I wrote the function that it didn't seem right, but I'm not sure how to implement what I need to do.

I'd appreciate anyone's suggestions on how to make this work as described!

First thing's first, you don't want to call $('.hideOnLoad').hide(); when the page loads. You run the risk of the user seeing the fields for a brief period. Instead add the following CSS. This is more likely to take effect before the user sees anything (but not guaranteed unless it's inline).

.hideOnLoad { display: none; }

As for your EditTableRow method, you're running it on everything rather than within the scope of the given row. Try something like this:

function EditTableRow(linkClicked) {
    var row = linkClicked.closest('tr');

    tr.find('.hideOnEdit').fadeOut(500);
    tr.find('.hideOnLoad').fadeIn(500);
}
HTML
<td>
    <label>value</label>
    <input id="val1" type="text" value="value">
</td>

JS
$('td').on('click', function(){
     $('input',$(this)).show();
     $('label',$(this)).hide();
});

$('input').on('blur', function(){
    var val = $(this).val();
    $(this).previous('label').html(val).show();
    $(this).hide();
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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