Collective.z3cform.datagridfield - How can I get reordering of a datagridfield to work in my RegistryEditForm?

I have a RegistryEditForm that has a widget that uses DataGridFieldFactory:

from zope.interface import Interface
from zope import schema
from z3c.form import field
from z3c.form import form
from collective.z3cform.datagridfield.registry import DictRow
from collective.z3cform.datagridfield import DataGridFieldFactory

from plone.z3cform import layout
from plone.app.registry.browser.controlpanel import RegistryEditForm
from plone.app.registry.browser.controlpanel import ControlPanelFormWrapper    

class IMyObject(Interface):
    title = schema.TextLine(title=u"Title",
                            required=True,
                           )

    description = schema.TextLine(title=u"Description",
                                  required = True,
                                  )

class IMySettingsForm(model.Schema)
    my_objects = schema.List(title=u"My Objects",
                             value_type=DictRow(title=u"My Objects", schema="IMyObject"),
                             )

class MySettingsForm(RegistryEditForm):
    form.extends(RegistryEditForm)
    schema = IMySettingsForm
    label = u"My Settings",
    fields = field.Fields(IMySettingsForm)
    fields['my_objects'].widgetFactory = DataGridFieldFactory

    def updateWidgets(self):
        super(MySettingsForm, self).updateWidgets()
        self.fields['my_objects'].allow_reorder = True

MySettingsControlPanelView = layout.wrap_form(MySettings, ControlPanelFormWrapper)
MySettingsControlPanelView.label = u"My Settings"

In my configure.zcml:

<browser:page
	name="my-settings"
    for="Products.CMFPlone.interfaces.IPloneSiteRoot"
    permission="cmf.ManagePortal"
    class="my.product.registry.my_settings.MySettings"
/>   

The DataGridField shows up, but the feature allowing reordering doesn't show up.
Is this the correct approach? If not, what am i doing wrong?

I'm using Plone 5.2.

I'd solve this via plone.autoform like this:

from plone.autoform import directives
from plone.supermodel import model

...

class IMySettingsForm(model.Schema)
    my_objects = schema.List(title=u"My Objects",
                             value_type=DictRow(title=u"My Objects", schema="IMyObject"),
                             )

    directives.widget(
        'my_objects',
        DataGridFieldFactory,
        allow_reorder=True,
    )

...

this way you do not need to redefine the updateWidgets method ... quickly tested in a behavior of mine and worked.

2 Likes

I get the error:
TypeError: AutoExtensibleSubformAdapter() takes no arguments

I removed updateWidgets and fields['my_objects'].widgetFactory = DataGridFieldFactory

```
class MySettingsForm(RegistryEditForm):
    form.extends(RegistryEditForm)
    schema = IMySettingsForm
    label = u"My Settings",
    fields = field.Fields(IMySettingsForm)

Try to specify it in the widget as Peter said. It is much easier.
Skip everything with 'fields' and 'updateWidgets'.

1 Like

Thanks. The sorting arrows show up now.

Is there any way to have something similar to how viewing folder contents works where they can drag/drop and reorder the items?

Don't thinks so.

It would probably be easier to make a custom widget instead of datagridfield.
If so, maybe (wild guess) it is possble to use something like https://plone.github.io/mockup/dev/#pattern/sortable

1 Like