Add support for "is row checkable" in grids

i.e. when grid has checkboxes, some rows maybe shouldn't get one
This commit is contained in:
Lance Edgar 2022-12-20 19:14:54 -06:00
parent e427e50d67
commit 871ea84f96
3 changed files with 85 additions and 2 deletions

View file

@ -69,6 +69,74 @@ class Grid(object):
""" """
Core grid class. In sore need of documentation. Core grid class. In sore need of documentation.
.. _Buefy docs: https://buefy.org/documentation/table/
.. attribute:: checkable
Optional callback to determine if a given row is checkable,
i.e. this allows hiding checkbox for certain rows if needed.
This may be either a Python callable, or string representing a
JS callable. If the latter, according to the `Buefy docs`_:
.. code-block:: none
Custom method to verify if a row is checkable, works when is
checkable.
Function (row: Object)
In other words this JS callback would be invoked for each data
row in the client-side grid.
But if a Python callable is used, then it will be invoked for
each row object in the server-side grid. For instance::
def checkable(obj):
if obj.some_property == True:
return True
return False
grid.checkable = checkable
.. attribute:: check_handler
Optional JS callback for the ``@check`` event of the underlying
Buefy table component. See the `Buefy docs`_ for more info,
but for convenience they say this (as of writing):
.. code-block:: none
Triggers when the checkbox in a row is clicked and/or when
the header checkbox is clicked
For instance, you might set ``grid.check_handler =
'rowChecked'`` and then define the handler within your template
(e.g. ``/widgets/index.mako``) like so:
.. code-block:: none
<%def name="modify_this_page_vars()">
${parent.modify_this_page_vars()}
<script type="text/javascript">
TailboneGrid.methods.rowChecked = function(checkedList, row) {
if (!row) {
console.log("no row, so header checkbox was clicked")
} else {
console.log(row)
if (checkedList.includes(row)) {
console.log("clicking row checkbox ON")
} else {
console.log("clicking row checkbox OFF")
}
}
console.log(checkedList)
}
</script>
</%def>
.. attribute:: raw_renderers .. attribute:: raw_renderers
Dict of "raw" field renderers. See also Dict of "raw" field renderers. See also
@ -117,6 +185,7 @@ class Grid(object):
sortable=False, sorters={}, default_sortkey=None, default_sortdir='asc', sortable=False, sorters={}, default_sortkey=None, default_sortdir='asc',
pageable=False, default_pagesize=None, default_page=1, pageable=False, default_pagesize=None, default_page=1,
checkboxes=False, checked=None, check_handler=None, check_all_handler=None, checkboxes=False, checked=None, check_handler=None, check_all_handler=None,
checkable=None,
clicking_row_checks_box=False, click_handlers=None, clicking_row_checks_box=False, click_handlers=None,
main_actions=[], more_actions=[], delete_speedbump=False, main_actions=[], more_actions=[], delete_speedbump=False,
ajax_data_url=None, component='tailbone-grid', ajax_data_url=None, component='tailbone-grid',
@ -175,6 +244,7 @@ class Grid(object):
self.checked = lambda item: False self.checked = lambda item: False
self.check_handler = check_handler self.check_handler = check_handler
self.check_all_handler = check_all_handler self.check_all_handler = check_all_handler
self.checkable = checkable
self.clicking_row_checks_box = clicking_row_checks_box self.clicking_row_checks_box = clicking_row_checks_box
self.click_handlers = click_handlers or {} self.click_handlers = click_handlers or {}
@ -1365,6 +1435,7 @@ class Grid(object):
count = len(raw_data) count = len(raw_data)
# iterate over data rows # iterate over data rows
checkable = self.checkboxes and self.checkable and callable(self.checkable)
for i in range(count): for i in range(count):
rowobj = raw_data[i] rowobj = raw_data[i]
@ -1372,6 +1443,11 @@ class Grid(object):
# logic finds it useful # logic finds it useful
row = {'_index': i} row = {'_index': i}
# if grid allows checkboxes, and we have logic to see if
# any given row is checkable, add data for that here
if checkable:
row['_checkable'] = self.checkable(rowobj)
# sometimes we need to include some "raw" data columns in our # sometimes we need to include some "raw" data columns in our
# result set, even though the column is not displayed as part of # result set, even though the column is not displayed as part of
# the grid. this can be used for front-end editing of row data for # the grid. this can be used for front-end editing of row data for

View file

@ -192,12 +192,17 @@
% if grid.check_all_handler: % if grid.check_all_handler:
@check-all="${grid.check_all_handler}" @check-all="${grid.check_all_handler}"
% endif % endif
## TODO: definitely will be wanting this... % if isinstance(grid.checkable, six.string_types):
## :is-row-checkable="" :is-row-checkable="${grid.row_checkable}"
% elif grid.checkable:
:is-row-checkable="row => row._checkable"
% endif
% if grid.sortable:
:default-sort="[sortField, sortOrder]" :default-sort="[sortField, sortOrder]"
backend-sorting backend-sorting
@sort="onSort" @sort="onSort"
% endif
:paginated="paginated" :paginated="paginated"
:per-page="perPage" :per-page="perPage"

View file

@ -451,6 +451,7 @@ class MasterView(View):
'url': lambda obj: self.get_action_url('view', obj), 'url': lambda obj: self.get_action_url('view', obj),
'checkboxes': checkboxes, 'checkboxes': checkboxes,
'checked': self.checked, 'checked': self.checked,
'checkable': self.checkbox,
'clicking_row_checks_box': self.clicking_row_checks_box, 'clicking_row_checks_box': self.clicking_row_checks_box,
'assume_local_times': self.has_local_times, 'assume_local_times': self.has_local_times,
} }
@ -2765,6 +2766,7 @@ class MasterView(View):
def get_effective_query(self, session=None, **kwargs): def get_effective_query(self, session=None, **kwargs):
return self.get_effective_data(session=session, **kwargs) return self.get_effective_data(session=session, **kwargs)
# TODO: should rename to checkable?
def checkbox(self, instance): def checkbox(self, instance):
""" """
Returns a boolean indicating whether ot not a checkbox should be Returns a boolean indicating whether ot not a checkbox should be