0

I follow the instruction for adding new table’s row from: Add table row in jQuery

A new row was successfully added but it didn’t get the other rows’ feature (highlight, draggable,…).

The table:

<table id="pubTab">
            <thead>
            <tr>
                <td align="center">col1</td>
                <td align="center">col2</td>
                <td align="center">col3</td>
                <td align="center">col4</td>
            </tr>
            </thead>
            <tbody>
            <tr id="1">
                <td>+++<input type="text" size="40"/></td>
                <td><input type="text" size="10"/></td>
                <td><input type="text" size="50"/></td>
                <td><input type="text" size="10" /></td>
            </tr>
            <tr id="2">
                <td>+++<input type="text" size="40"/></td>
                <td><input type="text" size="10"/></td>
                <td><input type="text" size="50"/></td>
                <td><input type="text" size="10"/></td>
            </tr>
            </tbody>
        </table>
<input id="addPubTab" type="button" value="ADD" style="background-color:green; width: 170px"/>

The jQuery script:

<script type="text/javascript">
        $(document).ready(function(){
            $("#addPubTab").click(function(){
                var id = "3";
                var value = "<tr id=\""+id+"\">"+
                            "<td>+++<input type=\"text\" size=\"40\" name=\"collection_name\""+id+"/></td>"+
                            "<td><input type=\"text\" size=\"10\" name=\"service_name\""+id+"/></td>"+
                            "<td><input type=\"text\" size=\"50\" name=\"out_fname\""+id+"/></td>"+
                            "<td><input type=\"text\" size=\"10\" name=\"service_id\""+id+"/></td>"+
                            "</tr>";
                $('#pubTab tbody').append(value);
            });
        });
    </script>

The problem Problem: It added the new row but I cannot drag it (up/down) as I drag the other tow (there are no curser for dragging).

Inspecting elements resolve with:

<table id="pubTab">
<thead>_</thead>
<tbody>
    <tr id=”1” style=”cursor: move; “ class>_</tr>
    <tr id=”2” style=”cursor: move; “ class>_</tr>
    <tr id=”3”>_</tr>
</tbody>

Please note that I am using the jQuery script (dragging/sorting):

$(function() {  $("#pubTab:not(thead)").tableDnD();   });

Please assist.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mike
  • 1,007
  • 2
  • 16
  • 33
  • I think u might have to destroy the initial draggable, and then reassign it again, to update the new children – Val Feb 24 '11 at 11:31
  • 1
    just after `$('#pubTab tbody').append(value);` btw :) – Val Feb 24 '11 at 11:31

3 Answers3

4

The tableDnD plugin has a method which may have been added since this question was originally posted.

.tableDnDUpdate()

As the documentation states

cause the table to update its rows so the drag and drop functionality works if, for example, you’ve added a row.

awj
  • 7,482
  • 10
  • 66
  • 120
0

Try reinitialization of tableDnD plugin after you insert new row.

Other solution would be to clone one existing row and change data in it.

Matej Baćo
  • 1,312
  • 2
  • 10
  • 12
0

Your new table rows aren't draggable because you haven't integrated them into your drag and drop object. They remain static HTML while your original rows are all fancy and interactive because you called .tableDnD() on them. See if calling .tableDnD on your new rows works, or update .tableDnD to use .live(). From the .live() page:

Description: Attach a handler to the event for all elements which match the current selector, now and in the future.

The .live() documentation page describes precisely the issue you are having, well worth a look!

Just Jake
  • 4,698
  • 4
  • 28
  • 33