Default value of Grocery CRUD date input?

心已入冬 提交于 2019-12-08 13:14:44

问题


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!


回答1:


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.




回答2:


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" />';
}



回答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!



来源:https://stackoverflow.com/questions/13794157/default-value-of-grocery-crud-date-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!