Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort ModelAdmin GridFields #6

Closed
StefanNeuser opened this issue Aug 18, 2012 · 3 comments
Closed

Sort ModelAdmin GridFields #6

StefanNeuser opened this issue Aug 18, 2012 · 3 comments

Comments

@StefanNeuser
Copy link

Hi, i would be cool when your Module supports his functionality for ModelAdmin generated GridFields.

I get it work after adding a few lines of code into the getEditForm Method by Overlading the ModelAdmin:

function getEditForm($id = null, $fields = null) {
    $list = $this->getList();
    $exportButton = new GridFieldExportButton('before');
    $exportButton->setExportColumns($this->getExportFields());      
    $listField = GridField::create(
        $this->sanitiseClassName($this->modelClass),
        false,
        $list,
        $fieldConfig = GridFieldConfig_RecordEditor::create($this->stat('page_length'))
            ->addComponent($exportButton)
            ->removeComponentsByType('GridFieldFilterHeader')
            ->addComponents(new GridFieldPrintButton('before'))
    );

....
$class = $this->modelClass;
if($class::$default_sort != null) {
$conf=GridFieldConfig_RelationEditor::create(10);
$conf->addComponent(new GridFieldSortableRows($class::$default_sort));
$listField->setConfig($conf);
}

.....
}

But i get this Notice when i dragged&dropped some Row:

[19-Aug-2012 00:04:30] Notice at SortableGridField/code/forms/GridFieldSortableRows.php line 214: Object of class GridState_Data could not be converted to int (http://fritz-mueller-gmbh.dev/admin/contactadmin/Devision/EditForm/field/Devision)

I fixed it by adding this:

if ($paginator = $gridField->getConfig()->getComponentsByType('GridFieldPaginator')->First()) { $pageState = $gridField->State->GridFieldPaginator; $currentPage = $pageState->currentPage; if(is_int($currentPage)) { if($pageState->currentPage && $pageState->currentPage>1) { $pageOffset = $paginator->getItemsPerPage() \* ($pageState->currentPage - 1); } } }

I think its better to extend the ModelAdmin instead of my overloading method.

How do you increase the 'SortOrder" Field when somebody add new Records?

Maybe you forget on the TestDataObject something like this? :

function onBeforeWrite()
{

    if(empty($this->Sort)) {

        $query = new SQLQuery();
        $query->select(array("max(Sort) as Sort"));
        $query->from("Devision");
        $result = $query->execute();                    
        $record = $result->first();

        if($record) {
            $this->Sort = $record['Sort'] + 1;
        } else {
            $this->Sort = 1;
        }            

    }

    parent::onBeforeWrite();        
}

In your Example?

Maybe to decorate the obBeforeWrite Method of the DataObject like this

    $sortField = self::$default_sort;

    if(empty($this->$sortField)) {

        $query = new SQLQuery();
        $query->selectField("max($sortField)", $sortField);
        $query->setFrom(get_class());
        $result = $query->execute();
        $record = $result->first();

        if($record) {
            $this->$sortField = $record[$sortField] + 1;
        } else {
            $this->$sortField = 1;
        }

    }

should be nice

@UndefinedOffset
Copy link
Owner

I think what your doing to get model admin to do what you want is very close to what I would have done, though though instead of replacing getEditField() I would have overridden it and added to the GridField's config. (Hint the GridField's name is the model class).

Thanks for the bug report, I've got the issue fixed now in master. So you should be good to go, let me know if it still causes issues.

To answer your last comment about how does the sort order get incremented when a new object is added. This is done the next time the grid field is displayed automatically via the fixSortColumn() method. So there is no need for a DataExtension of the DataObject. This way the class is more portable and could be merged into the core of SS like was intended but the core team didn't get a chance to review and merge in time though Ingo did guide all of the dev for this class.

@StefanNeuser
Copy link
Author

it works like a charm :-)

@wubies
Copy link

wubies commented Sep 19, 2012

In case anybody wants this... I overrode getEditForm to retain any existing components and config instead of creating the gridField from scratch.

function getEditForm($id = null, $fields = null) {
        $form = parent::getEditForm($id,$fields);
        $class = $this->modelClass;
        if($class::$default_sort != null) {
            $listField = $form->Fields()->fieldByName($this->modelClass);
            $listFieldConfig = $listField->getConfig()->addComponent(new GridFieldSortableRows($class::$default_sort));
            $listField->setConfig($listFieldConfig);
            $form->setFields(new FieldList($listField));
        }
        return $form;
    }

Add this function when you extend ModelAdmin to get sortable DataObject lists (that aren't part of has_many / any_many relationship).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants