WJProctor
WJProctor

Reputation: 71

JQuery Sortable Serialized

I am using http://johnny.github.io/jquery-sortable/ to sort a table, I have the JQuery working no problem but i cant get the serialize to work correctly. I wondered if someone could help and explain what i need todo?

So my html is.

<table class="table table-striped table-bordered sorted_table" id="positionstable">
    <tbody>
        <tr>
            <th class="sectionname" colspan="1">Position</th>
        </tr>
        <tr data-id="2">
            <td class=" ">Item 2</td>
        </tr>
        <tr data-id="1">
            <td class=" ">Item 1</td>
        </tr>
        <tr data-id="3">
            <td class=" ">Item 3</td>
        </tr>
        <tr data-id="4">
            <td class=" ">Item 4</td>
        </tr>
    </tbody>
</table>

My JQuery is:

$('.sorted_table').sortable({
  containerSelector: 'table',
  itemPath: '> tbody',
  itemSelector: 'tr',
  placeholder: '<tr class="placeholder"/>',

  onDrop: function (item, container, _super) {
            var dataToSend = $('.sorted_table').sortable("serialize").get();

            console.log(dataToSend)

            $.ajax({
                url: "ajax_action.php",
                type: "post",
                data: dataToSend,
                cache: false,
                dataType: "json",
                success: function () {}
            });
            //_super(item, container);
        }
})

You can also find the code at http://jsfiddle.net/WJProctor/7GqQu/

I would like an array of ID's to be sent to my ajax script.

I look forward to you any help and advice.

Kind Regards

James

Upvotes: 1

Views: 798

Answers (1)

vorillaz
vorillaz

Reputation: 6276

Usually the serialize works on form elements and generates JSON objects, using the name of each field as a key and the value of the element as it's value.

You can easily generate a serialized object , just as looping through your table rows as

$('.sorted_table').sortable({
  containerSelector: 'table',
  itemPath: '> tbody',
  itemSelector: 'tr',
  placeholder: '<tr class="placeholder"/>',

  onDrop: function (item, container, _super) {
            var obj = jQuery('.sorted_table tr').map(function(){
                return 'trvalue[]=' + jQuery (this).attr("data-id");
            }).get();
          console.log(obj)
          //do the ajax call
        }
})

http://jsfiddle.net/7GqQu/1/

Upvotes: 1

Related Questions