Jump to content


Setting Default Value for field_type and set_relation

dropdown set_relation

  • Please log in to reply
10 replies to this topic

#1 chavamm

chavamm

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationMexico

Posted 07 July 2013 - 02:09 AM

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



#2 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 09 July 2013 - 08:43 PM

Thank you Chava .. interesting piece of work, might be helpful for someone for sure.



#3 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 10 July 2013 - 06:59 AM

Thank you for your contribtuion chavamm.

We will let web-johnny about this.

You can always make it a git contribution if you want.

https://github.com/s...is/grocery-crud


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#4 chavamm

chavamm

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationMexico

Posted 10 July 2013 - 01:47 PM

Amit Shah, Davidoster:

 

you're welcome!
It is a pleasure to collaborate

 

Thank you!

 

Best regards!



#5 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 10 July 2013 - 01:55 PM

David, how to contribute directly to the grocery crud? I will like to know.. there are many things that can be integrated with the library.. or as an extension ..  how to contribute the same to the existing library on git?



#6 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 10 July 2013 - 02:00 PM

Well Amit you can clone the development and make changes and push the differences to the master or if it is an extension you can use Zalo's contrib and extend there.


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#7 Charles A.

Charles A.

    Member

  • Members
  • PipPip
  • 17 posts

Posted 24 July 2013 - 10:44 PM

Hi Chava, everything works perfect !! 



#8 superdan

superdan

    Advanced Member

  • Members
  • PipPipPip
  • 41 posts

Posted 19 August 2013 - 09:46 PM

hi chavamm

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 !



#9 chavamm

chavamm

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationMexico

Posted 20 August 2013 - 02:52 PM

hi chavamm

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 !

 

Hi Superdan,

if you have a database table, you could set a relation type with:

for exmple, "countries" database table like: ID, country_name
And your main table with "country_id" field, then the next sentence is fully correct:

 

$this->grocery_crud->set_relation('country_id','countries','country_name',null, null, $default_value = 1); // 1 is default country_id



But,if you have a database table like this: country_name (without ID)

And your main table with "country_name" field, then the next sentence is fully correct:
 

$countriesdb = $this->db->get('countries');
$countries = array();

foreach($countriesdb->result() as $country)
{
    $countries[$country->country_name] = $country->country_name; // This generate, in example: Mexico=>Mexico, España=>España
}

$this->grocery_crud->field_type('country_name','dropdown',$countries, $default_value = 'Mexico');

In theory this should work fine.

I hope that this help you.

Thanks, Regards!


Edited by chavamm, 20 August 2013 - 03:20 PM.


#10 superdan

superdan

    Advanced Member

  • Members
  • PipPipPip
  • 41 posts

Posted 20 August 2013 - 03:21 PM

This helped me a lot!
Thanks so much!

#11 Juanma

Juanma

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 08 October 2019 - 08:36 AM

This works!! Just remember that if you're using $crud instead of $this->grocery_crud you need to change that in order to work!!

 

Thanks you very much!!







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users