Bug 25279: Use the API to list cities
authorTomas Cohen Arazi <tomascohen@theke.io>
Fri, 24 Apr 2020 14:50:04 +0000 (11:50 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Tue, 12 May 2020 10:39:41 +0000 (11:39 +0100)
This patch makes the general cities datatable use the API for rendering.
To test:
1. Test the datatable behaviour
2. Apply this patch
3. Repeat your tests
=> SUCCESS: Things work! Filtering and sorting specially

Bonus: Use the browser inspector to notice each interaction with the
datatable triggers an API call with the right query parameters

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

admin/cities.pl
koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tt

index de86112..f25ec18 100755 (executable)
@@ -101,11 +101,6 @@ if ( $op eq 'add_form' ) {
     $op = 'list';
 }
 
-if ( $op eq 'list' ) {
-    my $cities = Koha::Cities->search( { city_name => { -like => "%$searchfield%" } } );
-    $template->param( cities => $cities, );
-}
-
 $template->param(
     cityid      => $cityid,
     searchfield => $searchfield,
index f4ec95d..3b33747 100644 (file)
         Searching: [% searchfield | html %]
     [% END %]
 
-    [% IF cities.count %]
-        <table id="table_cities">
-            <thead>
-                <tr>
-                    <th>City ID</th>
-                    <th>City</th>
-                    <th>State</th>
-                    <th>ZIP/Postal code</th>
-                    <th>Country</th>
-                    <th>Actions</th>
-                </tr>
-            </thead>
-            <tbody>
-                [% FOREACH city IN cities %]
-                <tr>
-                    <td>[% city.cityid | html %]</td>
-                    <td>[% city.city_name | html %]</td>
-                    <td>[% city.city_state | html %]</td>
-                    <td>[% city.city_zipcode | html %]</td>
-                    <td>[% city.city_country | html %]</td>
-                    <td class="actions">
-                        <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/cities.pl?op=add_form&amp;cityid=[% city.cityid | html %]"><i class="fa fa-pencil"></i> Edit</a>
-                        <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/cities.pl?op=delete_confirm&amp;cityid=[% city.cityid | html %]"><i class="fa fa-trash"></i> Delete</a>
-                    </td>
-                </tr>
-                [% END %]
-            </tbody>
-        </table>
-    [% ELSE %]
-        <div class="dialog message">
-            There are no cities defined. <a href="/cgi-bin/koha/admin/cities.pl?op=add_form">Create a new city</a>.
-        </div>
-    [% END %]
+    <table id="table_cities">
+        <thead>
+            <tr>
+                <th>City ID</th>
+                <th>City</th>
+                <th>State</th>
+                <th>ZIP/Postal code</th>
+                <th>Country</th>
+                <th>Actions</th>
+            </tr>
+        </thead>
+    </table>
 [% END %]
 
             </main>
     [% INCLUDE 'datatables.inc' %]
     <script>
         $(document).ready(function() {
-            $("#table_cities").dataTable($.extend(true, {}, dataTablesDefaults, {
-                "aoColumnDefs": [
-                    { "aTargets": [ -1, -2 ], "bSortable": false, "bSearchable": false },
-                ],
-                "aaSorting": [[ 1, "asc" ]],
-                "iDisplayLength": 10,
-                "sPaginationType": "full_numbers"
-            }));
+            var cities_table_url = '/api/v1/cities';
+            var cities_table = $("#table_cities").api({
+                "ajax": {
+                    "url": cities_table_url
+                },
+                'dom': 'C<"top pager"ilpfB><"#filter_c">tr<"bottom pager"ip>',
+                "columns": [
+                    {
+                        "data": "city_id",
+                        "searchable": true,
+                        "orderable": true
+                    },
+                    {
+                        "data": "name",
+                        "searchable": true,
+                        "orderable": true
+                    },
+                    {
+                        "data": "state",
+                        "searchable": true,
+                        "orderable": true
+                    },
+                    {
+                        "data": "postal_code",
+                        "searchable": true,
+                        "orderable": true
+                    },
+                    {
+                        "data": "country",
+                        "searchable": true,
+                        "orderable": true
+                    },
+                    {
+                        "data": function( row, type, val, meta ) {
+
+                            var result = '<a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/cities.pl?op=add_form&amp;cityid='+ row.id +'"><i class="fa fa-pencil"></i> Edit</a>';
+                            result += '<a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/cities.pl?op=delete_confirm&amp;cityid='+ row.id +'"><i class="fa fa-trash"></i> Delete</a>';
+                            return result;
+
+                        },
+                        "searchable": false,
+                        "orderable": false
+                    }
+                ]
+            });
         });
     </script>
 [% END %]