Hello,
hoping all are well, I am happy to share with you the following code.
Please,
Modify file library/grocery_crud.php:
on "grocery_CRUD" Class
public function set_relation($field_name , $related_table, $related_title_field, $where_clause = null, $order_by = null, $default_value=null)
{
$this->relation[$field_name] = array($field_name, $related_table,$related_title_field, $where_clause, $order_by, $default_value,$this->getState());
return $this;
}
Then, on "grocery_CRUD_Layout" class modify the method:
protected function get_relation_input($field_info,$value)
{
$this->set_css($this->default_css_path.'/jquery_plugins/chosen/chosen.css');
$this->set_js($this->default_javascript_path.'/jquery_plugins/jquery.chosen.min.js');
$this->set_js($this->default_javascript_path.'/jquery_plugins/config/jquery.chosen.config.js');
$ajax_limitation = 10000;
$total_rows = $this->get_relation_total_rows($field_info->extras);
//Check if we will use ajax for our queries or just clien-side javascript
$using_ajax = $total_rows > $ajax_limitation ? true : false;
//We will not use it for now. It is not ready yet. Probably we will have this functionality at version 1.4
$using_ajax = false;
//If total rows are more than the limitation, use the ajax plugin
$ajax_or_not_class = $using_ajax ? 'chosen-select' : 'chosen-select';
$this->_inline_js("var ajax_relation_url = '".$this->getAjaxRelationUrl()."';\n");
$select_title = str_replace('{field_display_as}',$field_info->display_as,$this->l('set_relation_title'));
$input = "<select id='field-{$field_info->name}' name='{$field_info->name}' class='$ajax_or_not_class' data-placeholder='$select_title' style='width:300px'>";
$input .= "<option value=''></option>";
if(!$using_ajax)
{
// Added by chava
$state = $field_info->extras[6]; // add|edit| ..etc
$default_value = $field_info->extras[5];
if ($state === 'add')
$value = $default_value;
$options_array = $this->get_relation_array($field_info->extras);
foreach($options_array as $option_value => $option)
{
$selected = !empty($value) && $value == $option_value ? "selected='selected'" : '';
$input .= "<option value='$option_value' $selected >$option</option>";
}
}
elseif(!empty($value) || (is_numeric($value) && $value == '0') ) //If it's ajax then we only need the selected items and not all the items
{
$selected_options_array = $this->get_relation_array($field_info->extras, $value);
foreach($selected_options_array as $option_value => $option)
{
$input .= "<option value='$option_value'selected='selected' >$option</option>";
}
}
$input .= "</select>";
return $input;
}
Finally, on your controller, you should use:
public function users()
{
$this->load->library('grocery_CRUD');
$this->grocery_crud->set_theme('flexigrid');
$this->grocery_crud->set_table('users');
$this->grocery_crud->set_relation('country_id','countries','country_name',null, null, $default_value = 1); // 1 is default country_id
$this->grocery_crud->display_as('country_id', 'Country');
$this->grocery_crud->set_subject('Users');
$output = $this->grocery_crud->render();
$this->_example_output($output);
}
function _example_output($output = null)
{
$this->load->view('myview',$output);
}
For Dropdown Type:
Modify File library/grocery_crud.php:
On "grocery_CRUD" Class, on next method:
public function field_type($field , $type, $extras = null, $default_value = null)
{
if ($type === 'dropdown')
{
$extras[0] = $extras;
$extras[1] = $default_value;
$extras[2] = $this->getState();
}
return $this->change_field_type($field , $type, $extras);
}
Then, on "grocery_CRUD_Layout" Class
protected function get_dropdown_input($field_info,$value)
{
$this->set_css($this->default_css_path.'/jquery_plugins/chosen/chosen.css');
$this->set_js($this->default_javascript_path.'/jquery_plugins/jquery.chosen.min.js');
$this->set_js($this->default_javascript_path.'/jquery_plugins/config/jquery.chosen.config.js');
$select_title = str_replace('{field_display_as}',$field_info->display_as,$this->l('set_relation_title'));
// added by chava
$state = $field_info->extras[2]; // add|edit| ..etc
$default_value = $field_info->extras[1];
if ($state === 'add')
$value = $default_value;
$input = "<select id='field-{$field_info->name}' name='{$field_info->name}' class='chosen-select' data-placeholder='".$select_title."'>";
$options = array('' => '') + $field_info->extras[0];
foreach($options as $option_value => $option_label)
{
$selected = !empty($value) && $value == $option_value ? "selected='selected'" : '';
$input .= "<option value='$option_value' $selected >$option_label</option>";
}
$input .= "</select>";
return $input;
}
Finally, On your controller, you should use:
public function test()
{
$this->load->library('grocery_CRUD');
$this->grocery_crud->set_theme('flexigrid');
$this->grocery_crud->set_table('sales');
$this->grocery_crud->field_type('status','dropdown',array('hold'=>'On Hold', 'done'=>'Done'), $default_value = 'hold');
/* Your code */
$output = $this->grocery_crud->render();
$this->_example_output($output);
}
function _example_output($output = null)
{
$this->load->view('myview',$output);
}
Thanks.
Regards!
--
Chava
