⚠ 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

Select option - default value



William Schoeffel
  • profile picture
  • Member

Posted 25 April 2013 - 13:17 PM

Hi,

 

I have a select/option in my form, this select have 2 options ('Pessoa fisica', 'Pessoa juridica').

 

i would like that when the user add a nem register, the default value selected is 'Pessoa Fisisca'.

 

How i do that with grocery crud and CI?


davidoster
  • profile picture
  • Member

Posted 26 April 2013 - 07:16 AM

This just needs a little bit of CodeIgniter form class knowledge actually!

Excerpt from the CI documentation.

You can use the following code in a callback_field function.

 

 

set_value()

 

Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:

<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />

The above form will show "0" when loaded for the first time.


davidoster
  • profile picture
  • Member

Posted 26 April 2013 - 07:21 AM

For the aspiring ones...

we can set default values on the database and then get those via a simple query on the table,

e.g.

SHOW COLUMNS FROM `activities` WHERE `default` IS NOT NULL

and then use the aforementioned set_value function to update the inputs with the default values.


William Schoeffel
  • profile picture
  • Member

Posted 29 April 2013 - 17:09 PM

This just needs a little bit of CodeIgniter form class knowledge actually!

Excerpt from the CI documentation.

You can use the following code in a callback_field function.

 

 

set_value()

 

Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:

<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />

The above form will show "0" when loaded for the first time.


Can you give me a exemple using select/option?

 

for exemple my select field name is "tipopessoa_id" and the two options are "Pessoa Fisica" and "Pessoa Juridica"

 

and my controller have this code:

 

    public function cliente()
    {
		
		$this->load->database();
        $this->load->helper(array ('url', 'file', 'form'));
      
 
        $this->load->library('grocery_CRUD');
		$this->grocery_crud->set_theme('flexigrid');
		
       $this->grocery_crud->set_table('clientes');
	   
	   $this->grocery_crud->set_relation('tipopessoa_id','tipopessoa','tipopessoa_ds');
	   
	    $this->grocery_crud->display_as('nome', 'Cliente');
		$this->grocery_crud->display_as('email', 'E-mail');
		$this->grocery_crud->display_as('ddd', 'DDD');
		$this->grocery_crud->display_as('endereco', 'Endereço');
		$this->grocery_crud->display_as('tipopessoa_id', 'Tipo');
		$this->grocery_crud->set_subject('Cliente');
	  	   	   
       $output = $this->grocery_crud->render();
 
       $this->_example_output($output); 
    }
	function _example_output($output = null)
 
    {
        $this->load->view('tabela',$output);    
    }

 

i need to create a function callback in the controller?

sorry i'm verry newbie in CI and grocery crud


chavamm
  • profile picture
  • Member

Posted 05 July 2013 - 22:33 PM

Hi,

 

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

    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)
        {
            // modified 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:

public function cliente()
{
        
        $this->load->database();
        $this->load->helper(array ('url', 'file', 'form'));

        $this->load->library('grocery_CRUD');
        $this->grocery_crud->set_theme('flexigrid');
        
        $this->grocery_crud->set_table('clientes');
    
        $this->grocery_crud->set_relation('tipopessoa_id','tipopessoa','tipopessoa_ds',null, null, $default_value = 1); // 1 is default tipopessoa_id
    
        $this->grocery_crud->display_as('nome', 'Cliente');
        $this->grocery_crud->display_as('email', 'E-mail');
        $this->grocery_crud->display_as('ddd', 'DDD');
        $this->grocery_crud->display_as('endereco', 'Endereço');
        $this->grocery_crud->display_as('tipopessoa_id', 'Tipo');
        $this->grocery_crud->set_subject('Cliente');
              
        $output = $this->grocery_crud->render();

        $this->_example_output($output); 
}

function _example_output($output = null)
{
        $this->load->view('tabela',$output); 
}

For Dropdown Type:

 

Modify File library/grocery_crud.php:

On "grocery_CRUD" Class

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'));
        
        // modified 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

public function test()
{
        
        $this->load->library('grocery_CRUD');
        $this->grocery_crud->set_theme('flexigrid');
    
        $this->grocery_crud->field_type('type','dropdown',array('hold'='On Hold', 'done'=>'Done'), $default_value = 'hold');

        /* Your code */
              
        $output = $this->grocery_crud->render();

        $this->_example_output($output);
}

Thanks.

Regards!

--

Chava


superdan
  • profile picture
  • Member

Posted 19 August 2013 - 20:28 PM

hi havamm

nice way to proceed!

but a question.

in this way i have to re-declare every single value of my database via array in the dropdown.

That is not so easy if you have tons of values or if you change them often.

isnt there a way to get all the values of the dropdown from the db?

Thanks !


Amit Shah
  • profile picture
  • Member

Posted 20 August 2013 - 10:15 AM

Hi there

 

Well if you dont plan to change the grocery crud and do the way suggested, then the other way round is write your own script (javascript) Using that u surely can alter.


mnish
  • profile picture
  • Member

Posted 31 August 2013 - 05:53 AM

use callback_before_insert()

 

i.e.

$crud->callback_before_insert(array($this,'add_function'));

 

function add_function ($post_array, $primary_key = NULL)
{
 
 
$post_array['field'] = 'Pessoa Fisisca';
 
return $post_array;
 
}

 

make this field as invisible in main function.