⚠ 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

adding multiple items and fields



maru
  • profile picture
  • Member

Posted 12 April 2016 - 22:42 PM

Hi! 

I am trying to add multiple items (products) and generate a field by each item to let me add a quantity.

I was searching for examples on internet but I'm stuck since several days with this, hope can help me.

 

Tahnks in advance! :)

 

 

Here is my code

<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class DocumentosElab extends CI_Controller {

	function __construct() 
	{
		
		parent::__construct();		
		$this->template->set_layout('layout.php');
		$this->load->database();
		$this->load->library('grocery_crud');
		$this->load->helper('url');
		$this->load->library(array('session'));
                $this->load->model(array('CI_auth', 'CI_menu'));
                $this->load->helper(array('html','url'));
	}

	function index() 
	{
	
	
	}

	
	
	function documentoselab_v()
	{
	
		if($this->CI_auth->check_logged()===FALSE)
		   redirect(base_url().'index.php/login/');
		else{
				$data['title'] = 'Area Restringida';
				$data['menu_top'] = $this->CI_menu->menu_top();
				$data['body'] = '<div style="padding:10px;">Usuario logueado</div>';
				
				echo $this->input->post('username');
				
				$this->load->view('_output_html', $data);
		}
		
		$this->load->view('layouts/header');
		$this->load->view('layouts/menu');
		
		try{

			$crud = new grocery_CRUD();
			$crud->set_theme('datatables');
			$crud->set_table('documentos');			
			$crud->set_subject('documentos');
			$crud->set_language('spanish');

			$crud->required_fields(
				'IdDocumento',				
				'IdProveedor'											
			);

			$crud->columns(
				'IdProveedor',	
				'DocFecha'
			);
			

			$crud->unset_fields('DocCompr');//, 'hidden', 0); //deshabilitado
			$crud->field_type('DocPuesto', 'hidden', 0); //escondido
			$crud->field_type('DocNumero', 'hidden', 0);
			$crud->field_type('DocFechaAlta', 'hidden', 0);
			$crud->field_type('DocPc', 'hidden', 0);
			$crud->field_type('DocFecha', 'hidden', 0);
			$crud->field_type('DocObservacion', 'hidden', 0);
			$crud->field_type('DocUsuario', 'hidden', 0);
			
			$crud->display_as('IdProveedor','Proveedor');
			$crud->display_as('DocFecha','Fecha');
			$crud->display_as('DocObservacion','Observación');
			
			$crud->set_relation('IdProveedor','proveedores', 'ProvRSocial');
                        
                        // I get multiple items here but I think I'm going to need something different for what I need 
			$crud->set_relation_n_n('Productos', 'docproductos', 'productos', 'IdDocumento', 'IdProducto', 'ProdDescripcion', 'priority');
			
			$output = $crud->render();
			
                        // and here I can generate a field but I need one by item
			$crud->add_fields('my_field');
                        $crud->change_field_type('my_field', 'text');
                        $crud->callback_edit_field('my_field',array($this,'callback_my_field'));

			function callback_my_field()
			{
				return '+30 <input type="text" maxlength="50" value="cantidad" name="Cantidad" style="width:462px">';
			}
			
			
			
			$this->load->view('documentoselab/documentoselab_v', $output);
			
		}catch(Exception $e){
			
			show_error($e->getMessage().' --- '.$e->getTraceAsString());
		}
		
		$this->load->view('layouts/footer');
	}
}
?>

Amit Shah
  • profile picture
  • Member

Posted 12 May 2016 - 05:39 AM

Hi there,

 

sorry i dont think this way you will be able to achieve what you wish to...

There are 2 ways .. i had implemented a similar solution in past....

1 - By default create a new field (... add a callback.. add a div ...and generate 2 fields .. 1 product download for selection and other quantity... 3rd element in the row - make it a button that will have a callback.)

Now this button that have a callback - when clicked, will add additional row inside the div itself. This simple process will allow you to generate as many rows as u want.

On save - simply use a callback_insert ... where u will manage the insertion the way you want to.

 

Of course u can also add a remove button that will allow the user to remove unwanted row...

 

As for Update - what you can do is.. generate the same number of rows with similar set of fields / buttons to allow user to handle the updates of the records.

Also you have make a callback_update ..

now here is a tricky part -  need to 1st compare the existing list of entries with the new list of entries...

if the 1 existing in db ... but not in list - remove the same

post that - do a reverse comparison - if we find and fresh entry that dose not exists - add the same to the db...

 

 

2nd option is something a bit different - while adding - generate all the multiple fields with all the products and quantity ... each row with checkbox - if interested to add / set - select the checbox - (on checked - enable the same .. else disable the same)

This seems to be simpler solution - but the same may not be a feasible solution in case if there are many records to be managed.

 

take a solution that derives best to you - 

 

Happy GCing :)