SilverStripe ListboxField按用户选择多选排序

时间:2016-07-21 15:01:33

标签: php silverstripe

我的many_manyTheses之间存在Subjects关系。我创建了ListboxField w / c接受多个值。

将其保存到数据库不是问题,但是当我再次查看信息时,主题会根据主题的ID对用户选择进行排序。

例如,用户选择订单[1,5,4,2]。保存并再次查看后,它将显示为[1,2,4,5]

这是我的代码:

Theses.php

class Theses extends DataObject {

    private static $db = array(
        'CallNo' => 'Varchar',
        'AuthorID' => 'Int',
        'TitleTH' => 'Text',
        'Year' => 'Int(4)',
        'PhysicalDesc' => 'Text',
        'Notes' => 'Text',
        'Summary' => 'Text',
        'DegreeCourse' => 'Varchar'
    );

    private static $has_one = array(
        'Author' => 'Author'
    );

    private static $field_labels = array(
        'CallNo' => 'Call Number',
        'TitleTH' => 'Title Headings',
        'Author.AuthorName' => 'Author',
        'DegreeCourse' => 'Degree Course',
        'Year' => 'Year Published',
        'SubjectsString' => 'Subject'
    );

    private static $summary_fields = array(
        'CallNo' => 'CallNo',
        'TitleTH' => 'TitleTH',
        'Author.AuthorName',
        'DegreeCourse',
        'Year' => 'Year',
        'SubjectsString'
    );

    private static $many_many = array(
        'Subjects' => 'Subject'
    );

    public function SubjectsString() {
        $returnString = '';
        foreach ($this->Subjects()->sort('Theses_Subjects.Created') as $Subjects) {
            $returnString .= $Subjects->SubjectTitle . '--';
        }
        return $returnString;
    }

    public function getCMSfields() {
        $fields = FieldList::create(TabSet::create('Root'));
        $fields->addFieldsToTab('Root.Main', array(
            TextField::create('CallNo'),
            DropdownField::create('AuthorID', 'AuthorName')
                ->setSource(Author::get()->sort('AuthorName')->map('ID', 'AuthorName')),
            TextField::create('TitleTH'),
            NumericField::create('Year', 'Year')
                ->setMaxLength(4),
            TextField::create('PhysicalDesc'),
            TextField::create('Notes'),
            TextAreaField::create('Summary'),
            DropdownField::create('DegreeCourse', 'DegreeCourse', array('BLIS' => 'BLIS', 'BLS' => 'BLS', 'MLIS' => 'MLIS', 'MLS' => 'MLS')),
            ListboxField::create('Subjects', 'Subjects', Subject::get()->map('ID', 'SubjectTitle')->toArray(), 1, 4, true),
        ));

        return $fields;
    }
}

Subject.php

class Subject extends DataObject {

    private static $db = array(
        'SubjectTitle' => 'Varchar'
    );

    private static $belongs_many_many = array(
        'Theses' => 'Theses'
    );

    public function canView($member = null) {
        return Permission::check('CMS_ACCESS_MyAdmin', 'any', $member);
    }

    public function canEdit($member = null) {
        return Permission::check('CMS_ACCESS_MyAdmin', 'any', $member);
    }

    public function canDelete($member = null) {
        return Permission::check('CMS_ACCESS_MyAdmin', 'any', $member);
    }

    public function canCreate($member = null) {
        return Permission::check('CMS_ACCESS_MyAdmin', 'any', $member);
    }

    private static $summary_fields = array(
        'SubjectTitle'
    );

}

1 个答案:

答案 0 :(得分:0)

我找了一种让ListBox工作的方法,但找不到适合你的东西,所以我以一种提供类似体验而不是ListBox的方式回答这个问题。

使用GridField Extensions Module可以将排序添加到GridField并删除"添加新的"按钮并通过自动完成保留添加具有相同的功能。它占用更多空间 - 但确实显示了主题的摘要并允许打开记录 - 以及根据需要重新排序。

由于存在一些丢失的对象(作者),我只包含了部分代码。另请注意,对于在ModelAdmin中使用的DataObject(这是我在这里的假设),您不需要为$ fields = parent :: getCMSFields();

构建每个字段。

希望这有帮助

class Theses extends DataObject {

    ...

    private static $many_many = array(
        'Subjects' => 'Subject'
    );

    public static $many_many_extraFields = array(
        'Subjects' => array(
            'SortOrder' => 'Int',
        ),
    );

    ...

    public function getCMSfields() {
        $fields = parent::getCMSFields();

        $gridSubjects = $fields->dataFieldByName('Subjects');
        $gridSubjects->getConfig()
            ->addComponent(GridFieldOrderableRows::create('SortOrder'))
            ->removeComponentsByType('GridFieldAddNewButton');

        $fields = FieldList::create(TabSet::create('Root'));
        $fields->addFieldsToTab('Root.Main', array(

            ...

            TextField::create('Notes'),
            TextAreaField::create('Summary'),
            DropdownField::create('DegreeCourse', 'DegreeCourse', array('BLIS' => 'BLIS', 'BLS' => 'BLS', 'MLIS' => 'MLIS', 'MLS' => 'MLS')),
            $gridSubjects
        ));

        return $fields;
    }
}