ตัวอย่างโค้ด
สร้าง function ไว้ในไฟล์ view.html.php สำหรับเรียกใช้งาน Modal
โค้ด: เลือกทั้งหมด
function getModalButtonObject ($name, $text, $link, $width = 400, $height = 300) {
/*
กำหนดพารามิเตอร์
$name เป็นชื่อที่ใช้อ้างอิงตัว Object ของ Modal
$text เป็นข้อความที่ใช้แสดงบนปุ่ม
$link เป็น url ที่ตัว Modal จะไปเรียกข้อมูลมาแสดงบน Popup
$width กำหนดความกว้างของ Popup ในตัวอย่างตั้ง default ไว้ที่ 400
$height กำหนดความสูงของ Popup ในตัวอย่างตั้ง default ไว้ที่ 300
*/
JHTML::_('behavior.modal', "a.{$name}");
/*
การใช้งาน Popup Modal จำเป็นต้องเรียกใช้ JHTML::_('behavior.modal'); ทุกครั้ง
ส่วน "a.{$name}" เป็นการกำหนดการใช้งาน Modal ที่ tag <a>
และ class คือ ชื่อเดียวกับตัวแปร $name
*/
$buttonMap = new JObject(); // สร้าง JObject สำหรับเก็บค่าต่างๆ
$buttonMap->set('modal', true); // กำหนดการใช้งาน Modal เป็น true
$buttonMap->set('text', $text ); // กำหนดข้อความที่แสดงบนปุ่ม
$buttonMap->set('modalname', $name); // กำหนดชื่อของ Modal
$buttonMap->set('options', "{handler: 'iframe', size: {x: ".$width.", y: ".$height."}}");
// options อันนี้กำหนด Popup เป็นลักษณะ iframe และกำหนดความกว้าง และ ความสูง
$buttonMap->set('link', $link); // กำหนด url เมื่อคลิกที่ปุ่ม Modal
return $buttonMap;
}
โค้ด: เลือกทั้งหมด
function display($tpl = null) {
$this->buttonModal = $this->getModalButtonObject(
'add_keyword',
JText::_('JTCONTENT_ADD_KEYWORDS'),
'index.php?option=com_msearch&c=content&task=view&layout=modal_keywords&tmpl=component&'.JSession::getFormToken().'=1'
);
/*
เรียกใช้ function getModalButtonObject และ ส่ง Object เก็บไว้ที่ตัวแปล this->buttonModal
ใน url กำหนด layout ที่จะนำมาแสดงใน Popup
กำหนด tmpl ต้องเป็น component
และใช้ JSession::getFormToken() ในการเช็ค Token
*/
parent::display($tpl);
}