use of options instead of dependant()

use of options instead of dependant()

loukinglouking Posts: 259Questions: 52Answers: 0
edited September 2016 in Editor

I am not sure if this is a question or a request. :) There's a lot going on here. Sorry for that.

I am in the process of enhancing my flask (python) app, and decided to come up with server classes to define the server processing. The reason for this is to simplify creation of the server and browser side code.

In doing this, as soon as the table starts referring to other tables (e.g., table1 has attribute table2_id) the Editor form needs to be able to access table2's data. This is perfect for a select (or selectize) type. So I needed to create a class to return data from table2 to fill in the options for the table1 new, edit forms.

Some of these options should be retrieved when the form is first opened, and some may need to be retrieved based on the data in another field. E.g.,

Auto Make: ['Ford', 'Honda']
Auto Model: <here's the dependancy on 'make' field>

The 'make' field should be filled in from the database when the form paints, and the 'model' field should be filled in when the 'make' field changes.

I started building this into my software, to process a columns/fields-like data structure, but wondered if this should be (or is already is) in Editor. E.g., something like fields.update

    'fields': {
          <other options>
          'update' {
               'url' : <url to retrieve options from>,
               'on' : <event>
               'dependent': <other field name>
             }
    }

where

  • url - url to retrieve json data to fill the option
  • on - event which triggers the update()
  • dependent - field name which, if changed, would trigger the update()

When reading the 1.5.6 Editor release notes, I stumbled on dependent() which I'd use, of course, but wondered if the options path already exists.

Having said that, dependent works like I want if one field changes, but is there a similar method which can be used to update some select options when opening the form?

This isn't a working version (yet), but you can see what I mean looking at https://gist.github.com/louking/5677a7b838b372f05faa0de2bcd826cc . If you look at services.py (way at the bottom) you can see the basics what I'm trying to achieve. Some of the other stuff might be interesting, but again this is a work in progress.

Also I assume the best way to implement this functionality would be through a plug-in, but I'm afraid I have no experience with jquery plug-ins, and what data structure to modify within Editor. Maybe you could give me a couple of hints? Else I can just brute force this in my datatables.js module.

Answers

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    It does indeed sound like dependent() is what you want. You can have it control a number of different aspects of the form, including options of other select fields.

    There is quite a lot that method can do, so to documentation for it is quite long, but have a look in particular at the Return options / JSON section.

    Allan

  • loukinglouking Posts: 259Questions: 52Answers: 0

    Sorry my question was so long. I assume that Editor doesn't already have something like the fields.update option I'm looking for

    Can you give me some hints on how I would make a plug-in to extend the fields options as such?

    I'm in the process of brute-forcing this into my datatables.js module, but think the plug-in model may be the right way to go.

  • loukinglouking Posts: 259Questions: 52Answers: 0
    edited September 2016

    [deleted]

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    I'm afraid I'm not quite clear on why dependent() or field().update() won't do what you are looking for?

    You say:

    The 'make' field should be filled in from the database when the form paints, and the 'model' field should be filled in when the 'make' field changes.

    dependent() can do that. It can both set the options available for a field which has options, and set values.

    Allan

  • loukinglouking Posts: 259Questions: 52Answers: 0
    edited September 2016

    I am planning to do this through a fields.update option which will trigger the calls to dependent. This is similar to other options which also have APIs, I think. I am catching the new option in my code now, but thought best to put it into a plug-in.

            if (options.updateopts !== undefined) {
                for (i=0; i<options.updateopts.length; i++) {
                    if (options.updateopts[i].on == 'open') {
                        editor.dependent( options.updateopts[i].name, options.updateopts[i].url, {event:'focus'} )
                    } else if (options.updateopts[i].on == 'change') {
                        editor.dependent( options.updateopts[i].name, options.updateopts[i].url, {event:'change'} )
                    }
                }
            }
    

    For the other point, on the 'make' field, I have found that dependent won't fire when called with {event:'open'}. As you see above, I have been able to make it work using the 'focus' event on the field, which I think is sub-optimal.

This discussion has been closed.