Topik 11. CRUD With Ajax and Modals - Updatejun2023
Topik 11. CRUD With Ajax and Modals - Updatejun2023
After finish check like the picture above, please move the button Submit and Cancel to the footer by :
- placing the Form element position after page-content.
- remove Close button, move Submit and Cancel button to footer
- add data-dismiss="modal“ in the cancel button
Result
Violaa..
Try to insert your new Category data
Note
1. We consider that this syntax have a new custom routing “(category.getEditForm)”. Don’t forget
to make a new route for this syntax
2. After the ajax succeded it fill in the modalContent with data.msg. Don’t forget to make a new
modals/div with id = “modalContent”
Prepare the Route
- Please add a new router for ‘getEditForm and redirected to CategoryController function ‘getEditForm’.
Note: don’t forget to make a custom function inside Category Controller
Prepare the editModal
- this function will response with a json element (msg) filled with html string
that showing the edit form from getEditForm2.blade.php
Create the Edit Form
The structure can be taken from the editForm modal. Modify the form action , add with method(‘PUT’), and fill in
the textfield and textarea with the sended data.
Result
When you click submit button, system will store the new value and re open the list of
category page.
Tips
If you have an error or unresponsive experience, you can
trace with:
1. Inspect Element > Console
2. Insepct Element > Network
Example #1: error variable
This error show after user click the “Edit Button”. To solve this,
you must
1. Check the javascript after click (onclick function)
2. Check the Ajax and return value of this Ajax
Solve for Example #1
The modals can’t show in your screen because the name of “id”
is different between <a href> and modal definition.
You must change the id value.
The limitation of TypeA mechanisms
Add an id for each edited <TD> using any string that express
the field following by the record ID
Add new edit button in each record
It open a modal, and can re-use our previous (type A) modal
The submit button in edit form calling an ajax function which
will updating database and updating the <TD> content
Add ID in TD’s
Add a new edit button (B)
Prepare the ajax function (B)
-There is a custom route with route name (category.getEditFormB) and after the Ajax
succeded it fills in the modalContent with data.msg
Prepare the Route (B)
- this function will response with a json element (msg) filled with html string that
showing the edit form from getEditFormB.blade.php
Create the Edit Form (B)
• Copy-paste from the type
A.
Remove the form.
• Specify the ID of textbox
and textarea.
• Modify the button
submit become a normal
button, add a data-
dismiss, and add a
calling ajax function in its
onclick