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.")

urls.py

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

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

rules.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 に答える