Skip to content

Commit 5bca15b

Browse files
committed
add async update example to README.rst
1 parent b10afe0 commit 5bca15b

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

README.rst

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,111 @@ Add script to the template from #5 and bind the ``modalForm`` to the trigger ele
212212
});
213213
</script>
214214

215+
Async create/update with or without modal closing on submit
216+
===========================================================
217+
218+
Set ``asyncUpdate`` and ``asyncSettings`` settings to create or update objects without page redirection to ``successUrl`` and define whether a modal should close or stay opened after form submission. See comments in example below and paragraph **modalForm options** for explanation of ``asyncSettings``.
219+
220+
.. code-block:: html
221+
222+
index.html
223+
224+
<!-- asyncSettings.dataElementId -->
225+
<table id="books-table" class="table">
226+
<thead>
227+
...
228+
</thead>
229+
<tbody>
230+
{% for book in books %}
231+
<tr>
232+
...
233+
<!-- Update book buttons -->
234+
<button type="button" class="update-book btn btn-sm btn-primary" data-form-url="{% url 'update_book' book.pk %}">
235+
<span class="fa fa-pencil"></span>
236+
</button>
237+
...
238+
</td>
239+
</tr>
240+
{% endfor %}
241+
</tbody>
242+
</table>
243+
244+
<script type="text/javascript">
245+
$(function () {
246+
...
247+
248+
# asyncSettings.successMessage
249+
var asyncSuccessMessage = [
250+
"<div ",
251+
"style='position:fixed;top:0;z-index:10000;width:100%;border-radius:0;' ",
252+
"class='alert alert-icon alert-success alert-dismissible fade show mb-0' role='alert'>",
253+
"Success: Book was updated.",
254+
"<button type='button' class='close' data-dismiss='alert' aria-label='Close'>",
255+
"<span aria-hidden='true'>&times;</span>",
256+
"</button>",
257+
"</div>",
258+
"<script>",
259+
"$('.alert').fadeTo(2000, 500).slideUp(500, function () {$('.alert').slideUp(500).remove();});",
260+
"<\/script>"
261+
].join();
262+
263+
# asyncSettings.addModalFormFunction
264+
function updateBookModalForm() {
265+
$(".update-book").each(function () {
266+
$(this).modalForm({
267+
formURL: $(this).data("form-url"),
268+
asyncUpdate: true,
269+
asyncSettings: {
270+
closeOnSubmit: false,
271+
successMessage: asyncSuccessMessage
272+
dataUrl: "books/",
273+
dataElementId: "#books-table",
274+
dataKey: "table",
275+
addModalFormFunction: updateBookModalForm
276+
}
277+
});
278+
});
279+
}
280+
updateBookModalForm();
281+
282+
...
283+
});
284+
</script>
285+
286+
.. code-block:: python
287+
288+
urls.py
289+
290+
from django.urls import path
291+
from . import views
292+
293+
urlpatterns = [
294+
...
295+
# asyncSettings.dataUrl
296+
path('books/', views.books, name='books'),
297+
...
298+
]
299+
300+
.. code-block:: python
301+
302+
views.py
303+
304+
from django.http import JsonResponse
305+
from django.template.loader import render_to_string
306+
from .models import Book
307+
308+
def books(request):
309+
data = dict()
310+
if request.method == 'GET':
311+
books = Book.objects.all()
312+
# asyncSettings.dataKey = 'table'
313+
data['table'] = render_to_string(
314+
'_books_table.html',
315+
{'books': books},
316+
request=request
317+
)
318+
return JsonResponse(data)
319+
215320
modalForm options
216321
=================
217322

@@ -233,6 +338,27 @@ errorClass
233338
submitBtn
234339
Sets the custom class for the button triggering form submission in modal. ``Default: ".submit-btn"``
235340

341+
asyncUpdate
342+
Sets asynchronous content update after form submission. ``Default: false``
343+
344+
asyncSettings.closeOnSubmit
345+
Sets whether modal closes or not after form submission. ``Default: false``
346+
347+
asyncSettings.successMessage
348+
Sets successMessage shown after succesful for submission. Should be set to string defining message element. See ``asyncSuccessMessage`` example above. ``Default: null``
349+
350+
asyncSettings.dataUrl
351+
Sets url of the view returning new queryset = all of the objects plus newly created or updated one after asynchronous update. ``Default: null``
352+
353+
asyncSettings.dataElementId
354+
Sets the ``id`` of the element which renders asynchronously updated queryset. ``Default: null``
355+
356+
asyncSettings.dataKey
357+
Sets the key containing asynchronously updated queryset in the data dictionary returned from the view providing updated queryset. ``Default: null``
358+
359+
asyncSettings.addModalFormFunction
360+
Sets the method needed for reinstantiation of event listeners on button after asynchronous update. ``Default: false``
361+
236362
Forms
237363
=====
238364

0 commit comments

Comments
 (0)