enthought / enthought/traitsui

TabularEditor selected doesn't sync with removals

Open
#990 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
306
Forks
99
PR merge metrics
No merged PRs in 30d

Description

I'm not sure if this is a bug or intended behavior. Anyhow, if you have a List, represented in a View by a TabularEditor with selected configured on the TabularEditor, then the selected trait doesn't sync with removals from the list. This results in ghost selections. This is better illustrated by example:

```python
from traits.api import *
from traitsui.api import *

class Foo(HasTraits):
x = Str()

def __repr__(self):
return f''

class FooContainer(HasTraits):
foos = List(Instance(Foo))
selected = List
delete_selected = Button

def _foos_default(self):
return [Foo(x=str(i)) for i in range(10)]

def _delete_selected_fired(self):
self.foos = [f for f in self.foos if f not in self.selected]

@on_trait_change('selected[]')
def _say_hi(self, obj, name, old, new):
print('hi', old, new)

traits_view = View(
UItem('delete_selected'),
UItem(
'foos',
editor=TabularEditor(
selected='selected',
multi_select=True,
adapter=TabularAdapter(
columns=[('x', 'x')]
)
)
)
)

FooContainer().configure_traits()
```

Of course, you can include something like:
```python
@on_trait_change('foos[]')
def _handle_selected_removal(self):
# Get all selections that aren't in the list any longer...
removals = [foo for foo in self.selected if foo not in self.foos]
# ... and remove them from the selection.
self.selected = [foo for foo in self.selected if foo not in removals]
```
to get this to work, but I'm not sure if this is intended to be handled by TabularEditor itself.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.