Jump to content

custom template loading by ajax

templateajax html controller

  • Please log in to reply
3 replies to this topic

#1 zrmba



  • Members
  • PipPip
  • 16 posts

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?



#2 davidoster


    Grocery CRUD Ninja

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

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.

Edited by davidoster, 13 May 2013 - 12:39 PM.



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



David Oster aka George Pasparakis,

#3 victor


    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 13 May 2013 - 12:58 PM

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

#4 zrmba



  • Members
  • PipPip
  • 16 posts

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>
	<meta charset="utf-8" />
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() {
		$('#idtype_field_box').append('<div id="champstype" style="border: 1px solid #000;"> Selectionez un type de facture</div>');
		  var typef;
		  typef = $('#field-idtype').val();
		  if (typef == ""){
			$('#champstype').html('Selectionez un type de facture');
			  '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
		$('#idclient_input_box').append('<a href="<?php echo site_url()?>/creances/quick_add/" class="various fancybox.ajax">Ajouter</a>');
<style type='text/css'>
	font-family: Arial;
	font-size: 14px;
a {
    color: blue;
    text-decoration: none;
    font-size: 14px;
	text-decoration: underline;
	<div style='height:20px;'></div>  
		<?php echo $output; ?>

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

function _creance_output($output = null)

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


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


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!

Also tagged with one or more of these keywords: templateajax, html, controller

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users