步骤如下:
1.下载最新jquery的js包(http://jquery.com/)并拷贝至tomcat的genexus工程下,和setevent.js等文件同一目录。因为jquery并不是genexus自带的js包,所以发布时需要手动拷贝过去。
2.新建webpanner名为:hwbp01,画面上追加一个按钮名为:Button1,在Genexus的Start事件里加入如下代码:
- Event Start
- &WS_SESSION.Set('TEST_SESSION_VALUE', '0')
- W_V_SAVE_SESSION_URL = "http://localhost:8080/SCRM4U/servlet/hwbp01"
- W_V_SAVE_SESSION_EVT = "E'TESTEVENT'."
- FORM.HeaderRawHTML = '<script language=javascript src="/SCRM4U/jquery.js"></script>' + newline()
- + '<script language=javascript>' + newline()
- + 'function goto_history(){' + newline()
- + ' $.ajax({ ' + newline()
- + ' type: "post", ' + newline()
- + ' async : false, ' + newline()
- + ' url: "' + &W_V_SAVE_SESSION_URL + '", ' + newline()
- + ' data: "_EventName=' + &W_V_SAVE_SESSION_EVT + '", ' + newline()
- + ' success: function(response){ ' + newline()
- + ' alert(response); ' + newline()
- + ' var url = "/SCRM4U/servlet/hwbp02";' + newline()
- + ' window.open(url,'+ newline()
- + ' "入力履歴参照",'+ newline()
- + ' "height=600,width=850,top=30,left=90,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=no,status=yes");' + newline()
- + ' } ' + newline()
- + ' }); ' + newline()
- + '}' + newline()
- + '</script>' + newline()
- Button1.JSEvent('onclick', 'goto_history()')
- EndEvent // Start
另外还需追加一个Test事件:
- Event 'TestEvent'
- &WS_SESSION.Set('TEST_SESSION_VALUE', '1')
- EndEvent // 'TestEvent'
新建另一个webpanner名为:hwbp02,Start事件如下:
- Event Start
- W_V_TESTVALUE = &WS_SESSION.Get('TEST_SESSION_VALUE')
- EndEvent // Start
=====================================================================================
请注意:
以上为触发ajax的按钮在Grid外的情况,如果在Grid里面(即每行都有一个按钮),因为参数中涉及到行数,方法有所不同。
假设ajax将触发后台的事件为:
- Event 'HistoryEvent'
- P_V_OPEN_SQ = &WS_SESSION.Get('P_V_OPEN_SQ')
- P_V_OPEN_NO = &WS_SESSION.Get('P_V_OPEN_NO')
- // &W_V_ROW = &WEB_REQUEST.GetVariable('_EventName')
- // &W_FIRST = &W_V_ROW.IndexOf('.')
- // &W_LAST = &W_V_ROW.Length()
- // &W_N_ROW_NUM = Val(&W_V_ROW.Substring(&W_FIRST + 1,&W_LAST))
- //
- // do 'CHANGE_DATAGRID_SUB'
- // do 'ORDER_CAPTION_INITIAL'
- //
- // &W_N_CNT = 1
- // for each line in grd_enr_info
- // if &W_N_CNT = &W_N_ROW_NUM
- // &WS_SESSION.Set('TESTVALUE1', &D_V_GRD_SCRG_NO)
- // &WS_SESSION.Set('TESTVALUE2', &D_V_GRD_PSN_NM)
- // &WS_SESSION.Set('TESTVALUE3', Trim(&P_V_OPEN_SQ))
- // &WS_SESSION.Set('TESTVALUE4', &P_V_OPEN_NO)
- // &WS_SESSION.Set('TESTVALUE5', &D_V_OPN_SBJ_NM)
- // exit
- // endif
- // &W_N_CNT = &W_N_CNT + 1
- // endfor
- &WS_SESSION.Set('TESTVALUE1', &D_V_GRD_SCRG_NO)
- &WS_SESSION.Set('TESTVALUE2', &D_V_GRD_PSN_NM)
- &WS_SESSION.Set('TESTVALUE3', Trim(&P_V_OPEN_SQ))
- &WS_SESSION.Set('TESTVALUE4', &P_V_OPEN_NO)
- &WS_SESSION.Set('TESTVALUE5', &D_V_OPN_SBJ_NM)
- EndEvent // 'HistoryEvent'
StartEvent中的代码为:
- W_V_SAVE_SESSION_URL = "/SCRM4U/servlet/hwbp03"
- W_V_SAVE_SESSION_EVT = "E'HISTORYEVENT'."
- FORM.HeaderRawHTML = '<script language=javascript src="/SCRM4U/jquery.js"></script>' + newline()
- + '<script language=javascript>' + newline()
- + 'function goto_history(num){' + newline()
- + ' var no = num.replace(/(^/s*)|(/s*$)/g, "").substr(1); ' + newline()
- + ' var pgrd = document.MAINFORM.nRC_Grd_enr_info.value; ' + newline()
- + ' var subj = document.MAINFORM._D_V_OPN_SBJ_NM.value; ' + newline()
- + ' var _D_V_GRD_SCRG_NO_X = "_D_V_GRD_SCRG_NO_"+no; ' + newline()
- + ' var _D_V_GRD_PSN_NM_X = "_D_V_GRD_PSN_NM_"+no; ' + newline()
- + ' var scrg = eval("document.MAINFORM." + _D_V_GRD_SCRG_NO_X).value; ' + newline()
- + ' var psnm = eval("document.MAINFORM." + _D_V_GRD_PSN_NM_X).value; ' + newline()
- + ' $.ajax({ ' + newline()
- + ' type: "post", ' + newline()
- + ' async : false, ' + newline()
- + ' url: "' + &W_V_SAVE_SESSION_URL + '", ' + newline()
- + ' data: "_EventName=' + &W_V_SAVE_SESSION_EVT + '"+no+"nRC_Grd_enr_info="+pgrd+"_D_V_OPN_SBJ_NM="+subj+"&"+_D_V_GRD_SCRG_NO_X+"="+scrg+"&"+_D_V_GRD_PSN_NM_X+"="+psnm,' + newline()
- + ' success: function(response){ ' + newline()
- //+ ' alert(response); ' + newline()
- + ' } ' + newline()
- + ' }); ' + newline()
- + ' var url = "/SCRM4U/servlet/hwbp04";' + newline()
- + ' window.open(url,'+ newline()
- + ' "hwbp04",'+ newline()
- + ' "height=600,width=850,top=30,left=90,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=no,status=yes");' + newline()
- + '}' + newline()
- + '</script>' + newline()
Grid的初期处理:
- Event grd_enr_info.Load
- W_N_INDEX_NUM = 10001
- for W_N_GRD_IDX = 1 to &W_S_ETR_SHT_RP_INFO.Count
- if (&W_N_GRID_START <= &W_N_GRD_IDX) and (&W_N_GRID_END >= &W_N_GRD_IDX)
- D_V_GRD_NO = &W_N_GRD_IDX//NO
- ......
- // W_V_GRD_PSN_NM_JS = &D_V_GRD_PSN_NM
- // W_V_OPN_SBJ_NM = &D_V_OPN_SBJ_NM
- // //java [!&W_V_GRD_PSN_NM_JS!] = GXutil.URLEncode([!&W_V_GRD_PSN_NM_JS!]);
- // //java [!&W_V_OPN_SBJ_NM!] = GXutil.URLEncode([!&W_V_OPN_SBJ_NM!]);
- // btn_history.JSEvent("onclick", "goto_history('" + &D_V_GRD_SCRG_NO + "','" + &W_V_GRD_PSN_NM_JS + "','" + Trim(&P_V_OPEN_SQ) + "','" + &P_V_OPEN_NO + "','" + &W_V_OPN_SBJ_NM + "')")
- btn_history.JSEvent("onclick", "goto_history('" + str(&W_N_INDEX_NUM) + "')")
- W_N_INDEX_NUM = &W_N_INDEX_NUM + 1
- load
- endif
- endfor
- EndEvent
我们发现,当按钮在Grid时,触发的事件参数形如: E'XXEVENT'.0001的形式,由最后四位数据来确定按钮所处的行数。上面的代码中自动加入了分析行数的功能了。
值得一提的是,Genexus后台为了能正确的判断实行哪些操作,在页面上放置了很多的Hidden.以下简单说明这里用到的参数:
nRC_Grd_enr_info:Grid中每页可显示的最大行数
_EventName:页面触发的事件参数。