JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
listings sort when you click headers
authorJason Woofenden <jason@jasonwoof.com>
Tue, 28 Aug 2012 07:00:34 +0000 (03:00 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Tue, 28 Aug 2012 07:00:34 +0000 (03:00 -0400)
metaform.php
metaform/template.html
metaform/template.php

index b4ec6e0..3a6e70d 100644 (file)
@@ -304,7 +304,7 @@ function make_html($whole_file = true) {
                }
 
                if(show_in_listing($type, $input, $format, $sql)) {
-                       $listing_headers[] = array('caption' => format_caption($name));
+                       $listing_headers[] = array('caption' => format_caption($name), 'name' => $name);
                        $listing_field = array('name' => $name);
                        if($format == 'bool' || $format == 'yesno') {
                                $listing_field['enc'] = 'yesno';
index b2e791f..89f850a 100644 (file)
                <div class="field"><a href="~file_name attr~~~id {~~?id=~~id~~~~}~~">Cancel</a></div><!--~}~-->
        <!--~}~--><!--~~}~~--><!--~opt_listing {~-->
 
-       <!--~~listings once {~~-->
+       <!--~~listings {~~-->
                <h2>~plural cap~ Listing</h2>
 
-               <!--~~listings once_if {~~-->
+               <!--~~rows once_if {~~-->
                        <p><a href="~file_name~?new=1">[Add a new ~singular~]</a></p>
 
                        <table cellspacing="0" cellpadding="4" border="1" summary="">
-                               <tr><!--~listing_headers {~--><th>~caption~</th><!--~}~--><th>&nbsp;</th></tr><!--~~listings {~~-->
+                               <tr><!--~listing_headers {~--><th><a href="?sort=~~sorting-by-~name~~~~name~">~caption~</a></th><!--~}~--><th>&nbsp;</th></tr><!--~~rows {~~-->
                                <tr><!--~listing_fields {~-->
                                        <td class="listing"><a href="~file_name~?~opt_display unset {~edit_~}~id=~~id~~"><!--~enc {~-->~~~name~ ~enc~~~<!--~~~name~ empty {~~--><em>(blank)</em><!--~~}~~--><!--~}~--><!--~thumb {~--><!--~~~name~ nonempty {~~--><img src="~~~name~ thumb_src~~" width="~~~name~ thumb_width~~" height="~~~name~ thumb_height~~" alt=""><!--~~}~~--><!--~}~--></a></td><!--~}~-->
                                        <td><a href="~file_name~?~file_name~_delete_id=~~id~~" onclick="return confirm('Permanently delete?')">[delete this ~singular~]</a></td>
 
                        </table>
                <!--~~}~~-->
-               <!--~~listings once_else {~~-->
+               <!--~~rows once_else {~~-->
                        <p>No ~plural~ in database.</p>
                <!--~~}~~-->
 
index fa05c07..6479fc5 100644 (file)
@@ -143,8 +143,22 @@ function ~file_name~_main_delete($id) {
 }~}~~opt_listing {~
 
 function ~file_name~_main_listing() {
-       $listing_rows = db_get_assocs('~table_name~', 'id~listing_fields_1 {~,~name~~}~', 'order by ~always_field~ limit 1000');
-       tem_set('listings', $listing_rows);
+       $data = array();
+       $desc = '';
+       $sort = _REQUEST_cut('sort');
+       if($sort && substr($sort, 0, 1) === '-') {
+               $sort = substr($sort, 1);
+               $desc = ' DESC ';
+       } else {
+               $data["sorting-by-$sort"] = '-';
+       }
+       $legal_sorts = explode(',', ~file_name upper~_DB_FIELDS);
+       if(!$sort || !in_array($sort, $legal_sorts)) {
+               $sort = '~always_field~';
+       }
+
+       $data['rows'] = db_get_assocs('~table_name~', 'id~listing_fields_1 {~,~name~~}~', "order by $sort $desc limit 1000");
+       tem_set('listings', $data);
 }~}~
 
 function ~file_name~_main_form($id = false) {~pulldowns {~