ObbyOss
ObbyOss

Reputation: 351

jQuery UI Sortable Not Working With Dynamic Table

I'm dynamically creating a table and trying to make each row (tr) sortable. I've followed the various documentation on the jQuery UI site and thought I understood. However, I can't quite seem to figure it out.

Where did I go wrong? Thanks!

 <!doctype HTML>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
    <link href="css/bootstrap-form-helpers.min.css" rel="stylesheet" media="screen">
    <link href="jquery-ui.css" type="text/css" rel="stylesheet">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script type="text/javascript>" href="jquery-ui.js"></script>
    <script type="text/javascript" href="bootstrap-2.2.2.min.js"></script>

    <script>
        $(function() {
            $( "tr" ).sortable();
            $( "tr" ).disableSelection();
        });
    </script>

    </head>
    <body>
    <div class="center">
    <form id="agenda_form">
        <p>Session Title</p>
        <input type="text" id="session_title">
        <p>Start Time<p>
        <input type="number" id="start_time">
        <p>End Time</p>
        <input type="number" id="end_time">
        <input type="submit" value="Submit" id="submit_form">
    </form>
    <table class="table-striped">
        <tr>
            <td>Session Title</td>
            <td>Start Time</td>
            <td>End Time</td>
        </tr>
    </table>
    </div>

    <script type="text/javascript">
        $("#submit_form").click(function (event) {
            event.preventDefault();
            var $tr = $('<tr />');
            $tr.append($("<td />", { text: $("#session_title").val()} ))
            $tr.append($("<td />", { text: $("#start_time").val()} ))
            $tr.append($("<td />", { text: $("#end_time").val()} ))
            $tr.appendTo("table");
            $("#agenda_form").each(function (){
                this.reset();
            });
        });
    </script>
    </body>
    </html>

Upvotes: 0

Views: 1765

Answers (1)

Mat Forsberg
Mat Forsberg

Reputation: 454

You need to use tbody instead of tr in your jquery selection. i would also suggest makeing your headers in a theader block --- http://jsfiddle.net/rcottkqx/1/

<div class="center">
<form id="agenda_form">
    <p>Session Title</p>
    <input type="text" id="session_title">
    <p>Start Time
        <p>
            <input type="number" id="start_time">
            <p>End Time</p>
            <input type="number" id="end_time">
            <input type="submit" value="Submit" id="submit_form">
</form>
<table class="table-striped">
    <thead>
        <th>Session Title</th>
        <th>Start Time</th>
        <th>End Time</th>
    </thead>
</table>

$("#submit_form").click(function (event) {
    event.preventDefault();
    var $tr = $('<tr class="t" />');
    $tr.append($("<td />", {
        text: $("#session_title").val()
    }));
    $tr.append($("<td />", {
        text: $("#start_time").val()
    }));
    $tr.append($("<td />", {
        text: $("#end_time").val()
    }));
    $tr.appendTo("table");
    $("#agenda_form").each(function () {
        this.reset();
    });
    $("tbody").sortable();
    $("tbody").disableSelection();
});

Upvotes: 1

Related Questions