dynamically delete buttons

This commit is contained in:
Pierre-antoine Comby 2020-03-24 22:12:44 +01:00
parent 8ab142c122
commit ba04a6555f
3 changed files with 31 additions and 5 deletions

View File

@ -5,6 +5,7 @@ from django.db.models import Q
from django_filters.rest_framework import DjangoFilterBackend from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter, SearchFilter from rest_framework.filters import OrderingFilter, SearchFilter
from api.viewsets import ReadProtectedModelViewSet, ReadOnlyProtectedModelViewSet from api.viewsets import ReadProtectedModelViewSet, ReadOnlyProtectedModelViewSet
from rest_framework import viewsets
from .serializers import NotePolymorphicSerializer, AliasSerializer, TemplateCategorySerializer, \ from .serializers import NotePolymorphicSerializer, AliasSerializer, TemplateCategorySerializer, \
TransactionTemplateSerializer, TransactionPolymorphicSerializer TransactionTemplateSerializer, TransactionPolymorphicSerializer
@ -81,7 +82,7 @@ class TemplateCategoryViewSet(ReadProtectedModelViewSet):
search_fields = ['$name', ] search_fields = ['$name', ]
class TransactionTemplateViewSet(ReadProtectedModelViewSet): class TransactionTemplateViewSet(viewsets.ModelViewSet):
""" """
REST API View set. REST API View set.
The djangorestframework plugin will get all `TransactionTemplate` objects, serialize it to JSON with the given serializer, The djangorestframework plugin will get all `TransactionTemplate` objects, serialize it to JSON with the given serializer,

View File

@ -90,5 +90,10 @@ class ButtonTable(tables.Table):
model = TransactionTemplate model = TransactionTemplate
delete = tables.TemplateColumn(template_code="""
<button id="{{ record.pk }}" class="btn btn-danger" onclick="delete_button(this.id)"> delete </a>
""")
def render_amount(self, value): def render_amount(self, value):
return pretty_money(value) return pretty_money(value)

View File

@ -14,9 +14,14 @@
</div> </div>
</div> </div>
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="col-md-10"> <div class="col-md-10 card shadow">
<div class="card-header text-center">
<h5> {% trans "buttons listing "%}</h5>
</div>
<div class="card" id="buttons_table">
{% render_table table %} {% render_table table %}
</div> </div>
</div>
</div> </div>
{% endblock %} {% endblock %}
@ -40,7 +45,6 @@ function getInfo() {
$('table tr').show(); $('table tr').show();
} }
} }
var timer; var timer;
var timer_on; var timer_on;
/* Fontion appelée quand le texte change (délenche le timer) */ /* Fontion appelée quand le texte change (délenche le timer) */
@ -54,5 +58,21 @@ function search_field_moved(secondfield) {
timer_on = true; timer_on = true;
} }
} }
// on click of button "delete" , call the API
function delete_button(button_id){
console.log(button_id);
$.ajax({
url:"/api/note/transaction/template/"+button_id,
method:"DELETE",
headers: {
"X-CSRFTOKEN": CSRF_TOKEN
},
success: function(){
addMsg('{% trans "button successfully deleted "%}','success');
$("#buttons_table").load('{% url "note:template_list" %} #buttons_tables');
},
error: addMsg(' {% trans "Unable to delete button "%} #' + button_id,'danger' )
});
}
</script> </script>
{% endblock %} {% endblock %}