Perent / Child - disable child New Button

Perent / Child - disable child New Button

Adrian ChallinorAdrian Challinor Posts: 21Questions: 8Answers: 0

I have a number of Parent/Child screens in our application. As is normal, when a parent record is selected, the child table is populated.

But we have a logic problem. If no parent record is selected, the child tables "New" button is still active. Choosing this will get an error when it gets to the server.

Is there a way to disable the child table's "New" button unless a parent record is selected?

This question has accepted answers - jump to:

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    Answer ✓

    I just noticed that the example
    https://editor.datatables.net/examples/advanced/parentChild.html
    has the same issue.

    The child's "New" button appears to work, as the ensuing modal has the "Location" field. However, it doesn't actually add the new record.

    Looks like an error. Either enable the new child record, or don't show the child's buttons until a parent location is selected.

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923
    Answer ✓

    I would look at using the draw event for the child table. In the event use page.info() to see if the table has any rows. The use the buttons().enable() API to enable or disable the button.

    Kevin

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    edited September 2020 Answer ✓

    Kevin's suggestion is one way to go. Another, which would be better if there are more rows in the user's table would be the following:

                { extend: 'create', editor: usersEditor, enabled: false, init: function(dt) {
                    var that = this;
                    siteTable.on('select deselect', function() {
                        that.enable(siteTable.rows({selected: true}).any())
                    })
                }},
    

    I've updated the example, which will make its way to the site on the next build - probably in the next week.

    Thanks to all for the suggestions and steps to reproduce,

    Colin

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    Cool, I've never used buttons.buttons.init. Got my learning in for the day :smile:

    Kevin

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    And it's barely breakfast - might as well go back to bed, the day can't get any better :)

This discussion has been closed.