Skip to content

Commit c4ca963

Browse files
committed
refactors to have get_selected_rows
1 parent a08867a commit c4ca963

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

ngwidgets/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.22.1"
1+
__version__ = "0.22.2"

ngwidgets/lod_grid.py

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -207,24 +207,38 @@ def handle_exception(self, ex: Exception) -> None:
207207
type="error",
208208
)
209209

210+
def get_index(self, lenient: bool = False,lod=None,key_col:str=None):
211+
"""
212+
get and index (dict of dicts) of the given list of
213+
dicts based on the given key column - if no key_col is given
214+
index by row number starting from 1
215+
"""
216+
lod_index = {}
217+
if lod is None:
218+
lod=self.lod
219+
if lod:
220+
for row_index, row in enumerate(lod):
221+
if key_col is None:
222+
lod_index[row_index+1] = row
223+
else:
224+
if self.config.key_col in row:
225+
key_value = row[key_col]
226+
lod_index[key_value] = row
227+
else:
228+
msg = f"missing key column {self.config.key_col} in row {row_index}"
229+
if not lenient:
230+
raise Exception(msg)
231+
else:
232+
print(msg, file=sys.stderr)
233+
# missing key
234+
pass
235+
return lod_index
236+
210237
def update_index(self, lenient: bool = False):
211238
"""
212-
update the index based on the given key column
239+
update my index (with view records)
213240
"""
214-
self.lod_index = {}
215-
if self.lod:
216-
for row_index, row in enumerate(self.lod):
217-
if self.config.key_col in row:
218-
key_value = row[self.config.key_col]
219-
self.lod_index[key_value] = row
220-
else:
221-
msg = f"missing key column {self.config.key_col} in row {row_index}"
222-
if not lenient:
223-
raise Exception(msg)
224-
else:
225-
print(msg, file=sys.stderr)
226-
# missing key
227-
pass
241+
self.lod_index=self.get_index(lenient=lenient,lod=self.lod,key_col=self.config.key_col)
228242

229243
def get_row_for_key(self, key_value: str):
230244
"""
@@ -371,6 +385,21 @@ async def get_selected_rows(self):
371385
selected_rows = await self.ag_grid.get_selected_rows()
372386
return selected_rows
373387

388+
async def get_selected_lod(self,lod_index=None):
389+
"""
390+
selected rows are in view (e.g. potentially html) format
391+
get back the original list of dict rows
392+
"""
393+
selected_lod = []
394+
selected_rows = await self.get_selected_rows()
395+
if lod_index is None:
396+
lod_index=self.get_index(lenient=self.config.lenient,lod=self.lod)
397+
for row in selected_rows:
398+
key_value = row[self.config.key_col]
399+
record = lod_index[key_value]
400+
selected_lod.append(record)
401+
return selected_lod
402+
374403
def select_all_rows(self):
375404
"""
376405
select all my ag_grid rows

0 commit comments

Comments
 (0)