首页 > 解决方案 > Mform SELECT - Moodle

问题描述

Is there mform select tag, when option is selected so that it highlights and stick to the selected one and select more options without the use of control key to select. 我尝试使用 selectMulitple,它允许按住控制键来选择选项。

$select = $mform->addElement('select', 'course', get_string('course', 'core_course'), $options);
        $mform->addHelpButton('course', 'course', 'core_course');
        $mform->addRule('course', null, 'required', null, 'client');
        $mform->setType('course', PARAM_INT);   
        $select->setMultiple(true); 

标签: moodle

解决方案


简短的回答是否定的。您需要使用控制键在一个选择中选择多个项目。

不过,您可以使用一系列复选框,如下所示:

$courses = core_course_category::get(0)->get_courses(
    array('recursive' => true, 'sort' => array('fullname' => 1)));

foreach ($courses as $course) {
    $mform->addElement('advcheckbox', "courses[{$course->id}]",
        format_string($course->fullname), null, array('group' => 1));
}
$this->add_checkbox_controller(1);

然后在您的编辑代码中,使用类似这样的东西

for each($formdata->courses as $courseid => $selected) {
    if ($selected) {
        // User selected this course.
    } else {
        // User unselected this course.
    }
}

推荐阅读