Fill data grid from json

Hello,
Regarding the below code, please help me with:

  1. how I will add data from json in datagrid (for each column separately) and editing after
  2. how I will set lost focus (for future data validation) in somes textboxes.

class Form1(Form1Template):
def init(self, **properties):

Set Form properties and Data Bindings.

self.init_components(**properties)

# Any code you write here will run when the form opens.
grid = DataGrid()    
self.add_component(grid)    
grid.columns = [
{ "id": "A", "title": "Cont", "data_key": "column_1",'width': '90' },
{ "id": "B", "title": "Denumire", "data_key": "column_2",'width': '150'},  
{ "id": "C", "title": "Cod", "data_key": "column_3",'width': '40'},  
{ "id": "D", "title": "Debit", "data_key": "column_4",'width': '100' },
{ "id": "E", "title": "Credit", "data_key": "column_5",'width': '100' },
{ "id": "F", "title": "Debit", "data_key": "column_6",'width': '100' },
{ "id": "G", "title": "Credit", "data_key": "column_7",'width': '100' },  
{ "id": "H", "title": "Debit", "data_key": "column_8",'width': '100' },
{ "id": "I", "title": "Credit", "data_key": "column_9",'width': '100' }
]

self.text_box = []
row = DataRowPanel()   
for i in range(1,10):
  for col in grid.columns:
    k=1
    txb = self.add_tb(k)
    row.add_component(txb, column=col["id"])
    txb.set_event_handler("...lost focus", self.ver())??? - 
    k=k+1
grid.add_component(row)    
grid.show_page_controls = True
grid.rows_per_page = 20
grid.role = "wide" 

def add_tb(self,k):
tb = TextBox( font=“Arial” )
tb.role = “input”
tb.tag.name = k
return tb
pass

def ver(self):
print(“b”)
pass

Completely managing a DataGrid from code is not easy.

I tried when I created the DataGridJson custom component, but then I decided to go with a hybrid solution, with two templates.

Try to have a look at this, maybe you will find some of the answers you are looking for: DataGridJson - A DataGrid with quick simple setup

1 Like

Thx Man, nice work! I will study.
Before json, I can’t set events on some textboxes from grid

No way with data grid! I finded a good solution on the forum and I have “lost focus” in grid panel, on each textbox. Any ideas to charge json without loop?
def… or init in form
self.grid_panel_1.visible
global txb
txb = self.txb = {}
global i
global j
i = 1
j = 0
for j in range (2,20): # rows
k = j * 10
for i in range(1, 9): # col
k=j*10+i
self.txb[k] = TextBox(type=“text”, font=“Arial”,
foreground="#000",background="#fff",placeholder=f"place{k}")
self.txb[k].role = “input” #“form-control”
self.txb[k].tag.name = k
if i == 1 or i == 2:
self.grid_panel_1.add_component(self.txb[k], row=j, col_xs=0, width_xs=1)
if i == 3:
self.grid_panel_1.add_component(self.txb[k], row=j, col_xs=0, width_xs=2)
if i > 3 :
self.grid_panel_1.add_component(self.txb[k], row=j, col_xs=0, width_xs=2)
self.txb[k].set_event_handler(‘lost_focus’, self.l_focus)
pass

def l_focus (self, sender,**event_args):
print(sender)
print(sender.tag.name)
if not sender.text:
pass
else:
S = sender.text
if S.isdigit() or (S.startswith("-") and S[1:].isdigit()):
pass
else:
sender.text = 0
alert(“Just numbers without decimal”)
pass

def button_2_click(self, **event_args):
#print(self.grid_panel_1.txb24.text)
#for t in self.grid_panel_1.get_components():
# if type(t) is Label:
# print (“x”)
#print(self.place24.text)
print(self.txb[24].text)
pass

I suggest you check out the Tabulator component @stucork developed:

3 Likes