I can add a new column and a new row
This commit is contained in:
@@ -151,7 +151,7 @@ class Command:
|
||||
before_commands = [bc for bc in self.owner.get_bound_commands(self.name) if bc.when == "before"]
|
||||
for bound_cmd in before_commands:
|
||||
logger.debug(f" will execute bound command {bound_cmd.command.name} BEFORE...")
|
||||
r = bound_cmd.command.execute() # client_response should not be forwarded as it's not the same command
|
||||
r = bound_cmd.command.execute(client_response)
|
||||
ret_from_before_commands.append(r)
|
||||
|
||||
# Execute main callback
|
||||
@@ -166,7 +166,7 @@ class Command:
|
||||
after_commands = [bc for bc in self.owner.get_bound_commands(self.name) if bc.when == "after"]
|
||||
for bound_cmd in after_commands:
|
||||
logger.debug(f" will execute bound command {bound_cmd.command.name} AFTER...")
|
||||
r = bound_cmd.command.execute() # client_response should not be forwarded as it's not the same command
|
||||
r = bound_cmd.command.execute(client_response)
|
||||
ret_from_after_commands.append(r)
|
||||
|
||||
all_ret = flatten(ret, ret_from_before_commands, ret_from_after_commands, collector.results)
|
||||
|
||||
@@ -6,6 +6,7 @@ ROUTE_ROOT = "/myfasthtml"
|
||||
|
||||
# Datagrid
|
||||
ROW_INDEX_ID = "__row_index__"
|
||||
ROW_SELECTION_ID = "__row_selection__"
|
||||
DATAGRID_DEFAULT_COLUMN_WIDTH = 100
|
||||
DATAGRID_PAGE_SIZE = 1000
|
||||
FILTER_INPUT_CID = "__filter_input__"
|
||||
@@ -19,6 +20,7 @@ class Routes:
|
||||
|
||||
|
||||
class ColumnType(Enum):
|
||||
RowSelection_ = "RowSelection_"
|
||||
RowIndex = "RowIndex"
|
||||
Text = "Text"
|
||||
Number = "Number"
|
||||
@@ -29,6 +31,10 @@ class ColumnType(Enum):
|
||||
Formula = "Formula"
|
||||
|
||||
|
||||
def get_columns_types() -> list[ColumnType]:
|
||||
return [c for c in ColumnType if not c.value.endswith("_")]
|
||||
|
||||
|
||||
class ViewType(Enum):
|
||||
Table = "Table"
|
||||
Chart = "Chart"
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import logging
|
||||
import uuid
|
||||
from typing import Optional
|
||||
from typing import Optional, Literal
|
||||
|
||||
from myfasthtml.controls.helpers import Ids
|
||||
from myfasthtml.core.commands import BoundCommand
|
||||
from myfasthtml.core.commands import BoundCommand, Command
|
||||
from myfasthtml.core.constants import NO_DEFAULT_VALUE
|
||||
from myfasthtml.core.utils import pascal_to_snake, get_class, snake_to_pascal
|
||||
|
||||
@@ -113,7 +113,10 @@ class BaseInstance:
|
||||
parent = self.get_parent()
|
||||
return parent.get_full_id() if parent else None
|
||||
|
||||
def bind_command(self, command, command_to_bind, when="after"):
|
||||
def bind_command(self,
|
||||
command: Command | str | tuple | list,
|
||||
command_to_bind,
|
||||
when: Literal["before", "after"] = "after"):
|
||||
"""
|
||||
Bind a command to another command.
|
||||
|
||||
@@ -126,18 +129,28 @@ class BaseInstance:
|
||||
Duplicate bindings are automatically prevented using two mechanisms:
|
||||
1. Check if the same binding already exists
|
||||
"""
|
||||
command_name = command.name if hasattr(command, "name") else command
|
||||
|
||||
# Protection 1: Check if this binding already exists to prevent duplicates
|
||||
existing_bindings = self._bound_commands.get(command_name, [])
|
||||
for existing in existing_bindings:
|
||||
if existing.command.name == command_to_bind.name and existing.when == when:
|
||||
# Binding already exists, don't add it again
|
||||
return
|
||||
|
||||
# Add new binding
|
||||
bound = BoundCommand(command=command_to_bind, when=when)
|
||||
self._bound_commands.setdefault(command_name, []).append(bound)
|
||||
|
||||
def _bind(_command_name, _command_to_bind, _when):
|
||||
# Check if this binding already exists to prevent duplicates
|
||||
existing_bindings = self._bound_commands.get(_command_name, [])
|
||||
for existing in existing_bindings:
|
||||
if existing.command.name == _command_to_bind.name and existing.when == _when:
|
||||
return # Binding already exists, don't add it again
|
||||
|
||||
# Add new binding
|
||||
bound = BoundCommand(command=_command_to_bind, when=_when)
|
||||
self._bound_commands.setdefault(command_name, []).append(bound)
|
||||
|
||||
commands = [command] if isinstance(command, (str, Command)) else command
|
||||
for c in commands:
|
||||
command_name = c.name if hasattr(c, "name") else c
|
||||
_bind(c, command_to_bind, when)
|
||||
|
||||
def unbind_command(self, command: Command | str | tuple | list):
|
||||
commands = [command] if isinstance(command, (str, Command)) else command
|
||||
for c in commands:
|
||||
command_name = c.name if hasattr(c, "name") else c
|
||||
self._bound_commands.pop(command_name, None)
|
||||
|
||||
def get_bound_commands(self, command_name):
|
||||
return self._bound_commands.get(command_name, [])
|
||||
|
||||
Reference in New Issue
Block a user