% my $listview = sprintf '.%sview', $request->listviewmode; # list or editlist

<h2><% $request->action eq 'do_search' ? 'Search results: ' : '' %><% ucfirst( $request->model_class->plural_moniker ) %></h2>

% # <pre>
% # display_columns: <% join( ', ', $request->model_class->display_columns ) %>
% # related:         <% join( ', ', $request->model_class->related ) %>
% # list_columns:    <% join( ', ', $request->model_class->list_columns ) %>
% # list_fields:     <% join( ', ', $request->model_class->list_fields ) %>
% # </pre>

<& .switch   &>
<& pager     &>
<& $listview &>
<& addnew    &>
<& search    &>
    
% #
% # The columns and fields rendered in .listview     are set here (list_columns + list_fields).
% # The columns and fields rendered in .editlistview are set in setup_form_mode, editlist mode
% #        (list_columns + list_fields in the default model MP::FB::Model).
% # The columns and fields rendered in the view template are the defaults in MP::P::QuickTable::tabulate()
% #         - display_columns + related
% # The columns and fields rendered in the edit   form...
% # The columns and fields rendered in the search form...
% #
% # -------------------------------------
<%def .listview>
<%perl>
    my $callback = sub 
    {
        my ( $object ) = @_;
        
        return $request->as_form( mode => 'edit_button',   entity => $object )->render . 
               $request->as_form( mode => 'delete_button', entity => $object )->render;
    };

    my @data = $request->tabulate( with_colnames => 1, 
                                   callback      => $callback, 
                                   fields        => [ $request->model_class->list_columns, 
                                                      $request->model_class->list_fields
                                                      ],
                                   );
    
    my $i = 0;
    
    push @{ $data[0] }, 'Actions';
</%perl>
% # quick_table will populate the table with display_columns by default
<div class="NavListTable"><% $request->quick_table( labels => 'T' )->render( @data ) %></div>
</%def>

% # -------------------------------------
<%def .editlistview>
<%perl>
    # Note: turn off stickiness. Otherwise, all the forms will display the values submitted 
    # in the previous action, e.g. addnew.
    my @forms = $request->as_forms( submit          => [ qw( view update edit delete ) ],
                                    reset           => 'reset',
                                    selectnum       => 2,
                                    mode            => 'editlist',
                                    no_textareas    => 1,
                                    sticky          => 0, 
                                    jsfunc          => qq(
if (form._submit.value == "delete") {
    if (confirm("Really DELETE this entry?")) return true;
    return false;
}
),
                                    );

    my %names = $request->model_class->column_names;
                                    
</%perl>
<div class="FormListTable">
<table border="0">
<tr>
% # foreach my $col ( $request->model_class->display_columns ) { # this misses has_many and might_have fields
% my $dummy_form = $forms[0] || $request->model_class->as_form( mode => 'editlist' );
% foreach my $field ( grep { $_->type ne 'hidden' } $dummy_form->fields ) {
<th><% $names{ $field } || ucfirst( $field ) %></th>
% }
<th colspan="5">Actions</th>
</tr>
% foreach my $form ( @forms ) {
<% $request->render_form_as_row( $form ) %>
% }
</table>
</div>
</%def>

% # -------------------------------------
<%def .switch>
% my $other = $request->listviewmode eq 'editlist' ? 'navigable' : 'editable';
<p class="subheading">
<% $request->link( table  => $request->model_class->table,
                   action => 'switchlistmode',
                   label  => "Switch to $other list view",
                   ) %>
</p>
</%def>