控制某些项目的位置,同时可以对其余项目进行排序

时间:2016-09-26 08:47:17

标签: javascript php silverstripe

我有ModelAdmin MyDataObject has_many AnotherDataObjectSilverStripe Grid Field Extensions Module正在控制

class TestAdmin extends ModelAdmin {
    static $managed_models = array('MyDataObject');
    static $url_segment = 'testadmin';
    static $menu_title = 'TestAdmin';
}

class MyDataObject extends DataObject {
    private static $db = array('Name' => 'Varchar(255)');
    private static $has_many= array('AnotherDataObjects' => 'AnotherDataObject');

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

        if ($grid = $fields->dataFieldByName('AnotherDataObjects')) {
            $grid->getConfig()
                ->removeComponentsByType('GridFieldAddExistingAutocompleter')
                ->addComponent(new GridFieldOrderableRows('Priority'));

            $fields->removeByName('AnotherDataObjects');
            $fields->insertAfter($grid,'Name');
        }

        return $fields;
    }
}

class AnotherDataObject extends DataObject {
    private static $db = array(
        'Name'      => 'Varchar(255)',
        'Type'      => "Enum('Middle,Top,End','Middle')",
        'Priority'  => 'Int'
    );
    private static $has_one = array('MyDataObject' => 'MyDataObject');

    function onBeforeWrite() {
        parent::onBeforeWrite();

        if($this->Type == 'Top')
            $this->Priority = DB::query("SELECT MIN(Priority) FROM Type = 'Top'")->value();

        if($this->Type == 'End')
            $this->Priority = DB::query("SELECT MAX(Priority) FROM AnotherDataObjectWHERE Type = 'End'")->value() + 1;
    }
}

我想在Type上使用AnotherDataObject来控制排序顺序,以便Top项目保持在顶部,然后在结尾处结束。我认为最简单的方法是阻止“dra dra”的能力。或"掉落"进入这些职位并控制onBeforeWrite内的热门/结束项目的优先级...但我不确定如何禁用那些"拖放" ...或者如果还有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

您需要覆盖jQuery UI可排序初始化:

$(".ss-gridfield-orderable tbody").entwine({
    onadd: function() {
        // skipped code

        // the `cancel` option allows to exclude items from drag&drop sorting
        this.sortable({
                handle: ".handle",
                helper: helper,
                opacity: .7,
                update: update,
                cancel: ".ui-state-nosort"
            });
        },
    });

您需要添加新课程' .ui-state-nosort'到您要从排序中排除的行。