簡體   English   中英

在表單中將hasOne()下拉字段設置為只讀

[英]Set hasOne() dropdown field as readonly in forms

關於設置表單中的字段為只讀的文章很多,但似乎沒有一篇涉及hasOne()字段。 我希望編輯屬於給定班級的學生(入學)列表。 該課程針對特定學科,例如數學,並存儲時間段和最大學生人數。 在編輯注冊時,我希望班級詳細信息是只讀的,而屬於班級的學生可以通過CRUD進行編輯。 我可以通過為每個字段調用display()來設置模型的本機字段(例如https://groups.google.com/forum/?fromgroups=#!topic/agile-toolkit-devel/v2xVYsRqFpY ),但是我不能找到設置hasOne的方法。

下面是我到目前為止的代碼。

主題模型

class Model_Subject extends Model_Table {
public $table='subject';

function init(){
        parent::init();

        $this->addField('name');
        $this->addField('subject_code');
        $this->addField('semester');
        $this->addField('description');

}
}

類模型

class Model_Class extends Model_Table {
    public $table='class';

    function init(){
        parent::init();
        $this->hasOne('Subject');
        $this->addField('date_start')->type('date')->caption('Start');
        $this->addField('date_end')->type('date')->caption('End');
        $this->addField('max_students')->type('int');

        $this->hasMany('ClassHasStudent','class_idclass', 'idclass');
    }
}

學生模型

class Model_Student extends Model_Table {
    public $table='student';

    function init(){
        parent::init();

        $this->hasMany('ClassJoinClassHasStudent');
        $this->addField('student_ID')->caption('Student ID');
        $this->addField('name')->caption('Name');

        $this->addExpression('number_classes')->set(
                $this->add('Model_ClassHasStudent')->addCondition('student_id',$this->_dsql()->getField('id'))->count()
        )->caption('Number of classes');
    }

}

鏈接表。

class Model_ClassHasStudent extends Model_Table {
public $table='class_has_student';

function init(){
    parent::init();

    $this->hasOne('Class', 'class_id', 'id');
    $this->hasOne('Student');

    $this->addField('date_enrolled')->type('date');
    $this->addField('grade');

}
}

形式為:

$form=$this->add('MVCForm');
$classes=$this->add('Model_Class');

// $classes-> set hasOne Subject name field to read only.

$classes->getField('date_start')->display(array('form'=>'readonly'));
$classes->getField('date_end')->display(array('form'=>'readonly'));
$classes->getField('max_students')->display(array('form'=>'readonly'));

// Method from Romans.
$form->model->load($id);
$this->add('CRUD')->setModel($form->model->ref('ClassHasStudent'));

$form->addSubmit('Save');

$form->onSubmit( function($form){

    $form->update();
    return $form->js()->univ()->location($form->api->getDestinationURL(
            'managestudents',
            array('id'=>false)));

});

另外,如果我設置$ this-> hasOne('Subject')-> readonly(true); 然后在Model_Class中,表單將主題ID顯示為文本,而不是主題的“名稱”。

謝謝你的幫助。

在模型中嘗試以下操作:

$this->hasOne('Subject'); // will create 'subject' and 'subject_id' fields
$this->getElement('subject')
    ->editable(true)
    ->display(array('form'=>'readonly'));

hasOne創建兩個字段。 取消引用的字段(不帶_id)是一個計算字段,默認情況下不包括在表單中。 通過設置“可編輯”,您可以將其返回到表單(通過計算得出事件)並設置display屬性。 或者,您可以執行以下操作:

$forms->getElement('subject_id')->setAttr('disabled',true);

這仍將使用下拉菜單,但將被禁用。 我不是100%積極的,但是任何jQueriUI增強(例如自動完成)都必須尊重禁用的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM