ทำให้ Joomla ไม่ต้องเปลี่ยนหน้าเพื่อดึงข้อมูลที่ต้องการมาแสดง เช่น List box หรือ text box แบบ Auto complete ตามรูป ก่อนอื่นต้อง ดาวน์โหลด Jquery UI มา่ไว้ใน Component ของเราก่อน
เริ่มส่วของโค้ด
Controller
โค้ด: เลือกทั้งหมด
function Ajax()
{
// Check for request forgeries
// JRequest::checkToken() or die( 'Invalid Token' );
JRequest::checkToken( 'get' ) or die( 'Invalid Token' ); //ป้องกัน CSRF Attack
$get = JRequest::get('get');
$model = $this->getModel('lhc');
$data = $model->getAjaxData($get);
echo $data;
jexit();
}
โค้ด: เลือกทั้งหมด
// method สำหรับดึงข้อมูลจาก database
function getAjaxData($get)
{
$db =& JFactory::getDBO();
$user_name = $get['term']; // ถูกสร้างมาโดย Jquery autocomplete
$sql = "SELECT login AS value FROM member
WHERE
mem_id='".$db->Quote($user_name)."' OR login LIKE '".$db->Quote($user_name)."%' ";
$db->setQuery( $sql, 0, 10);
$db->query();
$rows = $db->loadAssocList();
$json = $rows;
return $this->array_to_json($rows); // ส่งค่ากลับออกไปในณุปแบบ Json
}
// method สำหรับแปลง array ให้ไปอยู่ในรูปแบบของ Json เอามาจาก Jquery ดัดแปลงนิดหน่อย เพื่อให้เข้ากับ Joomla
function array_to_json( $array ){
if( !is_array( $array ) ){
return false;
}
$associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
if( $associative ){
$construct = array();
foreach( $array as $key => $value ){
// We first copy each key/value pair into a staging array,
// formatting each key and value properly as we go.
// Format the key:
if( is_numeric($key) ){
$key = "key_$key";
}
$key = "\"".addslashes($key)."\"";
// Format the value:
if( is_array( $value )){
$value = array_to_json( $value );
} else if( !is_numeric( $value ) || is_string( $value ) ){
$value = "\"".addslashes($value)."\"";
}
// Add to staging array:
$construct[] = "$key: $value";
}
// Then we collapse the staging array into the JSON form:
$result = "{ " . implode( ", ", $construct ) . " }";
} else { // If the array is a vector (not associative):
$construct = array();
foreach( $array as $value ){
// Format the value:
if( is_array( $value )){
$value = $this->array_to_json( $value );
} else if( !is_numeric( $value ) || is_string( $value ) ){
$value = "'".addslashes($value)."'";
}
// Add to staging array:
$construct[] = $value;
}
// Then we collapse the staging array into the JSON form:
$result = "[ " . implode( ", ", $construct ) . " ]";
}
return $result;
}
โค้ด: เลือกทั้งหมด
function display($tpl = null)
{
$doc = &JFactory::getDocument();
$doc->addScript( 'components/com_mindphp/javascript/jquery.js' );
$doc->addScript( 'components/com_mindphp/javascript/js/jquery-1.7.1.min.js' );
$doc->addScript( 'components/com_mindphp/javascript/js/jquery-ui-1.8.18.custom.min.js' );
$doc->addStyleSheet('components/com_mindphp/javascript/css/ui-lightness/jquery-ui-1.8.18.custom.css');
$js = ' $(function() {
$( "#user_name" ).autocomplete({
source: "'. 'index.php?option=com_jcrm&c=lhc&task=Ajax&'. JUtility::getToken() .'=1"
});
});';
$doc->addScriptDeclaration($js);
parent::display($tpl);
}