I can move columns
This commit is contained in:
@@ -94,6 +94,13 @@ class Commands(BaseCommands):
|
||||
self._owner.set_column_width
|
||||
).htmx(target=None)
|
||||
|
||||
def move_column(self):
|
||||
return Command("MoveColumn",
|
||||
"Move column to new position",
|
||||
self._owner,
|
||||
self._owner.move_column
|
||||
).htmx(target=None)
|
||||
|
||||
|
||||
class DataGrid(MultipleInstance):
|
||||
def __init__(self, parent, settings=None, save_state=None, _id=None):
|
||||
@@ -173,18 +180,49 @@ class DataGrid(MultipleInstance):
|
||||
if col.col_id == col_id:
|
||||
col.width = int(width)
|
||||
break
|
||||
|
||||
|
||||
self._state.save()
|
||||
|
||||
|
||||
def move_column(self, source_col_id: str, target_col_id: str):
|
||||
"""Move column to new position. Called via Command from JS."""
|
||||
logger.debug(f"move_column: {source_col_id=} {target_col_id=}")
|
||||
|
||||
# Find indices
|
||||
source_idx = None
|
||||
target_idx = None
|
||||
for i, col in enumerate(self._state.columns):
|
||||
if col.col_id == source_col_id:
|
||||
source_idx = i
|
||||
if col.col_id == target_col_id:
|
||||
target_idx = i
|
||||
|
||||
if source_idx is None or target_idx is None:
|
||||
logger.warning(f"move_column: column not found {source_col_id=} {target_col_id=}")
|
||||
return
|
||||
|
||||
if source_idx == target_idx:
|
||||
return
|
||||
|
||||
# Remove source column and insert at target position
|
||||
col = self._state.columns.pop(source_idx)
|
||||
# Adjust target index if source was before target
|
||||
if source_idx < target_idx:
|
||||
self._state.columns.insert(target_idx, col)
|
||||
else:
|
||||
self._state.columns.insert(target_idx, col)
|
||||
|
||||
self._state.save()
|
||||
|
||||
def mk_headers(self):
|
||||
resize_cmd = self.commands.set_column_width()
|
||||
|
||||
move_cmd = self.commands.move_column()
|
||||
|
||||
def _mk_header_name(col_def: DataGridColumnState):
|
||||
return Div(
|
||||
mk.label(col_def.title, name="dt2-header-title"),
|
||||
cls="flex truncate cursor-default",
|
||||
)
|
||||
|
||||
|
||||
def _mk_header(col_def: DataGridColumnState):
|
||||
return Div(
|
||||
_mk_header_name(col_def),
|
||||
@@ -194,12 +232,13 @@ class DataGrid(MultipleInstance):
|
||||
data_tooltip=col_def.title,
|
||||
cls="dt2-cell dt2-resizable flex",
|
||||
)
|
||||
|
||||
|
||||
header_class = "dt2-row dt2-header" + "" if self._settings.header_visible else " hidden"
|
||||
return Div(
|
||||
*[_mk_header(col_def) for col_def in self._state.columns],
|
||||
cls=header_class,
|
||||
id=f"th_{self._id}"
|
||||
id=f"th_{self._id}",
|
||||
data_move_command_id=move_cmd.id
|
||||
)
|
||||
|
||||
def mk_body_cell_content(self, col_pos, row_index, col_def: DataGridColumnState, filter_keyword_lower=None):
|
||||
|
||||
Reference in New Issue
Block a user