enthought / enthought/traitsui

Avoid creating a row if the `TableEditor.row_factory` callable returns None

Open
#1,940 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

Feature request.

Use case: when the user right clicks on a `TableEditor` and selects "Add new item", I would like to open a dialog where the user can customize the new object before it gets added to the table. However, I would also like the user the option to dismiss the creation.

Currently, if I make the `row_factory` return `None`, a row of _None_ will be added. If I make it `raise` on dismissal, a traceback will be leaked to the console.

```python
from traits.api import HasStrictTraits, Str, Int, List, Instance
from traitsui.api import TableEditor, UItem, View
from pyface.api import choose_one

class Vehicle(HasStrictTraits):
name = Str()
type = Str()
year = Int(2022)

def initialize_vehicle():
""" Returns a Vehicle unless the user dismisses, in which case returns None"""
type_ = choose_one(None, "type", ["bike", "car", "scooter"])
if type_ is None:
return None
return Vehicle(type=type_)

class Garage(HasStrictTraits):
vehicles = List(Instance(Vehicle))
traits_view = View(
UItem(
"vehicles",
editor=TableEditor(
row_factory=initialize_vehicle,
sortable=False
)
), width=300, height=180
)

Garage(vehicles=[Vehicle(name="Lalaa", type="bike", year=1999)]).configure_traits()
```

![Peek 2022-09-29 17-40](https://user-images.githubusercontent.com/7642150/193089547-53082121-d1f3-4384-8a86-5947d1093ce6.gif)

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.