⚠ 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

custom template loading by ajax



zrmba
  • profile picture
  • Member

Posted 13 May 2013 - 11:47 AM

Hi guys!

I'm experiencing a behaviour I would like to manage with your help.

 

I have in my class two methods like this:

class MyClass extends CI_Controller {
....

function MyMethod1()
{

}
function MyMethod2()
{

}
}

MyMethod2 is called within MyMethod1 by jquery ajax onchange action on a dropdown. But it loads MyMethod2 with the grid. But I just  want MyMethod2 to make sql query and produce custom html. I have tried to create a custom template which does not have the common

<?php echo $output; ?>

but it does not work, so how to load custm template for MyMethod2?

 

Thanks


davidoster
  • profile picture
  • Member

Posted 13 May 2013 - 12:35 PM

So, let's overview what you do.

Your MyMethod1 within the view file (or at the outputed html code) has a jquery $.ajax function that calls MyMethod2. Right?

Now MyMethod2 must have each own view file and the output of this view file must be replacing something within your original view file, e.g.

/* sample javascript */
if(data){ container.html(data); } else { container.html(''); }

Now strictly speaking this is not related to Grocery CRUD. If you're trying to do something like this within a Grocery CRUD view then this is a different story and you should post your actual code so we can help you out.


victor
  • profile picture
  • Member

Posted 13 May 2013 - 12:58 PM

show your code. I think you have a wrong path to function.

zrmba
  • profile picture
  • Member

Posted 15 May 2013 - 01:55 AM

Ok guys thanks for your sollicitude.

well, I have a template derived from the comon one in the distribution: here is the code where i added my jquery code:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
<?php 
foreach($css_files as $file): ?>
	<link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
<?php endforeach; ?>
<?php foreach($js_files as $file): ?>
	<script src="<?php echo $file; ?>"></script>
<?php endforeach; ?>
<script type="text/javascript">
	$(document).ready(function() {
		//$('#etatpayement_field_box').hide();
		$('#idtype_field_box').append('<div id="champstype" style="border: 1px solid #000;"> Selectionez un type de facture</div>');
		$('#idtype_field_box').change(function(){
		  var typef;
		  typef = $('#field-idtype').val();
		  if (typef == ""){
			$('#champstype').html('Selectionez un type de facture');
		  }else{
			$.ajax({
			  'url' : 'creances/champsupp',
			  'type': 'POST',
			  //'dataType': 'json',
			  'data': {'idtype':typef},
			  'success': function(response){
				 // okay, here you need to write a script to change the DOM, but for now I'll just show it in javascript console
				 //console.log(response);
				 $('#champstype').html(response);
			  }
		    });
			//$('#champstype').html(typef);
		  }
		  
		});
		$('#idclient_input_box').append('<a href="<?php echo site_url()?>/creances/quick_add/" class="various fancybox.ajax">Ajouter</a>');
	});
</script>
<style type='text/css'>
body
{
	font-family: Arial;
	font-size: 14px;
}
a {
    color: blue;
    text-decoration: none;
    font-size: 14px;
}
a:hover
{
	text-decoration: underline;
}
</style>
</head>
<body>
	
	<div style='height:20px;'></div>  
    <div>
		<?php echo $output; ?>
    </div>
</body>
</html>

The in y controller I have two methods where I want to load two different views.

function _creance_output($output = null)
    {
        $this->load->view('creance.php',$output);    
    }

function factures()
	{
		$crud = new grocery_CRUD();
		
		$crud->set_table('factures');
		$crud->set_subject('Factures');
		$crud->set_relation('idtype', 'types', 'designation');
		$crud->field_type('etatpayement','enum',array('En cours','En retard', 'Recl&eacturee'));
		$crud->set_relation('idclient', 'clients', 'designation');
		$crud->field_type('active','enum',array('yes','no'));
		$output = $crud->render();

		$this->_creance_output($output);
	}

when adding an item here, there is a field where the previous:

$('#idtype_field_box').change(function(){

i addes tothe view applies. I mean when the select box changes it is supposed to make an ajax request within the:

'url' : 'creances/champsupp',

here is that method:

function champsupp()
	{
		$this->data['typefact'] = $_POST['idtype'];
		$this->load->view('creance2.php', $this->data);
	}

At this stage it didn't work! but with the victor's suggestion i changed the path atthe ajax query like this

 'url' : '<?php echo site_url()?>/creances/champsupp',

And it  finally works, hope it will help.

 

Thank you guys!