views:

594

answers:

1

Hi,

I am trying to sort rows of a table, and put them as 'child' elements of a table row

I found this: http://code.google.com/p/nestedsortables/ this works with ul li lists, but i want to build it for a table.

<table>
  <thead>
    <th>tablehead</th>
  </thead>
  <tbody>
    <tr><td>somevalue</td></tr>
    <tr><td>somevalue2</td></tr>
  </tbody>
</table>

so yeah you can use jquery.sortable() and sort the rows, but i want 'somevalue' to be become a child element of 'somevalue2' if you drag 'somevalue' over 'somevalue2'

I don't know if it is posible with a table.

can anyone help me?

Thnx!

+1  A: 

Oke so i found this plugin: http://ludo.cubicphuse.nl/jquery-plugins/treeTable/doc/index.html#example-1

this uses tables and allows it to be nested.

now i just need to sort it and post submit the table elements with a parent id so i can save it to the database.

I hope this helps someone else.

so this is what i came up with. I don't want more then 2 levels ( only parent and childs ) so i changed it to let parents only accept childs, and not made parents draggable.

<html>
    <head>
        <link href="jquery.treeTable.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="jquery-ui.js"></script>
        <script type="text/javascript" src="jquery.treeTable.js"></script>
        <script type="text/javascript">

    $(document).ready(function()  {
        $("#dragndrop").treeTable();

        // Drag & Drop Code
        $("#dragndrop .child").draggable({
          helper: "clone",
          opacity: .75,
          refreshPositions: true, // Performance?
          revert: "invalid",
          revertDuration: 300,
          scroll: true
         }
        );

        $("#dragndrop .parent").each(function() {
          $($(this).parents("tr")[0]).droppable({
            accept: ".child",
            drop: function(e, ui) {
              $($(ui.draggable).parents("tr")[0]).appendBranchTo(this);
              setNewParent($($(ui.draggable).parents("tr")[0]).attr("id"), $(this).attr("id"));
            },
            hoverClass: "accept",
            over: function(e, ui) {
              if(this.id != $(ui.draggable.parents("tr")[0]).id && !$(this).is(".expanded")) {
                $(this).expand();
              }
            }
          });
        });

        function setNewParent(child, parent)
        {
            // do ajax call here
            alert(child);
            alert(parent);
        }
    });

    </script>
</head>
<body>

    <table id="dragndrop">
      <thead>
        <tr> 
          <th>Title</th>
          <th>Size</th>
          <th>Kind</th>
        </tr>
      </thead>
      <tbody>
        <tr id="node-1">
          <td><span class="parent">CHANGELOG</span></td>
          <td>4 KB</td>
          <td>Parent</td>
        </tr>

        <tr id="node-3" class="child-of-node-1">
          <td><span class="child">images</span></td>
          <td>--</td>
          <td>child</td>
        </tr>

        <tr id="node-2">
          <td><span class="parent">doc</span></td>
          <td>--</td>
          <td>Parent</td>
        </tr>

        <tr id="node-4" class="child-of-node-2">
          <td><span class="child">bg-table-thead.png</span></td>
          <td>52 KB</td>
          <td>child</td>
        </tr>

        <tr id="node-5" class="child-of-node-2">
          <td><span class="child">folder.png</span></td>
          <td>4 KB</td>
          <td>child</td>
        </tr>

      </tbody>
    </table>
</body>

To make the whole table sortable you can use jquerty's .sortable() function, but i left that out of here to make it more clear.

FLY