⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Sort callback column in a list



bvarlb
  • profile picture
  • Member

Posted 09 April 2013 - 21:17 PM

Hi all,

I look for the tip to sort a list by clicking on a callback column, this column is computed and loaded from another queries included in a model.

example :

list based on table products (id, reference, name, ...)

I want to display a column based on orders - how many products ordered this month / this year, computed in a callback column. I display xx times on xxx.

My display is good but if I click on the column heading, nothing happens, there is no sort.

 

Thanks in advance for your help


davidoster
  • profile picture
  • Member

Posted 09 April 2013 - 22:50 PM

hmmm! it might depend on the content that the column has.

can you make a screen capture in order to understand better?


bvarlb
  • profile picture
  • Member

Posted 10 April 2013 - 13:21 PM

contents of column is '0 times on 3' or '2 times on 2'...


davidoster
  • profile picture
  • Member

Posted 15 April 2013 - 12:09 PM

Probably we need to have a look at your controller's code.

Post it please.


bvarlb
  • profile picture
  • Member

Posted 17 April 2013 - 09:09 AM

Thanks for your reply, the controller code is :

  function circuits()
    {
        if($this->auth->logged_in()==FALSE)
        {
            $redirect = site_url();
      redirect($redirect, "refresh");
        }
        else
        {
          try{
        $data=array();
        $data['title']='Parcours';
        
              $crud = new grocery_CRUD();
 
              $crud->set_subject('Parcours');

              $crud->set_table('circuits');
              $crud->order_by('nom_circuit', 'asc');

              $crud->columns('nom_circuit','total_kms','total_deniv', 'nb_fois', 'profil');

        $crud->required_fields('nom_circuit','total_kms');

              $crud->display_as('nom_circuit','Description parcours')
              ->display_as('total_kms','Nb kms')
              ->display_as('total_deniv','Dénivelé')
              ->display_as('nb_fois','Parcouru')
              ->display_as('profil','');

         $crud->callback_column('nb_fois', array($this, '_parcouru_callback'));
        $crud->callback_column('profil',array($this,'_profil_callback'));
        $crud->add_action('Détailler Parcours', '', '','detail-icon',array($this,'_detail_callback'));
        $output = $crud->render();
        
              $this->_main_output($output, $data);
          }catch(Exception $e){
              show_error($e->getMessage().' --- '.$e->getTraceAsString());
          }
    }
    }        
  function _parcouru_callback($value, $row)
  {
      $nbfois_an=$this->Circuits_model->count_sorties_an($row->id_circuit, date("Y"));
      $nbfois_tot=$this->Circuits_model->count_sorties_tot($row->id_circuit);
      $to_display=$nbfois_an.' fois sur '.$nbfois_tot;
      return '<a href="'.site_url('main/sorties_circuit/'.$row->id_circuit).'" title="Voir les sorties effectuées">'.$to_display.'</a>';
  }
  function _profil_callback($value, $row)
  {
      if ($row->lien=='')
      {
        return '';
      }
      else
      {
        return '<a href="'.site_url('main/parcours_profil/'.$row->id_circuit).'" alt="Profil" title="Profil">'.'<img src="'.base_url().'assets/grocery_crud/themes/perso/css/images/profil.jpg">'.'</a>';
      }
  }

  function _detail_callback($value, $row)
  {
    return site_url('main/detail_parcours').'/'.$row->id_circuit;
  }

and the used tables are defined like this :

CREATE TABLE IF NOT EXISTS `circuits` (
        id_circuit INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY
    ,    nom_circuit VARCHAR(128) NOT NULL
    ,    total_kms DECIMAL(4,1) NOT NULL
    ,    total_deniv INT(4) NOT NULL
  , lien VARCHAR(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `sorties` (
        id_sortie INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY
    , id_circuit INT(10) NOT NULL
    ,    date_sortie DATE NOT NULL
    ,    kms_sortie INT(3) NOT NULL
    ,    temps_sortie TIME NOT NULL
    ,    comments_sortie TEXT
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

Thanks in advance for your help


davidoster
  • profile picture
  • Member

Posted 18 April 2013 - 06:50 AM

Can you help us a little bit here?

Which field (column) are we talking about  and which callback function generates the "xx times on xxx" part you mention?

Cause I can't find it!


bvarlb
  • profile picture
  • Member

Posted 24 April 2013 - 07:09 AM

Sorry for may late answer :

   $crud->callback_column('nb_fois', array($this, '_parcouru_callback'));

  function _parcouru_callback($value, $row)
  {
      $nbfois_an=$this->Circuits_model->count_sorties_an($row->id_circuit, date("Y"));
      $nbfois_tot=$this->Circuits_model->count_sorties_tot($row->id_circuit);
      $to_display=$nbfois_an.' times on '.$nbfois_tot;
      return '<a href="'.site_url('main/sorties_circuit/'.$row->id_circuit).'" title="See the rides">'.$to_display.'</a>';
  }


in the model Circuits_model :

     function count_sorties_an($id, $annee){
    $this->db->where('id_circuit_sortie', $id);
    $this->db->where('year(date_sortie)', $annee);
    $this->db->from('sorties');
        return $this->db->count_all_results();
    }

  function count_sorties_tot($id)
  {
    $this->db->where('id_circuit_sortie', $id);
    $this->db->from('sorties');
        return $this->db->count_all_results();
    }
 

I think the solution is to use Jquery to have an ajax function to sort as I want but I don't know how to do...

 

Thanks for your help


davidoster
  • profile picture
  • Member

Posted 25 April 2013 - 13:37 PM

Check this line:

 

 $this->db->where('year(date_sortie)', $annee);

 

it doesn't seem right to me.