Write-protect TabularAdapter Column

41 views Asked by At

I've a table in Enthought Python that's displayed with a TabularAdapter. Using "def get_edit()", I can set any row(s) to be protected (the user can't change the values), but I can't find a way to write-protect a column... Anyone have any suggestions, or want to point me in the right direction?

1

There are 1 answers

0
Steve76063 On

After contacting Enthought Support, they came up with the following:

This behavior can be achieved by defining the following method in your class inheriting from TabularAdapter:

def get_can_edit_cell(self, object, trait, row, column):
    return self._result_for('get_can_edit', object, trait, row, column)

Then in your class you can set individual columns to be non-editable with the following (replacing col with the Column ID of interest):

col_can_edit = Bool(False)

For instance, using the example here, you can make the age and name columns non-editable by adding the following to the ReportAdapter class:

age_can_edit = Bool(False)
name_can_edit = Bool(False)

def get_can_edit_cell(self, object, trait, row, column):
    return self._result_for('get_can_edit', object, trait, row, column) 

Hope this helps. Please let me know if you run into any issues getting this implemented.

I modified their example so I could get it to work with my numpy arrays (the ColumnID is an integer, not a string) like this:

class TestArrayAdapter1(TabularAdapter):

columns = [('Int1 #', 0), ('Int2', 1), ('Float3', 2)]

even_bg_color = 0xf4f4f4  # very light gray

width = 200

def get_format(self, object, name, row, column):
    formats = ['%d', '%d', '%.3f']
    return formats[column]

def get_can_edit_cell(self, object, trait, row, column):
    write_protected = [False, True, True]
    return write_protected[column]