Component Creator i domysle sortowanie wyników
Wyniki 1 do 8 z 8

Temat: Component Creator i domysle sortowanie wyników

  1. #1

    Joomla! 3.5 Component Creator i domysle sortowanie wyników

    Nie potrafię ustawić aby domyślny ordering po wejściu w komponent wchodził mi w sortowanie po:


    Kod PHP:
    parent::populateState('a.data_premiery''ASC'); 
    Mimo kombinowania ciągle nie śmiga - co robię źle? Fragment kodu w models poniżej.

    Kod PHP:
    <?php

    /**
     * @version    CVS: 1.5.2
     * @package    Com_Ksiazki
     * @author     Ciubas <biuro@mappo.pl>
     * @copyright  2017 Ciubas
     * @license    GNU General Public License v2 lub późniejsza; zobacz LICENSE.txt
     */
    defined('_JEXEC') or die;

    jimport('joomla.application.component.modellist');

    /**
     * Methods supporting a list of Ksiazki records.
     *
     * @since  1.6
     */
    class KsiazkiModelKsiazki extends JModelList
    {
        
    /**
         * Constructor.
         *
         * @param   array  $config  An optional associative array of configuration settings.
         *
         * @see        JController
         * @since      1.6
         */
        
    public function __construct($config = array())
        {
            if (empty(
    $config['filter_fields']))
            {
                
    $config['filter_fields'] = array(
                    
    'id''a.id',
                    
    'ordering''a.ordering',
                    
    'state''a.state',
                    
    'created_by''a.created_by',
                    
    'modified_by''a.modified_by',
                    
    'tytul''a.tytul',
                    
    'autor''a.autor',
                    
    'okladka''a.okladka',
                    
    'isbn''a.isbn',
                    
    'format''a.format',
                    
    'cena''a.cena',
                    
    'sprzedaz''a.sprzedaz',
                    
    'data_premiery''a.data_premiery',
                    
    'opis_krotki''a.opis_krotki',
                    
    'opis_dlugi''a.opis_dlugi',
                    
    'recenzje''a.recenzje',
                    
    'patronaty''a.patronaty',
                    
    'link_papier_g''a.link_papier_g',
                    
    'link_papier''a.link_papier',
                    
    'link_ebook_g''a.link_ebook_g',
                    
    'link_ebook''a.link_ebook',
                    
    'link_audio_g''a.link_audio_g',
                    
    'link_audio''a.link_audio',
                );
            }

            
    parent::__construct($config);
        }

        
    /**
         * Method to auto-populate the model state.
         *
         * Note. Calling getState in this method will result in recursion.
         *
         * @param   string  $ordering   Elements order
         * @param   string  $direction  Order direction
         *
         * @return void
         *
         * @throws Exception
         *
         * @since    1.6
         */
        
    protected function populateState($ordering null$direction null)
        {


            
    $app  JFactory::getApplication();
            
    $list $app->getUserState($this->context '.list');



            
    $ordering  = isset($list['filter_order'])     ? $list['filter_order']     : null;
            
    $direction = isset($list['filter_order_Dir']) ? $list['filter_order_Dir'] : null;

            
    $list['limit']     = (int) JFactory::getConfig()->get('list_limit'20);
            
    $list['start']     = $app->input->getInt('start'0);
            
    $list['ordering']  = $ordering;
            
    $list['direction'] = $direction;

            
    $app->setUserState($this->context '.list'$list);
            
    $app->input->set('list'null);

            
    // List state information.
            
    parent::populateState($ordering$direction);

         
    //    parent::populateState('data_premiery', 'ASC'); //sortowanie po dacie premiery!

            
    $app JFactory::getApplication();

            
    $ordering  $app->getUserStateFromRequest($this->context '.ordercol''filter_order'$ordering);
            
    $direction $app->getUserStateFromRequest($this->context '.orderdirn''filter_order_Dir'$ordering);

            
    $this->setState('list.ordering'$ordering);
            
    $this->setState('list.direction'$direction);

            
    $start $app->getUserStateFromRequest($this->context '.limitstart''limitstart'0'int');
            
    $limit $app->getUserStateFromRequest($this->context '.limit''limit'0'int');

            if (
    $limit == 0)
            {
                
    $limit $app->get('list_limit'0);
            }

            
    $this->setState('list.limit'$limit);
            
    $this->setState('list.start'$start);


        }

  2. Pani Reklamowa
    Pani Reklamowa jest aktywna
    Avatar Panny Google

    Dołączył
    19-08-2010
    Skąd
    Internet
    Postów
    milion
    Pochwał
    setki
  3. #2
    Nowicjusz
    Dołączył
    25-12-2012
    Wpisy
    20
    Punkty
    3

    Domyślny

    Ordering jest dokonywany w getListQuery(). To co tutaj jest to obsługa sortowania wywoływana z interfejsu użytkownika.
    Jeśli chcesz na sztywno to na sztywno zrobić to w populateState dodaj $query->order('nazwapola kierunek') gdzie kierunek to ASC lub DESC.

    W Twoim kodzie za przekazanie sortowania do getListQuery odpowiada:

    parent:: populateState($ordering, $direction);

    Inicjowane masz to wcześniej:

    $ordering = isset($list['filter_order']) ? $list['filter_order'] : null;
    $direction = isset($list['filter_order_Dir']) ? $list['filter_order_Dir'] : null;

    Jeśli null'e pozamieniasz odpowiednio na pole (nie wiem jak getListQuery wygląda ale np. a.id) i na kierunek (ASC lub DESC) to domyślny widok będzie z sortowaniem jakim będziesz chciał:

    $ordering = isset($list['filter_order']) ? $list['filter_order'] : 'a.id';
    $direction = isset($list['filter_order_Dir']) ? $list['filter_order_Dir'] : 'asc';



    Oczywiście w getListQuery() musi znajdować się $query->order.

  4. #3

    Domyślny

    Hej,

    obecnie mam jak poniżej i jest super ale jak użyję przełączania przez przyciski (1,2,3,4) strony to sortowanie się znowu pierdzieli. Totalnie nie potrafię ustawić aby domyślnie sortował właśnie po: data_premiery

    Kod PHP:
    <?php
    /** * @version    CVS: 1.5.2 * @package    Com_Ksiazki * @author     Ciubas <biuro@mappo.pl> * @copyright  2017 Ciubas * @license    GNU General Public License v2 lub późniejsza; zobacz LICENSE.txt */defined('_JEXEC') or die;
    jimport('joomla.application.component.modellist');
    /** * Methods supporting a list of Ksiazki records. * * @since  1.6 */class KsiazkiModelKsiazki extends JModelList{    /**     * Constructor.     *     * @param   array  $config  An optional associative array of configuration settings.     *     * @see        JController     * @since      1.6     */    public function __construct($config = array())    {        if (empty($config['filter_fields']))        {            $config['filter_fields'] = array(                'data_premiery''a.data_premiery',                'ordering',                'id''a.id',                 'a.ordering',                'state''a.state',                'created_by''a.created_by',                'modified_by''a.modified_by',                'tytul''a.tytul',                'autor''a.autor',                'okladka''a.okladka',                'isbn''a.isbn',                'format''a.format',                'cena''a.cena',                'sprzedaz''a.sprzedaz',                'opis_krotki''a.opis_krotki',                'opis_dlugi''a.opis_dlugi',                'recenzje''a.recenzje',                'patronaty''a.patronaty',                'link_papier_g''a.link_papier_g',                'link_papier''a.link_papier',                'link_ebook_g''a.link_ebook_g',                'link_ebook''a.link_ebook',                'link_audio_g''a.link_audio_g',                'link_audio''a.link_audio',            );        }
            
    parent::__construct($config);    }
        
    /**     * Method to auto-populate the model state.     *     * Note. Calling getState in this method will result in recursion.     *     * @param   string  $ordering   Elements order     * @param   string  $direction  Order direction     *     * @return void     *     * @throws Exception     *     * @since    1.6     */    protected function populateState($ordering null$direction null)    {

            
    $app  JFactory::getApplication();        $list $app->getUserState($this->context '.list');


            
    $ordering  = isset($list['filter_order'])     ? $list['filter_order']     : null;        $direction = isset($list['filter_order_Dir']) ? $list['filter_order_Dir'] : null;
            
    $list['limit']     = (int) JFactory::getConfig()->get('list_limit'20);        $list['start']     = $app->input->getInt('start'0);        $list['ordering']  = $ordering;        $list['direction'] = $direction;
            
    $app->setUserState($this->context '.list'$list);        $app->input->set('list'null);
            
    // List state information.        parent::populateState($ordering, $direction);
         //    parent::populateState('data_premiery', 'ASC'); //sortowanie po dacie premiery!
            
    $app JFactory::getApplication();
            
    $ordering  $app->getUserStateFromRequest($this->context '.ordercol''filter_order'$ordering);        $direction $app->getUserStateFromRequest($this->context '.orderdirn''filter_order_Dir'$ordering);
            
    $this->setState('list.ordering'$ordering);        $this->setState('list.direction'$direction);
            
    $start $app->getUserStateFromRequest($this->context '.limitstart''limitstart'0'int');        $limit $app->getUserStateFromRequest($this->context '.limit''limit'0'int');
            if (
    $limit == 0)        {            $limit $app->get('list_limit'0);        }
            
    $this->setState('list.limit'$limit);        $this->setState('list.start'$start);

        }
        
    /**     * Build an SQL query to load the list data.     *     * @return   JDatabaseQuery     *     * @since    1.6     */    protected function getListQuery()    {        // Create a new query object.        $db    = $this->getDbo();        $query = $db->getQuery(true);
            // Select the required fields from the table.        $query            ->select(                $this->getState(                    'list.select', 'DISTINCT a.*'                )            );
            
    $query->from('`#__ksiazki` AS a');
            
    // Join over the users for the checked out user.        $query->select('uc.name AS uEditor');        $query->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');
            // Join over the created by field 'created_by'        $query->join('LEFT', '#__users AS created_by ON created_by.id = a.created_by');
            // Join over the created by field 'modified_by'        $query->join('LEFT', '#__users AS modified_by ON modified_by.id = a.modified_by');        // Join over the foreign key 'autor'        $query->select('`#__users_2695087`.`name` AS users_fk_value_2695087');        $query->join('LEFT', '#__users AS #__users_2695087 ON #__users_2695087.`id` = a.`autor`');
            
    if (!JFactory::getUser()->authorise('core.edit''com_ksiazki'))        {            $query->where('a.state = 1');        }
            
    // Filter by search in title        $search = $this->getState('filter.search');
            
    if (!empty($search))        {            if (stripos($search'id:') === 0)            {                $query->where('a.id = ' . (int) substr($search3));            }            else            {                $search $db->Quote('%' $db->escape($searchtrue) . '%');                $query->where('( a.tytul LIKE ' $search '  OR #__users_2695087.name LIKE ' $search '  OR  a.isbn LIKE ' $search ' )');            }        }

            
    // Add the list ordering clause.        $orderCol  = $this->state->get('list.ordering', 'data_premiery');        $orderDirn = $this->state->get('list.direction', 'DESC');
            
    if ($orderCol && $orderDirn)        {            $query->order($db->escape($orderCol ' ' $orderDirn));        }
            return 
    $query;    }
        
    /**     * Method to get an array of data items     *     * @return  mixed An array of data on success, false on failure.     */    public function getItems()    {        $items parent::getItems();
            foreach (
    $items as $item)        {            if (isset($item->autor) && $item->autor != '')            {                if (is_object($item->autor))                {                    $item->autor = \Joomla\Utilities\ArrayHelper::fromObject($item->autor);                }
                    
    $values = (is_array($item->autor)) ? $item->autor explode(','$item->autor);                $textValue = array();
                    foreach (
    $values as $value)                {                    $db JFactory::getDbo();                    $query $db->getQuery(true);                    $query                            ->select('`#__users_2695087`.`name`')                            ->from($db->quoteName('#__users''#__users_2695087'))                        ->where($db->quoteName('id') . ' = ' $db->quote($db->escape($value)));                    $db->setQuery($query);                    $results $db->loadObject();
                        if (
    $results)                    {                        $textValue[] = $results->name;                    }                }
                    
    $item->autor = !empty($textValue) ? implode(', '$textValue) : $item->autor;            }
                        
    $item->sprzedaz JText::_('COM_KSIAZKI_KSIAZKI_SPRZEDAZ_OPTION_' strtoupper($item->sprzedaz));        }
            return 
    $items;    }
        
    /**     * Overrides the default function to check Date fields format, identified by     * "_dateformat" suffix, and erases the field if it's not correct.     *     * @return void     */    protected function loadFormData()    {        $app              JFactory::getApplication();        $filters          $app->getUserState($this->context '.filter', array());        $error_dateformat false;
            foreach (
    $filters as $key => $value)        {            if (strpos($key'_dateformat') && !empty($value) && $this->isValidDate($value) == null)            {                $filters[$key]    = '';                $error_dateformat true;            }        }
            if (
    $error_dateformat)        {            $app->enqueueMessage(JText::_("COM_KSIAZKI_SEARCH_FILTER_DATE_FORMAT"), "warning");            $app->setUserState($this->context '.filter'$filters);        }
            return 
    parent::loadFormData();    }
        
    /**     * Checks if a given date is valid and in a specified format (YYYY-MM-DD)     *     * @param   string  $date  Date to be checked     *     * @return bool     */    private function isValidDate($date)    {        $date str_replace('/''-'$date);        return (date_create($date)) ? JFactory::getDate($date)->format("Y-m-d") : null;    }}

  5. #4
    Nowicjusz
    Dołączył
    25-12-2012
    Wpisy
    20
    Punkty
    3

    Domyślny

    Nie będę tego analizował, bo się nie da.

    Po prostu zamiast:
    $ordering = isset($list['filter_order']) ? $list['filter_order'] : null;
    $direction = isset($list['filter_order_Dir']) ? $list['filter_order_Dir'] : null;

    ma być:


    $ordering = isset($list['filter_order']) ? $list['filter_order'] : 'a.data_premiery';
    $direction = isset($list['filter_order_Dir']) ? $list['filter_order_Dir'] : 'asc';

    Jeśli w międzyczasie nie wciśniesz sortowania po którejś z kolumn, to domyślnie będzie Ci sortował po dacie premiery.

  6. #5

    Domyślny

    Wpisywałem zamiast null co napisałeś i wyskakiwał błąd jak włączałem sortowanie.

    Chcę aby wszystko działało ale domyślnie aby sortował właśnie po 'a.data_premiery' działa mi to na dzień dobry prawidłowo ale jak już kliknę w liczbę to się przełącza.

    Możesz to zobaczyć na stronie: http://www.szaragodzina.pl/ksiazki/katalog.html

    Klikasz na dole 2 a potem 1 i znowu sortuje po ID - nie potrafię tego ogarnąć.

    Gdyby chodziło tylko o sortowanie po dacie to już by działało - jak widzisz w nowościach czy też zapowiedziach.

  7. #6
    Nowicjusz
    Dołączył
    25-12-2012
    Wpisy
    20
    Punkty
    3

    Domyślny

    Do tego co napisałem:

    odkomentować
    // parent:: populateState('data_premiery', 'ASC'); //sortowanie po dacie premiery!
    i zmienić na (oczywiście bez spacji przed populaterState)

    parent:: populateState($ordering, $direction);

    zakomentować drugie wywołanie:
    $app = JFactory::getApplication();

    poprawić
    $direction = $app->getUserStateFromRequest($this->context . '.orderdirn', 'filter_order_Dir', $ordering);
    na
    $direction = $app->getUserStateFromRequest($this->context . '.orderdirn', 'filter_order_Dir', $direction);

  8. #7

    Domyślny

    Bardzo dziękuję za pomoc, wszystko śmiga idealnie!

  9. #8
    Nowicjusz
    Dołączył
    25-12-2012
    Wpisy
    20
    Punkty
    3

    Domyślny

    Bardzo proszę

Podobne tematy

  1. Template Creator CK
    przez alex51 na forum Programowanie pod Joomla!
    Odpowiedzi: 14
    Ostatni post/autor: 14-01-2014, 18:10
  2. tabela wyników
    przez qoryto na forum Szukam dodatku do Joomla
    Odpowiedzi: 3
    Ostatni post/autor: 23-03-2010, 20:23
  3. [poszukuje] Theme creator
    przez S!wy na forum Różne
    Odpowiedzi: 2
    Ostatni post/autor: 05-07-2009, 15:37
  4. Typowanie Wyników
    przez Krajan na forum Administracja składnikami
    Odpowiedzi: 12
    Ostatni post/autor: 06-04-2006, 02:30

Reguły pisania

  • Nie możesz zakładać nowych tematów
  • Nie możesz dodawać wypowiedzi
  • Nie możesz dodawać załączników
  • Nie możesz poprawiać swoich postów
  •