开发者

Editing an HTML table cell

I have a table with a few rows... I want to be able to select a row and click on modify and I should be able to make all the cells of that row editable...

How would开发者_如何学Go I make a cell editable in Javascript? And is it better to use Jquery?


There's no need to do your own code, a plugin for jQuery for this very purpose exists already. Try jEditable, it can do exactly what you need.

Their demo page has some nice examples:

http://www.appelsiini.net/projects/jeditable/default.html


Setcontent-editable property of each element you want to edit.
http://html5demos.com/contenteditable


Here's a quick concept I just worked up for you:

$(function(){
  $("button[name='doModify']").click(function(){
    // disable out modify button
    $(this).attr("disabled","disabled");
    // enable our save button
    $("button[name='save']").removeAttr("disabled");
    // cycle through each row having marked for modification
    $(":checkbox[name='modify']:checked").each(function(){
      $(this).closest("tr").find("td:gt(0)").each(function(){
        // convert each cell into an editable region
        $(this).wrapInner("<textarea name='"+$(this).attr("rel")+"'></textarea>");
      });
    });
  });
});

--

<table border="1" cellspacing="1" cellpadding="5">
  <tbody>
    <tr>
      <td><input type="checkbox" name="modify" /></td>
      <td rel="username[]">jon.doe</td>
      <td rel="information[]">This is my bio.</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="modify" /></td>
      <td rel="username[]">jonathan.sampson</td>
      <td rel="information[]">This is my bio.</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="modify" /></td>
      <td rel="username[]">yellow.05</td>
      <td rel="information[]">This is my bio.</td>
    </tr>
    <tr>
      <td colspan="3" align="right">
        <button name="doModify">Modify</button> 
        <button name="save" disabled="disabled">Save</button>
      </td>
    </tr>
  </tbody>
</table>


You can insert textboxes inside each cell and set the values to be that of the table cell.

Something like this

$(function(){
        $("#tbl1 tr").click ( function(){
            if ( !$(this).hasClass('clicked') )
            {
                $(this).children('td').each ( function() {
                    var cellValue = $(this).text();
                    $(this).text('').append ( "<input type='text' value='" + cellValue + "' />" );
                });

                $(this).addClass('clicked');
            }
        });
    });
<table id="tbl1">
            <tr>
                <td>1</td>
                <td>4</td>
                <td>3</td>
            </tr>
            <tr>
                <td>4</td>
                <td>5</td>
                <td>6</td>
            </tr>
        </table>

You can then place an update button and fetch the values from the textboxes and update.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜