dongzhenjian5195 2016-09-26 08:47
浏览 19
已采纳

控制一些物品的位置,同时可以对剩余物品进行分类

I have a ModelAdmin with MyDataObject has_many AnotherDataObject and SilverStripe Grid Field Extensions Module that is controlling the

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;
    }
}

I'd like to use Type on AnotherDataObject to control the sort order so that the Top items remain at the top and then End at the end. I think the easit way would be to prevent the ability to "drap" or "drop" into those positions and control the Priority for the Top/End items within onBeforeWrite... but I'm not sure how to disable those from being "drag/drop"... or if there is a better way?

  • 写回答

1条回答 默认 最新

  • dpxnrx11199 2016-09-26 09:45
    关注

    You need to override jQuery UI sortable initialisation:

    $(".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"
                });
            },
        });
    

    You will need to add new class '.ui-state-nosort' to the rows that you want to exclude from sorting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备