| 67 | With the default settings two buttons are provided, Open and Delete. Additional buttons can be added by using the actions, or more fully, response.s3.actions. The following code will add an "Import" button |
| 68 | {{{ |
| 69 | #!div style="font-size: 80%" |
| 70 | Add this to the postp() function of the controller: |
| 71 | {{{#!python |
| 72 | def postp(r, output): |
| 73 | response.s3.actions = [ |
| 74 | dict(label=str(UPDATE), _class="action-btn", |
| 75 | url=str(URL(r=request, |
| 76 | c=module, |
| 77 | f=resourcename, |
| 78 | args=["[id]", "update"]))), |
| 79 | dict(label=str(DELETE), _class="action-btn", |
| 80 | url=str(URL(r=request, |
| 81 | c=module, |
| 82 | f=resourcename, |
| 83 | args=["[id]", "delete"]))), |
| 84 | dict(label=str(T("Import")), _class="action-btn", |
| 85 | url=str(URL(r=request, |
| 86 | c=module, |
| 87 | f=resourcename, |
| 88 | args=["[id]", "import"]))), |
| 89 | ] |
| 90 | return output |
| 91 | }}} |
| 92 | Notice that before the Open and Delete buttons were provided, now they need to be explicitly added. |
| 93 | [[BR]] |
| 94 | [[Image(DataTable_Plus_Import_Btn.png)]] |
| 95 | [[BR]] |
| 97 | Maybe you don't want the ability to import every file. Such as files that are older than a certain date. |
| 98 | {{{ |
| 99 | #!div style="font-size: 80%" |
| 100 | Add this to the postp() function of the controller: |
| 101 | {{{#!python |
| 102 | def postp(r, output): |
| 103 | from datetime import tzinfo, timedelta, datetime |
| 104 | cutoffDate = datetime.now() - timedelta(days=30) |
| 105 | query = (r.table.created_on > cutoffDate) |
| 106 | rows = db(query).select(r.table.id) |
| 107 | restrict = [str(row.id) for row in rows] |
| 108 | response.s3.actions = [ |
| 109 | dict(label=str(UPDATE), _class="action-btn", |
| 110 | url=str(URL(r=request, |
| 111 | c=module, |
| 112 | f=resourcename, |
| 113 | args=["[id]", "update"]))), |
| 114 | dict(label=str(DELETE), _class="action-btn", |
| 115 | url=str(URL(r=request, |
| 116 | c=module, |
| 117 | f=resourcename, |
| 118 | args=["[id]", "delete"]))), |
| 119 | dict(label=str(T("Import")), _class="action-btn", |
| 120 | url=str(URL(r=request, |
| 121 | c=module, |
| 122 | f=resourcename, |
| 123 | args=["[id]", "import"])), |
| 124 | restrict = restrict |
| 125 | ), |
| 126 | ] |
| 127 | return output |
| 128 | }}} |
| 129 | The key is the restrict argument that is passed to the Import button. This is set up as a list of record ids that will display the button. |
| 130 | [[BR]] |
| 131 | [[Image(DataTable_Plus_Cond_Import_Btn.png)]] |
| 132 | [[BR]] |
| 133 | |