Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make editable table using bootstrap and jQuery only?

This is my bootstrap table.I want to insert data into table using jQuery only.On clicking a particular cell,a textbox should be open to enter data.I don't want to use any other plugin.

       <table class="table table-bordered">
         <thead class="mbhead">
           <tr class="mbrow">
             <th>A</th>
             <th>B</th>
             <th>C</th>
             <th>D</th>
           </tr>
        </thead>
        <tbody>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
             <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
           <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>          
         </tbody>
       </table>

help me.thanx.

like image 274
Amrendra Avatar asked Mar 01 '13 05:03

Amrendra


1 Answers

You basically want to add an event to the user clicking on a table row. In jQuery you can add that event like this

$("table.table tr td").bind("click", dataClick);

In the dataClick function you wan to make the row editable. You can do so like this.

function dataClick(e) {
    console.log(e);
    if (e.currentTarget.innerHTML != "") return;
    if(e.currentTarget.contentEditable != null){
        $(e.currentTarget).attr("contentEditable",true);
    }
    else{
        $(e.currentTarget).append("<input type='text'>");
    }    
}

I have a sample here, http://jsfiddle.net/JPVUk/4/

Hope this helps

like image 169
sissonb Avatar answered Sep 22 '22 11:09

sissonb