⚠ 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

Birthdate in add/edit but Age in list



gtya

gtya
  • profile picture
  • Member

Posted 21 March 2017 - 00:38 AM

Hello :)

i would like to display an Age in the main lsit view of my table, while having only a 'birthday' in the database table, and so a Date field in the Edit and AddNew view

 

i am able to change the settings for the list view with the field name "Age" with :

 

    $crud->display_as('birthdate', 'Age')

 

and so compute the age and replace the date, in this column for the "list view" with :

 

    $crud->callback_column('birthdate',array($this,'_callback_birthday'));

 

and so a private method to calculate an age from a date (remember i have birthday date in database)

 

i would like to have this 'Age' name as field in the list view, but then go back to a label like 'Birthdate'

 

so my question : is it possible to change the display_as just for the Add and Edit views ?

 

    public function _callback_birthday($value, $row)
    {
        // i fight a lot because sometimes people write (m-d-Y) but it is not SQL compatible => (yyyy-mm-dd)
        $age = '';
        $happyBirthday = '';
        $dob = $value;
        if(!empty($dob)) {
            $birthdate = new DateTime($dob);
            $today   = new DateTime('today');
            $age = $birthdate->diff($today)->y; // date_diff(date_create($value'), date_create('today'))->y;
            if (date('m-d') == $birthdate->format('m-d')) {
                $happyBirthday = "happy birthday";
            }
        }
        return $age . ' ' . $happyBirthday

    }

 

thank you for your help :)

 


xheradon

xheradon
  • profile picture
  • Member

Posted 21 March 2017 - 07:56 AM

Yes, you can.

if ($crud->getState() == 'add' || $crud->getState() == 'edit') {
      $crud->display_as('birthdate', 'Birthdate')
  } else { 
      $crud->display_as('birthdate', 'Age');
  }

gtya

gtya
  • profile picture
  • Member

Posted 24 March 2017 - 23:00 PM

oh cool :) thank u !