Skip to main content

Row Action Methods

The row_actions attribute within the ModelTable class offers a versatile means to define actions applicable to individual rows in a table. It aims to provide users with an intuitive and interactive interface for manipulating individual table rows. It enables tasks like editing patient records, toggling statuses, or deleting records, fostering meaningful engagement with the data. This section dives into the structure, purpose, and utilization of row_actions for seamless implementation. To use the row_actions attribute in your PatientTable class, follow these steps:

  • Define Row Actions: Inside your class, specify row_actions as a list of dictionaries, each defining a unique row action.
  • Specify Action Details: For each row action, specify the details as per the row actions definition.
  • Set Row Action Permissions: Specify whether a user is allowed to perform a specific row action.
  • Process Row Actions: Introduces the method processrow_action<action_key> responsible for executing the logic associated with a specific row action.

Defining Row Actions

In your class, define the row_actions attribute as a list of dictionaries. Each dictionary represents a distinct row action.

row_actions = [

{
# Action 1 details
},

{
# Action 2 details
},

# Additional actions...
]

Specifying Action Details

The row_actions attribute comprises a list of dictionaries, each representing a distinct row action. These actions are characterized by specific key-value pairs:

  • "name" (required): Display name or label.
  • "key" (required): Unique identifier.
  • "description" (required): Brief purpose description.
  • "type" (required): Determines action type ("form" or "simple").
  • "form" (conditional, if type is "form"): Form class for user input.
  • "Simple" (conditional, if type is "simple"): Denotes actions executed with a single click, prompting confirmation dialogs.
  • "confirmation_message" (optional): Confirmation message.
  • "roles" (optional): Permitted user roles for action execution.

Example Usage

row_actions = [
{
"name": "Edit Patient",
"key": "edit",
"description": "Edit patient record",
"type": "form",
"form": YourModelFormClass,
"roles": ["AnonymousUsers"]
},
{
"name": "Mark Active/Inactive",
"key": "mark_active_inactive",
"description": "Mark Patient Active/Inactive",
"type": "simple",
"confirmation_message": "Are you sure you want to perform this action?",
"roles": ["AnonymousUsers"]
},
{
"name": "Delete",
"key": "delete_patient",
"description": "Delete Patient",
"type": "simple",
"confirmation_message": "Are you sure you want to delete this action?",
"roles": ["AnonymousUsers"]
}
]

Setting Row Action Permissions

This method assesses whether a user is authorized to execute a specific row action based on the request and the corresponding object (record). It returns a Boolean value (True or False) signifying the action's visibility and accessibility.

Method Signature

def can_perform_row_action_<action_key>(self, request, obj)

Parameters

  • self: Instance of the ModelTable class.
  • request: Django request object representing the user's request.
  • obj: Object (record) associated with the row for the intended action.

Return Value

  • True: User is allowed to perform the action.
  • False: User is not allowed to perform the action.

Example Usage

# Example logic to determine if the user can mark a record as active or inactive
# Only allow marking as inactive if the record is currently active

def can_perform_row_action_<action_key>(self, request, obj):
return obj.active

Processing Row Actions

This method manages the execution of a specific row action triggered by the user. It encompasses the necessary logic or operations associated with the action, such as modifying the object's data, saving changes, and generating a response message.

Method Signature

def process_row_action_<action_key>(self, request, obj):

Parameters

  • self: Instance of the ModelTable class.
  • request: Django request object representing the user's request.
  • obj: Object (record) associated with the row for the intended action.

Return Value

  • success: Boolean value (True or False) indicating whether the action was successfully processed.
  • response: Dictionary containing any relevant response data, such as a success message or error message.

Example Usage

def process_row_action_mark_active_inactive(self, request, obj):

# Example logic to mark a record as active or inactive

obj.active = not obj.active
obj.save()
success = True

response = {
"message": "Marked as " + ("Active" if obj.active else "Inactive")
}

return success, response