【问题标题】:Default value of Grocery CRUD date input?Grocery CRUD 日期输入的默认值?
【发布时间】:2012-12-10 02:23:29
【问题描述】:

我遵循 Grocery CRUD 网络文档中的指南 http://www.grocerycrud.com/examples/callback_edit_field_example

我猜这是插入时为输入字段设置默认值的方法之一。我正在尝试使用相同的方法设置日期输入字段的默认值。日期值确实出现在该字段中,但 jquery 日期选择器功能不再可用。

是否有人尝试在 Grocery CRUD 中设置日期的默认值但仍保留其 jquery datepicker 功能?

先谢谢了!

【问题讨论】:

    标签: codeigniter jquery-ui-datepicker grocery-crud


    【解决方案1】:

    我找到了一种简单的方法来使用不可编辑的默认值来做到这一点

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

    这将在日期字段上设置今天的日期,但这是不可编辑的。

    【讨论】:

      【解决方案2】:

      这个功能实际上并不存在于杂货店 CRUD 中。因此,我们将尝试使用一个小的“hack”来获得预期的结果。下面的代码对你来说很好:

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

      【讨论】:

      • 谢谢约翰!我对您的代码进行了一些更改,并且可以正常工作!我已经在这篇文章中给出了这个例子作为答案。如果您在代码中发现任何错误,请 cmets。顺便说一句,你对这个问题有什么想法吗? stackoverflow.com/questions/13793871/…
      • 对于图像 CRUD,还没有这样的功能,我不知道有谁有类似的 hack。对不起。
      【解决方案3】:

      谢谢约翰!

      我正在寻找在添加输入页面中出现在输入表单中的默认日期值。我对您的示例进行了一些更改

      $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;
      }
      

      有效!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-28
        • 2015-08-03
        • 1970-01-01
        • 1970-01-01
        • 2020-03-25
        • 2013-02-19
        相关资源
        最近更新 更多