I'm having difficulty getting built-in grid sort working with my MVC
component.
I have scoured the com_contact and com_newsfeeds to compare code
within the model and view.
Only the sort column is being applied, not the sort order. So I can
only sort columns ASC. Another strange item is that the fancy little
sort images do not appear in the column headers.
At the start of my view is the boilerplate:
$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn = $this->escape($this->state->get('list.direction'));
when I use the grid sorting in the columns i use the boilerplate:
<?php echo JHtml::_('grid.sort', 'COM_XXXXXXX', 'a.list_date',
$listDirn, $listOrder); ?>
at the end of the view is the boilerplate:
<input type="hidden" name="filter_order" value="<?php echo
$listOrder; ?>" />
<input type="hidden" name="filter_order_Dir" value="<?php echo
$listDirn; ?>" />
my model has the following boilerplate in 'getListQuery'
$orderCol = $this->state->get('list.ordering');
$orderDirn = $this->state->get('list.direction');
$query->order($db->getEscaped($orderCol.' '.$orderDirn));
For me to get it work work, in my model I added the following to
'populateState':
$filter_order = JRequest::getCmd('filter_order');
$filter_order_Dir = JRequest::getCmd('filter_order_Dir');
$this->setState('filter_order', $filter_order);
$this->setState('filter_order_Dir', $filter_order_Dir);
---
And then in my view, I make a request for those two state variables.
$this->listDirn = $state->get('filter_order_Dir');
$this->listOrder = $state->get('filter_order');
--
I then reference $this->listDirn and $this->listOrder in the html for
the grid headers.
*** Can someone please clarify how the built-in joomla components
manage to do this by simply doing the following in the view, and no
specific ordering logic exists in the 'populateState' ****
$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn = $this->escape($this->state->get('list.direction'));
Basically, what you need is the __construct, getListQuery, and
populateState methods. The constructor is where you build the array of
sortable fields. populateState should be where you set all of your
parameters (as well as the default sort order). getListQuery is where
you'll build your query to get the data, and here you'll work primarily
work with just your query object and the state parameters that were set in
populateState.
>--
>You received this message because you are subscribed to the Google Groups
>"Joomla! General Development" group.
>To post to this group, send an email to
>joomla-de...@googlegroups.com.
>To unsubscribe from this group, send email to
>joomla-dev-gene...@googlegroups.com.
>For more options, visit this group at
>http://groups.google.com/group/joomla-dev-general?hl=en-GB.
>
Found my problem....more of a PHP goof on my part.
I followed everything to a "T", except that my default.php for the
view pulls in templates for the head/body/footer. The $listDirn and
$listOrder were declare in default.php, and went out of scope in the
header file. I had sheepishly assumed that PHP would give those
variables scope since it is within the same execution context...guess
not....guess that's a good thing :-)
On Dec 15, 7:58 am, Mark Dexter <dextercow...@gmail.com> wrote:
> Make sure you properly filter the order and order direction values.
> These can be used to attack the site if you aren't careful about this.
> For the order direction, it's easy. Just limit to ASC or DESC. For the
> order values, we build an array in the constructor and then make sure
> the value is in the array. Mark
>
>
>
>
>
>
>
> On Thu, Dec 15, 2011 at 12:23 AM, Michael Babker <mbab...@flbab.com> wrote:
> > Best way I can do this is to link you to one of my own backend models, so
> > look here:
> >https://github.com/mbabker/Podcast-Manager/blob/2.0-wip/com_podcastma...
> > admin/models/podcasts.php
>
> > Basically, what you need is the __construct, getListQuery, and
> > populateState methods. The constructor is where you build the array of
> > sortable fields. populateState should be where you set all of your
> > parameters (as well as the default sort order). getListQuery is where
> > you'll build your query to get the data, and here you'll work primarily
> > work with just your query object and the state parameters that were set in
> > populateState.
>