【问题标题】:Controlling the Position of some items whilst the remainder can be sorted控制一些项目的位置,而其余的可以排序
【发布时间】:2016-09-26 08:47:17
【问题描述】:

我有一个 ModelAdminMyDataObject 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;
    }
}

我想在 AnotherDataObject 上使用 Type 来控制排序顺序,以便 Top 项目保持在顶部,然后 End 在最后。我认为最简单的方法是防止“拖放”或“下降”到这些位置并控制onBeforeWrite 内的顶部/结束项目的优先级...但我不确定如何禁用这些正在“拖放”...或者是否有更好的方法?

【问题讨论】:

    标签: javascript php silverstripe


    【解决方案1】:

    您需要覆盖 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”添加到要从排序中排除的行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      相关资源
      最近更新 更多