2016-05-24 24 views
1

我是moodle中的新成員,並且始終在客戶端進行編程。我想也許我因爲這個原因錯過了一些東西。根據他在組合中選擇的內容,我需要爲用戶提供各種UI元素。所以我正在考慮根據戰略(設計模式)寫出這些文章。從mod_form.php一個對象,我嘗試執行這樣的事情:根據select元素的onchange事件添加新的moodle UI元素

正在執行和日誌打印在控制檯中,但絕不會在執行中displayStrategy.php內容
$this -> _form -> addElement('select', 'displayStrategy', get_string('displayStrategy', 'xForum'), $displayStrategy, array('onchange' => 'javascript: function loadStrategy(selVal){ 

$.ajax({ 
type: "POST", 
url: "../mod/xForum/action/displayStrategy.php", 
data: { class: selVal } 
}).done(function(msg) { 
console.log("Strategy was executed"); 
}); 
}; loadStrategy(this.value);')); 

,一「加載」效果被添加到當前視圖,最後一個問題是我還需要在寫入UI的同一個對象中調用一個函數(mod_form.php中執行所有$ this - > _form - > addElement的函數(...))

你能幫我一把嗎?我如何根據戰略執行這些方法?

非常感謝!

回答

0

我做了以下實現這一目標:

  1. 放在一個單獨的文件腳本和你file_form.php中調用它:

    // Get data dynamically based on the selection from the dropdown 
    $PAGE->requires->js(new moodle_url('/blocks/mymodulename/js/myscript.js')); 
    
  2. 文件myscript.js有「.change」函數用於識別何時「選擇」已更改,「.load」用於從文件getter.php中獲取數據,以傳遞已更改的select中的參數。如果你檢查了元素,那麼還要注意前綴#id_在離開和學習之前,這就是實際調用的方式。

    window.onload = init; 
    
    function init() { 
    
        // When a select is changed, look for the students based on the department id 
        // and display on the dropdown students select 
        $('#id_departmentid').change(function() { 
         $('#id_studentid').load('getter.php?departmentid=' + $('#id_departmentid').val());         
        }); 
    
    } 
    
  3. 在getter.php文件,捕獲參數通過$ _ GET方法發送,做您的查詢和回聲你的結果。因此,文件getter.php應該是這樣的:

    <?php 
    require_once("../../config.php"); 
    global $DB; 
    
    // Get the parameter 
    $departmentid = optional_param('departmentid', 0, PARAM_INT); 
    
    // If departmentid exists 
    if($departmentid) { 
    
        // Do your query 
        $query = 'SELECT * FROM {table_without_prefix} WHERE departmentid = ' . $departmentid; 
        $student_arr = $DB->get_records_sql($query, null, $limitfrom=0, $limitnum=0); 
    
        // echo your results, loop the array of objects and echo each one 
        echo "<option value='0'>All Students</option>"; 
        foreach ($student_arr as $student) { 
         echo "<option value=".$student->id.">" . $student->fullname . "</option>"; 
        } 
    
    } 
    ?> 
    
  4. 最後你file_form.php將有2種選擇,其中一個選項,和其他與在那裏你需要顯示的結果。

    $mform->addElement('select', 'departmentid', "Select Department", $department_array); 
    $student_array = array("All Students"); 
    $mform->addElement('select', 'studentid', "Select Student", $student_array); 
    

另外,確保添加以下功能,使您能夠正確使用$形式 - 當> GET_DATA()讀取數據;

function get_data(){ 
    global $DB; 

    $data = parent::get_data(); 

    if (!empty($data)) { 
     $mform =& $this->_form; 

     // Add the studentid properly to the $data object. 
     if(!empty($mform->_submitValues['studentid'])) { 
      $data->studentid = $mform->_submitValues['studentid']; 
     } 

    } 

    return $data; 
} 
  • 下面是結果的快速視頻:http://screencast.com/t/KDtiWzDN
  • 我希望它能幫助!