pre-filling fields
- Single Page
Posted 10 April 2012 - 09:27 AM
This is my first topic in this forum. it's been a pleasure to find this comunity, i'd like to thank to the gcroud's author and all the contributors.
This is my question:
I'm tring to fill a field in the add state, i'll try to describe the situation:
- I have a customers table and a reports table: 1 customers have lot of records, so i have a idcustomer in the reports table.
- and i've added an action to view all de reports for each customers.
function reports_customer_link($primary_key,$row){
return site_url('fts/reports_customers/'.$primary_key);
}
- the action simply is a callback to the reports table doing a where filter,
function reports_customer($idcliente,$op){
$crud = new grocery_CRUD();
$crud->set_subject('Ordenes cliente');
$crud->where('idcliente',$idcliente);
$crud->set_table('orden');
$output = $crud->render();
$this->_example_output($output);
}
- I realize that if you add an $op parameter, you can capture the add operation, because the and it is posible, to add records for this customer, bus it will be necesary to pre-fill the field idcustomer, and hide it.
That is: the add record button generates the url " .../reports_customer/3/add " so I understand that i could do something like this:
function reports_customer($idcliente,$op){
$crud = new grocery_CRUD();
$crud->where('idcliente',$idcliente);
$crud->set_table('reports');
/*
PRE-FILL idcustomer field
*/
$output = $crud->render();
$this->_example_output($output);
}
well.. sorry about this long words, i'd like to ensure i explain the problem correctly, and my english is not very good.
Maybe there is another way and easiest to do the same.. any suggestion will be thankfull
regards
Posted 10 April 2012 - 10:17 AM
IF you want to prefill all the add statements - you can do it by callback_add_field() or callback_edit_field() - make a function that returns the proper html of the form + value
.... something like this:
$crud->callback_edit_field('plan_id', array($this, '_plan_column_callback'));
/**
* gets all the plans and generates proper html dropdown select
* @return html-dropdown
*/
function _plan_column_callback($info = NULL) {
//get the subcats
$query = $this->db->get('plan');
$result = $query->result();
$field = "<div class='form-input-box' id=\"plan_id_input_box\">";
$field .= "<select name='plan_id' id='' class='chosen-select' data-placeholder='plan name / plan_percent / plan credit limit name' style='width:300px'><option value=''> </option>";
foreach ($result as $row):
$field .= "<option value='" . $row->plan_id . "'";
if ($info == $row->plan_id) {
$field .= " selected=selected ";
}
$field .=">" . $row->plan_name . ": " . $row->plan_percent . " " . $row->plan_credit_limit . "</option>";
endforeach;
$field .= "</select></div>";
return($field);
}
IF you want to do it for a custom action button - it is not possible at the moment, atleast that's what i got like an answer when i asked the question - I need something like it but there are no callbacks for the custom actions .
Posted 10 April 2012 - 10:56 AM
I've simplified the code. I just added add_action and then a controller's method showing the detail table filter by idcustomer.
Until that, everything is ok. , Now i see an add record button (in the detail table), that it generated this url: controller/method/key/add
So, It would be perfect to be able to force the detail add form to has the 'idcustomer' = key. and then hide it.
Doing that, we get that if we add a detail report for one customer, coming from the filter table, obiously is not necesary to choose the customer...
this is my code for now:
function reports_customer($idcustomer,$op = ''){
$crud = new grocery_CRUD();
$customername= $this->db->get_where('customer',array('id' => $idcustomer))->row()->nombre;
$crud->set_subject('Customer: '.$customername);
$crud->where('idcliente',$idcustomer);
$crud->set_table('reports');
if ($op == 'add'){
//inserting new report for this customer
//can we force the idcustomer field for this report?????
}
$output = $crud->render();
$this->_example_output($output);
echo $op;
}
thanks a lot
Posted 10 April 2012 - 11:01 AM
$crud->change_field_type('idcustomer', 'hidden', '1');
Posted 10 April 2012 - 11:14 AM
it is working now.
the final source code is:
[php]function reports_customer($idcustomer,$op = ''){
$crud = new grocery_CRUD();
$customername= $this->db->get_where('customer',array('id' => $idcustomer))->row()->nombre;
$crud->set_subject('Customer: '.$customername);
$crud->where('idcliente',$idcustomer);
$crud->set_table('reports');
if ($op == 'add'){
$crud->change_field_type('idcustomer', 'hidden', $idcustomer);
}
$output = $crud->render();
$this->_example_output($output);
}[/php]
thank's a lot xxaxxo
Posted 15 January 2014 - 15:46 PM
I'm dealing with some similar problem.
I want to pre-fill some fields in the form, like the original poster, but still be able to show and edit them in the form. So, the value should only act as a default. Is it possible?
Posted 17 May 2017 - 05:50 AM
Hi there! first of all, thank you for this awesome tool!
I'm dealing with similar problem but the code isn't working for me. I think the if statement is not working properly but i'm not sure. When i'm going to add, the id doesn't appear on its field and the field appears empty. What am I doing wrong?
The code:
public function entregas_lista($idCarga, $op = '') { $crud = new grocery_CRUD(); if ($this->Entregas_Model->get_rows($idCarga)) { $crud->unset_add(); } $datos = array( 'title' => "Solicitudes", 'username' => "Administrador" ); $this->load->view('commons/header', $datos); $crud->set_language("spanish"); $crud->set_theme('flexigrid'); $crud->set_table('entregas'); //$crud->unset_columns(array('accion', 'estado')); $crud->display_as('idCitas', 'Cita'); $crud->display_as('idAcciones', 'Acción'); $crud->display_as('idEstadoSolicitud', 'Estado Solicitud') ->display_as('horaCita', 'Hora Cita') ->display_as('numeroEntrega', 'Nº Entrega') ->display_as('Origen', 'OrÃgen') ->display_as('cargaPrevista', 'Carga Prevista') ->display_as('entregaPrevista', 'Entrega Prevista'); $crud->where('entregas.idCitas =', $idCarga); $crud->display_as('idCarga', 'Nº Entrega'); $crud->set_relation('idCitas', 'citas', 'cita'); $crud->set_relation('idAcciones', 'acciones', 'nombreAccion'); $crud->set_relation('idEstadoSolicitud', 'estadosolicitudes', 'nombreEstado'); $crud->add_fields('idCitas', 'idAcciones', 'idEstadoSolicitud', 'fechaCita', 'horaCita', 'numeroEntrega', 'cliente', 'Origen', 'Destino', 'cargaPrevista', 'entregaPrevista'); $crud->edit_fields('idAcciones', 'idEstadoSolicitud', 'fechaCita', 'horaCita', 'numeroEntrega', 'cliente', 'Origen', 'Destino', 'cargaPrevista', 'entregaPrevista'); if ($op == 'add') { $crud->change_field_type('idCitas', 'hidden', $idCarga); } $output = $crud->render(); $this->_example_output($output); //--------------- Cargo la vista 'commons/footer.php' ------------- / $this->load->view('commons/footer'); }
Posted 17 May 2017 - 06:43 AM
Hi there! first of all, thank you for this awesome tool!
I'm dealing with similar problem but the code isn't working for me. I think the if statement is not working properly but i'm not sure. When i'm going to add, the id doesn't appear on its field and the field appears empty. What am I doing wrong?
The code:
public function entregas_lista($idCarga, $op = '') { $crud = new grocery_CRUD(); if ($this->Entregas_Model->get_rows($idCarga)) { $crud->unset_add(); } $datos = array( 'title' => "Solicitudes", 'username' => "Administrador" ); $this->load->view('commons/header', $datos); $crud->set_language("spanish"); $crud->set_theme('flexigrid'); $crud->set_table('entregas'); //$crud->unset_columns(array('accion', 'estado')); $crud->display_as('idCitas', 'Cita'); $crud->display_as('idAcciones', 'Acción'); $crud->display_as('idEstadoSolicitud', 'Estado Solicitud') ->display_as('horaCita', 'Hora Cita') ->display_as('numeroEntrega', 'Nº Entrega') ->display_as('Origen', 'OrÃgen') ->display_as('cargaPrevista', 'Carga Prevista') ->display_as('entregaPrevista', 'Entrega Prevista'); $crud->where('entregas.idCitas =', $idCarga); $crud->display_as('idCarga', 'Nº Entrega'); $crud->set_relation('idCitas', 'citas', 'cita'); $crud->set_relation('idAcciones', 'acciones', 'nombreAccion'); $crud->set_relation('idEstadoSolicitud', 'estadosolicitudes', 'nombreEstado'); $crud->add_fields('idCitas', 'idAcciones', 'idEstadoSolicitud', 'fechaCita', 'horaCita', 'numeroEntrega', 'cliente', 'Origen', 'Destino', 'cargaPrevista', 'entregaPrevista'); $crud->edit_fields('idAcciones', 'idEstadoSolicitud', 'fechaCita', 'horaCita', 'numeroEntrega', 'cliente', 'Origen', 'Destino', 'cargaPrevista', 'entregaPrevista'); if ($op == 'add') { $crud->change_field_type('idCitas', 'hidden', $idCarga); } $output = $crud->render(); $this->_example_output($output); //--------------- Cargo la vista 'commons/footer.php' ------------- / $this->load->view('commons/footer'); }
Try
if ($crud->getState() == "add") { //do stuff }
Posted 17 May 2017 - 07:01 AM
I've tried the following and it still not working, I don't understand why. (The id field is a dropdown field with various choices built automatically by grocery crud as 1_n relation result).
if ($crud->getState() == "add") { $crud->change_field_type('idCitas', 'hidden', $idCarga); }
This is what appears and don't let me save ('Cita' was a field like 'Acción'):
Posted 17 May 2017 - 07:05 AM
Well, thats because you have a relation on idCita. You should do relation only when you need it. I mean, yo should do relation on "idCita" when $crud->getState() != "add". While you have a relation on a field, you can't hide it.
Posted 17 May 2017 - 07:20 AM
Well, thats because you have a relation on idCita. You should do relation only when you need it. I mean, yo should do relation on "idCita" when $crud->getState() != "add". While you have a relation on a field, you can't hide it.
Thank you very much! i didn't know that. I'm assuming it will work without a relation (normal field).
Posted 20 May 2017 - 11:27 AM
Hello again! I have a new question wich is related with prefilling. Let's say i want to prefill a field wich is not passing id with callback column, for example, in my previous question i am getting the id of cita with callback column and autofilling a field in another table. But this table is composed with set relation betweent both tables (intervalos and citas). So, i spent three days and i'm unable to get the id of intervalohorario, for example, the intervalo (De 7:00 a 8:30) in order to hide its id in the other table too, like cita. How can i do that?
I have been trying hard this way but it doesn't work. Is there any efficient way?
Model function
public function get_idintervalo($idCitas) { $query = $this->db->query('select intervaloshorarios.idIntervaloHorario ' . 'from intervaloshorarios left join citas ' . 'on intervaloshorarios.idIntervaloHorario=citas.idCitas ' . 'where citas.idCitas = '.$idCitas.' ;'); return $query ->result(); }
Controller (not full version)
public function entregas_lista($idCitas) { //Calling model function $query = $this->Intervalos_Model->get_idintervalo($idCitas); $crud->where('entregas.idCitas =', $idCitas); if ($crud->getState() == "add") { //This works $crud->change_field_type('idCitas', 'hidden', $idCitas); //Trying to pass idINtervaloHorario $crud->change_field_type('idIntervaloHorario', 'hidden', $query); } }