javascript - Django / jQuery - Button to update a value on each row of a table -
i learning django, making app can maintain track of books boolean whether have read them or not learning project. model relatively self explanatory books have title , id , boolean completed "field". want have page lists books , has button next each book alter read status true , update using ajax/jquery (and similar unread button mistakes). having problem getting button work. think need somehow assign sort of id each row jquery knows item working with.
apologies obvious errors. new web programming , regret having inquire help, have been trying work out solution myself few days now.
here django template snippet:
{% book in lib_dict.books %} <tr> <td>{{ book.title }}</td> <td>{{ book.date_due }}</td> <td id="book_completed_{{ book.id }}">{{ book.completed }}</td> <td> <button id="{{ book.id }}" data-bookid="{{ book.id }}" class="btn-mini btn-primary c" type="button">complete</button> </td> </tr> {% endfor %}
views.py:
def complete_book(request): context = requestcontext(request) book_id = none if request.method == 'get': book_id = request.get['book_id'] if book_id: book = book.objects.get(id=int(book_id)) if book: book.completed = true book.save() homecoming httpresponse(book.completed)
jquery code - imagine error(s) here
$('.btn-mini btn-primary c').click(function(){ var bookid; bookid = $(this).attr("data-bookid"); $.get('/libco/complete_book/', {book_id: bookid}, function(data){ var bookstring = '#book_completed_' + bookid; $(bookstring).html(data); \\trying update completed table cell $('#bookid').hide(); \\trying hide button }); });
please note, of connectivity works fine. if create button single row dedicated id, , utilize appropriate #-tag in first line of jquery works, having problem generalization. maybe "." thing isn't way class in first line?
also, much trying teach myself, if there resources kind of thing please allow me know. started working on official jquery tutorial, looked through , nil seemed related this.
thanks all!
change this:
$('.btn-mini btn-primary c')
to:
$('.btn-mini.btn-primary.c')
javascript jquery django
No comments:
Post a Comment