Search code examples
python-3.xflask-admin

Flask-Admin custom action with unselected


Can we use @action decorator to work with unselected element?

When we use

@action
def action_custom(self, ids):

ids = selected element on page, but can we get unselected element here?


Solution

  • You can hack action.js in Flask-Admin and change the submit form to include the unchecked rows as hidden values and then you can pickup these values in your python @action method using the submitted form's getlist method.

    At line 21 in action.js we have :

    $('input.action-checkbox', form).remove();
    $('input.action-checkbox:checked').each(function() {
        form.append($(this).clone());
    });
    
    form.submit();
    

    Change this so the unchecked rows are included with the form. Note the use of the :not(:checked) in the jQuery selector and the values are saved to hidden inputs named 'notrowid':

    $('input.action-checkbox', form).remove();
    
    $('input.action-checkbox:not(:checked)').each(function(i, v) {
        form.append($('<input>').attr({'type':'hidden', 'name':'notrowid', 'value': v.value}));
    });
    
    $('input.action-checkbox:checked').each(function() {
        form.append($(this).clone());
    });
    form.submit();
    

    Now in your python (Python 2) @action method you can do the following :

    @action
    def action_custom(self, ids):
        _not_selected_ids = request.form.getlist('notrowid')
        print _not_selected_ids