Giter Site home page Giter Site logo

sortable-tasks's Introduction

Tasks

Simple demo of using Django 1.9 order_with_respect_to and jQuery sortable.

steps:

pip install -r requirements.txt
createdb -Upostgres tasks
python manage.py migrate
python manage.py bower install

important code

model.py

class Task(models.Model):
    tasklist = models.ForeignKey(TaskList, null=False)
...
    class Meta:
        order_with_respect_to = 'tasklist'

tasklist.html

<ul id="sortable">
    {% for task in tasks %}
    <li id="task_{{task.id}}"><i class="fa fa-sort"></i> {{ task.name }}
    <a href="{% url "task_edit" task.id %}">edit</a>
    <a href="{% url "task_delete" task.id %}">delete</a>
    </li>
    {% endfor %}
</ul>

urls.py

url(r'^(?P<pk>\d+)/task_sort$', views.task_sort, name='task_sort'),

view.html

def task_sort(request, pk, template_name=None):
    task_list = get_object_or_404(TaskList, pk=pk)
    tasks = Task.objects.filter(tasklist=task_list)
    if request.method=='POST':
        new_order = request.POST.getlist("task[]")
        new_order = [int(x) for x in new_order]
        print("New Order",new_order)
        print("Existing Order", task_list.get_task_order())
        task_list.set_task_order(new_order)
    return HttpResponse(status=200)

base.html

$(function () {
    var csrftoken = Cookies.get('csrftoken');
    function csrfSafeMethod(method) {
        // these HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }
    $.ajaxSetup({
        beforeSend: function(xhr, settings) {
            if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            }
        }
    });
    $("#sortable").sortable({
        axis: "y",
        cursor: "move",
        update: function (event, ui) {
            var order = $('#sortable').sortable('serialize');
            //$("#showmsg").text(order);
            //order = order.replace(/\[\]/gi, "");
            console.log("reorder",order);
            var path = window.location.pathname;
            path = path + "/task_sort";
            $.post(path, order, null);
        }
    });
    $("#sortable").disableSelection();
});

sortable-tasks's People

Contributors

pmuston avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.