-1

When I insert a data through ajax, I have used

$( "#example_wrap" ).load(window.location.href + " #example" );

to reload a div, but after this has done, my dragging and sorting the list is not working, so I just used

        sort_table;
        $('#example tbody').sortable('destroy');        
        $('#example tbody').unbind();
        sort_table;

sorting functions are assigned to the variable sort_table

sort_table =
$( "#example tbody" ).sortable({
    axis: 'y',
    update: function (event, ui) {
        var self = $(this)[0];
        var tr = $(self).find('tr');

        topnews_a = [];
        var order = 1;
        $(tr).each(function(){
            var content_id = parseInt( $(this).attr('content_id') );
            var order_id = order;

            topnews_a.push({
                content_id: content_id,
                order_id: order_id,
            });
            order++;
        });
        var topnews_obj = JSON.stringify(topnews_a);
        var data_a = {
            topnews: topnews_obj
        }



        $.ajax({
            type:"POST",
            url: base_url+"topnews/do_order",
            data: data_a,
            beforeSend:function(){
                //$('.clearClassBlock').html('<div class="alert alert-danger alert-dismissable">Select A Section.</div>');
            },
            complete:function(){

            },
            success:function(x){
                alert('Order Changed Successfully');
            },
            error:function(x){

            }
        });
    }
});
Midhun
  • 115
  • 1
  • 1
  • 10

1 Answers1

0

First thing first: As per MDN unnamed functions (a function assigned to a variable) should be attached to event for callback.

After

sorting functions are assigned to the variable sort_table

this is not sorting functions once you assign this to a variable, it is now function expression as definition is without function keyword as first statement.

When you call the variable without () then it will return the expression definition as your asking for the value of the variable (not invoking a function) .

So you need to invoke your function expression. sort_table(); the sort_table should be declared before you invoke it.

Further you can refer this for how to name a javascript function and execute it immediately?

Community
  • 1
  • 1
Anil
  • 3,722
  • 2
  • 24
  • 49
  • But whenever tries this also, not working... after refreshing query is running, sorting function will not work. – Midhun May 03 '17 at 06:48
  • have you referred the url from answer and tried with `sort_table = function() { $( "#example tbody" ).sortable( ........ };` – Anil May 03 '17 at 07:15