Randy Stegner Sr.
Randy Stegner Sr.

Reputation: 87

How use Flask LinkCol on database query data results?

I have been working with Flask for a little while and developed a database manager application for managing internal data for the department I work in (I am not a "real" developer but have learned by doing/necessity). The application works great as a basic CRUD app. In it I have used Flask forms and tables including the LinkCol column for linking to different functions within the app. In those cases, I am using an 'Edit' or 'Delete' clickable link; meaning every column in the table has an extra cell with that word. Clicking either of those takes the user to the corresponding page where they can take appropriate action on that item.

Currently I am trying to use LinkCol so that data returned from the database can be the clickable link that links to another function rather than every table having an additional column (for example, "Go to Account"). So, when my table populates with an account number, the user can click the actual account number in the table to go to a different page to view additional data about that specific account. I read the docs and looked at the examples on the creators page regarding overwriting things in the classes but haven't been able to figure it out. I have written a few of my own classes, but am not an expert on the matter by any means. I also have been unable to find anything else that might help including on SO which is always my go to resource for figuring things out.

Here is my table definition using a LinkCol that renders incorrectly and puts 'Account #' in each cell instead of the dynamic data I would like. Changing to Col gives correct display but I want it to be clickable:

class AccountsResults(MainTable):
    AccNo = LinkCol('Account #','dbhome_bp.route.account_detail', url_kwargs=
           dict(AccNo='AccNo',StatusCode='Active',ServiceAddress1=\
           'ServiceAddress1'))
    TnCount = Col('Total TNs')
    ServiceAddress1 = Col('Service Address')
    ServiceCity = Col('Service City')
    ServiceZip = Col('Service Zip Code')
    MasterServiceDate = Col('Master Service Date')
    Active = Col('Currently Active')

Here is an example of what I know I can do versus what I would like to do. Can do ('Go To Account' is clickable in the Go To Account column):

Go To Account Account # Service Address Etc.
Go To Account 12345678 1234 Anywhere Yup
Go To Account 12345679 5678 Somewhere Nah

Would like to do (Account numbers are clickable in Account # column ):

Account # Service Address Etc.
12345678 1234 Anywhere Yup
12345679 5678 Somewhere Nah

Does anyone know how to make the dynamic data returned from a query the clickable link for a LinkCol in Flask Table?

Any help is greatly appreciated.

Upvotes: 0

Views: 529

Answers (1)

pjcunningham
pjcunningham

Reputation: 8046

Assuming an Sqlalchemy model Account with PK column id and a route defined as follows:

accounts = Blueprint('accounts', __name__)
@accounts.route("/accounts/<int:account_id>")
def detail(self, account_id):

    _account = Account.get_or_404(account_id)
    # show account details
    return render('account-details.html', account=_account)

Define your account link column as follows:

account = LinkCol(
    name='Account #',
    attr='id',
    endpoint='accounts.detail',
    url_kwargs=dict(account_id='id'),
)

The parameter meanings are as follows:

name # the column header text
attr # the name of the attribute of the account object to render as the text in the <td> cell
endpoint # the endpoint to link to
url_kwargs # the arguments that get passed to the url_for function along with the endpoint

The links would be constructed as if you had done a url_for for each row in the table as follows:

_row_account_detail_url = url_for('accounts.detail', account_id=row_id)

Example Table definition:

class AccountTable(Table):

    account = LinkCol(
        name='Account #',
        attr='id',
        endpoint='accounts.detail',
        url_kwargs=dict(account_id='id'),
    )

    service_address = Col(
        'Service Address',
        attr='service_address'
    )

    #  other columns

Upvotes: 1

Related Questions