Most of the maintenance program on the site I’m rewriting use an AJAX-CRUD program – with some modifications – but the overall design was a but too complicated. So, I started looking around for an AJAX CRUD sample program for CI and found a pretty nice one that uses Bootstrap. It displays a table which has a search option and the columns can be sorted by clicking the column headings. There is an Add button at the top and on each table line there are Edit and Delete buttons. The Add and Edit options display a Bootstrap modal to enter or edit record information. But, there were a few things I didn’t like about it.
It would require a new model and view of each application. OK, that is pretty standard – but in this case the only change in the model is some basic configuration and the view is mostly boilerplate. And, for my application I have at least 12 CRUD programs. So, I made some changes.
I put all the configuration in a “settings” method in the controller and wrote some helper functions to generate table column headings, the JS for the AJAX and the modal input fields – based on the settings. Also added the ability to have extra buttons on the table rows – if I need to call a sub-program (which I will).
The original program author was not using the CI validation class so I incorporated that as well.
End result – I have one CRUD model and one CRUD view which can be used by many CRUD controllers. All the configuration is in the controller.
I set up a second version, stripped down to remove the table view – so it has just add and edit buttons and each displays the modal. The edit button simulates loading the modal fields from a database. This second version allowed my to test out different input field types and options such as CKEditor and Datepicker. Took a lot of fiddling to get Radio buttons and Checkboxes to work correctly but it all works. I added a feature for a Select input (dropdown list) where the options are from a reference table ad selecting an option pulls values from the reference table to pre-populate input fields.