為uniDBGrid設置文字操作欄,如下圖的效果,用戶點擊審核,執行審核代碼,點退回,執行退回代碼:
對於Web應用界面,這是最常見的方式,那對於我等Delphi開發者來說,基於uniGUI該怎么實現呢?
首先,為uniDBGrid准備“操作”這一欄的顯示內容,具體說,在數據集中准備好顯示的數據。
我是為數據集加了一列,叫operation。
然后在operation的TField.OnGetText中返回顯示的數據:
<a class="btnopbtn" style="color: #148a14" onclick="ajaxRequest(O430,'audit',['id=C42EBDFDDA9B4526B67EECE72FD7AF5F']);">審核</a>|<a class="btnopbtn" style="color: #f94936" onclick="ajaxRequest(O430,'unaudit',['id=C42EBDFDDA9B4526B67EECE72FD7AF5F']);">退回</a>
對應的Delphi代碼:
procedure TCustomBillBrowserFrame.OperationFieldGetText(Sender: TField; var Text: string; DisplayText: Boolean); begin inherited; Text:= '<a class="btnopbtn" style="color: #148a14" onclick="ajaxRequest('+UniDBGrid1.JSName+',' + QuotedStr('audit')+',[''id='+Sender.DataSet.FieldByName('fid').AsString+''']);">'+'審核</a>|' + '<a class="btnopbtn" style="color: #f94936" onclick="ajaxRequest('+UniDBGrid1.JSName+',' + QuotedStr('unaudit')+',[''id='+Sender.DataSet.FieldByName('fid').AsString+''']);">'+'退回</a>'; end;
要說明的是:
1.ajaxRequest會觸發對應控件的OnAjaxEvent事件
2.具體是哪一個控件呢,由ajaxRequest的第一個參數決定,那這里我們要寫成UniDBGrid1.JSName
3.第二個audit:對應EventName,第三個參數對應Params,參考下面的代碼:
procedure TCustomBillBrowserFrame.UniDBGrid1AjaxEvent(Sender: TComponent; EventName: string; Params: TUniStrings); begin inherited; // end;
明白上面的原理,可以直接看下面的實際代碼了:
procedure TAudit2YmJHSBFrame.UniDBGrid1AjaxEvent(Sender: TComponent; EventName: string; Params: TUniStrings); var id: string; begin inherited; // uniSession.Log('EventName='+EventName); if EventName = 'audit' then begin id := Params['id'].AsString; //執行審核操作的代碼,這里省了... end; if EventName = 'unaudit' then begin id := Params['id'].AsString; //執行退回操作的代碼,這里省了 end; end;
上面內容是汗血寶寶教我的,感謝他無私的指導!
補充:
一定要重新定位對應的數據集,要清楚,你點擊的單元格所在的行,與uniDBGrid對應數據集的當前記錄不一定是對應的。