Question

i follow the guide in Grocery CRUD web documentation http://www.grocerycrud.com/examples/callback_edit_field_example

i guess this is one of the ways to set default value for your input field when insert. I am trying to set the default value for date input field by using the same method. The date value did appear in the field but it the jquery date picker function is not available anymore.

did any one tried to set the default value for date in Grocery CRUD but still remain its jquery datepicker function ?

thanks in advanced!

Was it helpful?

Solution 3

Thanks John!

I am looking for default date value appear in the input form when in the add input page. I made some changes on your example

$crud = new grocery_CRUD();
....

if( $crud->getState() == 'add' ) { //add these only in add form
    $crud->set_css('assets/grocery_crud/css/ui/simple/'.grocery_CRUD::JQUERY_UI_CSS);
    $crud->set_js('assets/grocery_crud/js/jquery_plugins/config/jquery.datepicker.config.js');
}

$crud->callback_add_field('date',array($this,'_add_default_date_value'));

....
$output = $crud->render();

function _add_default_date_value(){
        $value = !empty($value) ? $value : date("d/m/Y");
        $return = '<input type="text" name="date" value="'.$value.'" class="datepicker-input" /> ';
        $return .= '<a class="datepicker-input-clear" tabindex="-1">Clear</a> (dd/mm/yyyy)';
        return $return;
}

It works!

OTHER TIPS

I found an easy way to do this with a non-editable default value was

$this->grocery_crud->callback_add_field('BookedDate', function(){ return date('y-m-d'); });

This will set todays date on a date field, But this is non editable.

This functionality doesn't actually exist in grocery CRUD yet. So we will try with a small "hack" to have the expected results. The below code will work fine for you:

$crud = new grocery_CRUD();
....

if( $crud->getState() == 'edit' ) { //add these only in edit form
    $crud->set_css('assets/grocery_crud/css/ui/simple/'.grocery_CRUD::JQUERY_UI_CSS);
    $crud->set_js_lib('assets/grocery_crud/js/'.grocery_CRUD::JQUERY);
    $crud->set_js_lib('assets/grocery_crud/js/jquery_plugins/ui/'.grocery_CRUD::JQUERY_UI_JS);
    $crud->set_js_config('assets/grocery_crud/js/jquery_plugins/config/jquery.datepicker.config.js');
}

$crud->callback_edit_field('phone',array($this,'_add_default_date_value'));

....
$output = $crud->render();
....


function _add_default_date_value($value){
    //The below line is only to avoid the error in JavaScript
    $return  = '<script type="text/javascript">var js_date_format = "dd/mm/yyyy"; </script>';

    $value = !empty($value) ? $value : date("d/m/Y");
    return $return.'<input type="text" name="phone" value="'.$value.'" class="datepicker-input" />';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top