Search code examples
datagridreact-admin

How to unselect checkboxes after bulk action execution?


On a List module, I created a bulk action button to generate PDF by calling a custom action. The problem is <Datagrid> checkboxes are not unselected once the action is executed.

Here is my custom action:

export const print = (resource, ids, data) => ({
  type: DOWNLOAD,
  payload: {
    id: ids.length === 1 ? ids[0] : ids,
    data,
  },
  meta: {
    resource: resource,
    fetch: PRINT,
    onFailure: {
      notification: {
        body: 'ra.notification.http_error',
        level: 'warning',
      },
    },
  },
});

And here is my button:

class PrintBulkButton extends React.Component {
  handleClick = () => {
    const { basePath, options, print, resource, selectedIds } = this.props;

    print(resource, selectedIds, options, basePath);
  };

  render() {
    return (
      <Button {...sanitizeRestProps(this.props)} onClick={this.handleClick}>
        {this.props.icon}
      </Button>
    );
  }
}

I'm using react-admin 2.3.0, but it wasn't working with previous versions either.

I think the checkboxes are not unchecked because the service I call doesn't update data. Am I right?

Do I have to call another service or action to uncheck them, or am I missing something?


Solution

  • You can add this onSuccess side effect parameter unselectAll: true that we should document (please open an issue for it):

    export const print = (resource, ids, data) => ({
      type: DOWNLOAD,
      payload: {
        id: ids.length === 1 ? ids[0] : ids,
        data,
      },
      meta: {
        resource: resource,
        fetch: PRINT,
        onSuccess: {
            unselectAll: true,
        },
        onFailure: {
          notification: {
            body: 'ra.notification.http_error',
            level: 'warning',
          },
        },
      },
    });