2

我使用这个轻型可排序插件通过拖放 http://farhadi.ir/projects/html5sortable/对我的数据进行排序

现在,如何更新我的排序顺序

 $('.sortable').sortable().bind('sortupdate', function() {
    //Triggered when the user stopped sorting and the DOM position has changed.

 });

视图.py

 def filter_order(request):
    if request.method == 'POST':
        order = request.POST.getlist('filter[]')
        count = 0
        for id in order:
            count += 1
            filter = FilterModel().objects.get(pk=id)
            filter.sort_order = count
            filter.save()
        return HttpResponse('Successfully updating rules order.')
    else:
        return HttpResponse("Error updating rules order.")

网址.py

 urlpatterns = patterns('transactions.views',
      ............

      url(r'^filter-order/$', 'filter_order', 
            name='filter_order'),
 )

规则.html

 <ul class="sortable" id="filter-items">
    {% for filter in filters %}
    <li id="{{ filter.id }}">{{filter.rules}}</li>
    {% endfor %}
 </ul>    
4

1 回答 1

0
<script>
$('.sortable').sortable().bind('sortupdate', function() {
    datas = new Array();
    var i = 0;

    $('#filter-items li').each(function() {
        datas[i] = $(this).attr('id');
        i++;
    });

    $.ajax({
        type: "POST",
        data: { 
            'filter[]': datas, 
            'csrfmiddlewaretoken': '{{csrf_token}}'
        }, 
        url:"/transactions/filter-order/",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function(data) { 
            notify('', 'Successfully updating rules order.', { autoClose: true, delay: 1000 });
        },
        error: function(ts) { 
            notify('', ts.responseText, { autoClose: true, delay: 1000 });
        }
    });
});
</script>
于 2013-01-27T16:06:25.843 回答