For the standard TT templates, I think Template::Plugin::URL may be the way to go. Anybody have any views?

OK, so I have added a new macro - link_query - uses this plugin to generate the query URLs:

[%# link_query

  # This creates an HTML link to a Maypole action that takes query
  # parameters by concatenating the base URL, table, command and query
  # arguments. The query args are supplied as a hash reference - see the
  # docs for the URL plugin for details
-%]
[%-
MACRO link_query(table, command, arg_hash, label) BLOCK;
    SET pth = base _ '/' _ table _ '/' _ command ;
    USE lnk = URL(pth, arg_hash) ;
    '<a href="' _ lnk _ '">'; label; '</a>';
END;
-%]

I'll also probably add a check in link() to make it die if there's a '?' in the additional arg.

And I've modified a template of mine to match (see attached file). It seems to work in my little test rig. Notes about the template:

-1- This is one of mine that I use as a sub-template to list.tt and anywhere else I need headings (like in my view.tt :)

 -2- It uses my tables and columns hash rather than classmetadata etc

-3- It does some extra stuff to be compatible with both Maypole::Model::CDBI and Maypole::FormBuilder

I'll modify my other templates (pager.tt for one) and experiment with a more seriously sized dataset. I'll certainly test it with M-FB and may do some cursory testing with M-M-CDBI. It would be great if other people tried these changes in their own templates. I'll add a bug report and at some point will commit equivalent changes unless I hear of problems.

Cheers, Dave
<!-- custom/table_headings.tt -->
[%# Produce the table headings

    # This produces column headings displayed at the top of lists.
    # It outputs a table heading row: <tr> <th>..</th> ... </tr>
    # Template variables used include:
    # - request - the Maypole request object
    # - tables  - the global static hash of table properties
    # - columns - the global static hash of column properties
    #
    # - table   - the name of the current table (set by list.tt)
    # - pager   - an object that responds to the Data::Page interface.
-%]
    <tr>
[%  USE HTML ;

    # Copy all params that represent columns from the query to a hash.
    # Also, make the action a search if there are any such params
    action = "list" ;
    FOR name = tables.$table.display_columns.list ;
        IF request.query.$name ;
            args.$name = request.query.$name ;
            action = "do_search" ;
        END ;
    END ;

    # There are two ways that order_by can be specified by M-M-CDBI and
    # M-FB (don't ask why!) so make sure we handle both.
    order_col = request.query.order ;   # name of column to sort by, if any
    order_dir = request.query.o2 ;      # 'desc' or 'asc' or blank (=asc)
    IF request.query.search_opt_order_by ;
        soob = request.query.search_opt_order_by.split(' ') ;
        order_col = soob.0 ;
        order_dir = soob.1 ;
    END ;
    IF order_dir == 'asc' ; order_dir = '' ; END ; # just 'desc' or blank

    # Copy the comparison operator parameter across, if it's present
    # and the page number
    IF request.query.search_opt_cmp ;
        args.search_opt_cmp = request.query.search_opt_cmp ;
    END ;

    IF pager ; args.page = pager.current_page ; END ;
    
    # Now make a heading for each column
    FOR col = tables.$table.list_columns.list ;
        label = columns.$table.$col.singular ;

        # Order by this column, if link is clicked (in two ways!) and
        # make sure we invert the ascending/descending as appropriate
        args.order = col ;
        args.search_opt_order_by = col ;
        args.o2 = '' ;  # would like to delete key, but TT can't!
        IF col == order_col and order_dir != 'desc' ;
            args.o2 = 'desc' ;
            args.search_opt_order_by = col _ ' desc' ;
        END ;

        # Output the HTML table element containing the link, and include
        # a down arrow or up arrow for the currently sorted column
        '      <th>' ;
        link_query(table, action, args, label) ;

        IF col == order_col ;
            IF order_dir == "desc" ;
                "&uarr;" ;
            ELSE ;
                "&darr;" ;
            END ;
        END ;
        "</th>\n" ;
    END ;
-%]
      <th id="actionth">Actions</th>
    </tr>
<!-- end custom/table_headings.tt -->

Reply via email to