This commit is contained in:
yumoqing 2020-11-12 14:28:46 +08:00
parent 1f5a8d93de
commit 9333cd9bd4
2 changed files with 567 additions and 571 deletions

View File

@ -49,7 +49,9 @@ class BoxViewer(BoxLayout):
if options.get('toolbar'): if options.get('toolbar'):
self.toolbar = Toolbar(options['toolbar']) self.toolbar = Toolbar(options['toolbar'])
lopts = options['dataloader'].copy() lopts = options['dataloader'].copy()
self.dataloader = RelatedLoader(target=self,**lopts['options']) if lopts.get('options'):
lopts = lopts.get('options')
self.dataloader = RelatedLoader(target=self,**lopts)
self.dataloader.bind(on_deletepage=self.deleteWidgets) self.dataloader.bind(on_deletepage=self.deleteWidgets)
self.dataloader.bind(on_pageloaded=self.addPageWidgets) self.dataloader.bind(on_pageloaded=self.addPageWidgets)
self.dataloader.bind(on_newbegin=self.deleteAllWidgets) self.dataloader.bind(on_newbegin=self.deleteAllWidgets)
@ -75,6 +77,7 @@ class BoxViewer(BoxLayout):
def addPageWidgets(self,o,data): def addPageWidgets(self,o,data):
widgets = [] widgets = []
recs = data['data'] recs = data['data']
page = data['page']
dir = data['dir'] dir = data['dir']
idx = 0 idx = 0
if dir == 'up': if dir == 'up':
@ -83,7 +86,7 @@ class BoxViewer(BoxLayout):
for r in recs: for r in recs:
self.showObject(widgets, r, index=idx) self.showObject(widgets, r, index=idx)
self.dataloader.bufferObjects(widgets) self.dataloader.bufferObjects(page, widgets)
x = self.dataloader.getLocater() x = self.dataloader.getLocater()
self.locater(x) self.locater(x)

View File

@ -147,6 +147,7 @@ class Body(ScrollWidget):
row.row_id = id row.row_id = id
self.add_widget(row,index=index) self.add_widget(row,index=index)
self.idRow[id] = row self.idRow[id] = row
return row
def clearRows(self): def clearRows(self):
self.idRow = {} self.idRow = {}
@ -217,7 +218,7 @@ class DataGridPart(WidgetReady, BoxLayout):
return self.body.clearRows() return self.body.clearRows()
def addRow(self,id, data): def addRow(self,id, data):
self.body.addRow(id, data) return self.body.addRow(id, data)
class DataGrid(WidgetReady, BoxLayout): class DataGrid(WidgetReady, BoxLayout):
row_selected = BooleanProperty(False) row_selected = BooleanProperty(False)
@ -260,30 +261,18 @@ class DataGrid(WidgetReady, BoxLayout):
if self.normal_part: if self.normal_part:
b.add_widget(self.normal_part) b.add_widget(self.normal_part)
self.add_widget(b) self.add_widget(b)
if self.options.get('paging',False): ldr_desc = options.get('dataloader')
self.loader = Paging(adder=self.addRow, if not ldr_desc:
clearer=self.clearRows, raise Exception('DataGrid need a DataLoader')
dataurl=self.dataUrl, self.dataloader = RelatedLoader(target=self, **ldr_desc)
target=self, self.dataloader.bind(on_deletepage=self.delete_page)
params=self.params, self.dataloader.bind(on_pageloaded=self.add_page)
method=self.options.get('method','GET') self.dataloader.bind(on_newbegin=self.clearRows)
)
self.add_widget(self.loader.widget)
else:
self.loader = RelatedLoader(adder=self.addRow,
remover=self.delRow,
locater=self.setScrollPosition,
page_rows=self.page_rows,
dataurl=self.dataUrl,
target=self,
params=self.params,
method=self.options.get('method','GET')
)
self.on_sizeTask = None self.on_sizeTask = None
self.register_event_type('on_selected') self.register_event_type('on_selected')
self.register_event_type('on_scrollstop') self.register_event_type('on_scrollstop')
def setScrollPosition(self,pos): def locater(self,pos):
self.normal_part.body.scroll_y = pos self.normal_part.body.scroll_y = pos
if self.freeze_part: if self.freeze_part:
self.freeze_part.body.scroll_y = pos self.freeze_part.body.scroll_y = pos
@ -300,8 +289,6 @@ class DataGrid(WidgetReady, BoxLayout):
if self.freeze_part and o == self.freeze_part.body: if self.freeze_part and o == self.freeze_part.body:
self.normal_part.body.scroll_y = o.scroll_y self.normal_part.body.scroll_y = o.scroll_y
if self.options.get('paging'):
return
if o.scroll_y <= 0.001: if o.scroll_y <= 0.001:
self.loader.loadNextPage() self.loader.loadNextPage()
if o.scroll_y >= 0.999: if o.scroll_y >= 0.999:
@ -343,33 +330,38 @@ class DataGrid(WidgetReady, BoxLayout):
return 60 return 60
return self.show_rows return self.show_rows
def init(self,t):
if self.options.get('dataurl'):
self.loader.loadPage(1)
else:
data = self.options.get('data',[])
if len(data) > 0:
loading(self)
for d in data:
self.addRow()
loaded(self)
def clearRows(self): def clearRows(self):
if self.freeze_part: if self.freeze_part:
self.freeze_part.body.clearRows() self.freeze_part.body.clearRows()
self.normal_part.body.clearRows() self.normal_part.body.clearRows()
def add_page(self,o,data):
ids = []
recs = data['data']
page = data['page']
dir = data['dir']
idx = 0
if dir == 'up':
recs.reverse()
idx = -1
for r in recs:
ids.append(self.addRow(r,index=idx))
self.dataloader.bufferObjects(page,ids)
x = self.dataloader.getLocater()
self.locater(x)
def delete_page(self,o,data):
for id in data:
self.delRow(id)
def addRow(self,data, **kw): def addRow(self,data, **kw):
id = getID() id = getID()
f_row = None
if self.freeze_part: if self.freeze_part:
self.freeze_part.body.addRow(id, data, **kw) self.freeze_part.body.addRow(id, data, **kw)
self.normal_part.body.addRow(id, data, **kw) self.normal_part.body.addRow(id, data, **kw)
return id return id
def setData(self,data):
for d in data:
self.addRow(d)
def delRow(self,id,**kw): def delRow(self,id,**kw):
if self.freeze_part: if self.freeze_part:
self.freeze_part.body.delRowById(id) self.freeze_part.body.delRowById(id)
@ -405,3 +397,4 @@ class DataGrid(WidgetReady, BoxLayout):
def on_ready(self,o,v=None): def on_ready(self,o,v=None):
print('***********onRadey*************') print('***********onRadey*************')
Factory.register('DataGrid',DataGrid)