[eluser]Gorazd Reichman[/eluser]
Hello,
This is my first post on this forum. I have been using CI for a while now.
It is great but I miss some ajax functionality. As I have read on this forum we can expect some sort ajax/JQuery support in the near future but I needed a solution right now, so I wrote simple JQuery autocomplete libraray for use in CI.
Library:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class JQuery {
var $el_Num;
var $AjaxDelay ;
var $AjaxBaseUrl ;
var $AjaxMinChars ;
var $scripts ;
function JQuery()
{
$this->CI =& get_instance();
$this->el_Num = array() ;
$this->AjaxDelay = 500 ;
$this->AjaxMinChars = 3 ;
$this->AjaxBaseUrl = "" ;
$this->scripts = array() ;
$this->CI->load->helper('form') ;
}
function setAjaxDelay($delay)
{
$this->AjaxDelay = $delay ;
}
function setAjaxMinChars($chars)
{
$this->AjaxMinChars = $chars ;
}
function setAjaxBaseUrl($url)
{
$this->AjaxBaseUrl = $url ;
}
function loadJScript($src)
{
$this->scripts[] = $src ;
}
function JScript()
{
$output = "" ;
foreach ($this->scripts as $script)
{
$output .= '[removed][removed]' ;
}
$output .= '[removed]' ;
$output .= 'var autocomplete_baseurl = "'.$this->AjaxBaseUrl.'" ;' ;
$output .= 'var autocomplete_delay = '.$this->AjaxDelay.' ;' ;
$output .= 'var autocomplete_minchars = '.$this->AjaxMinChars.' ;' ;
$output .= '[removed]' ;
return $output ;
}
function form_autocomplete($ajaxFunction, $hidden = FALSE, $data = '', $value = '', $extra = '')
{
if (!array_key_exists($ajaxFunction,$this->el_Num)) $this->el_Num[$ajaxFunction] = 0 ;
$this->el_Num[$ajaxFunction]++ ;
$output = form_input($data,$value,'class="autocomplete" id="'.$ajaxFunction.'_'.$this->el_Num[$ajaxFunction].'" '.$extra) ;
if ($hidden)
{
$output .= form_hidden("hidden".$ajaxFunction.'_'.$this->el_Num[$ajaxFunction]) ;
}
return $output ;
}
}
// end of class