我们有跨多个页面的共同功能: Cancel/Save/Back/Next (按钮)。
我通过应用程序级静态JavaScript文件添加了快捷键:Alt/S/B/N(在按钮和jQuery绑定上使用公共CSS )。
问题:在编辑单元格时使用快捷键时,对单元格的编辑没有使用Alt、Alt保存。(出于同样的原因,取消/返回时没有请求确认,因为网格没有看到任何更改。)
大量的文献检索和阅读并没有揭示出答案.
经过的巨大努力:通过控制台和检查窗格检查小部件/ interactiveGrid/ model/ getCurrentView (部分原因是我很长时间没有找到/检查.interactiveGrid('getViews','grid).getColumns() ),我找到了一个解决方案,但我觉得必须是一个更优雅的解决方案。建议!TIA。
发布于 2022-05-03 09:46:06
这是我的解决办法。注意,名称空间是lps,在下面的代码之前,我已经将apex.jQuery别名为lps.$。我包括一些对JS、Apex、jQuery等缺乏经验的甲骨文同事的评论。
该解决方案适用于所有4个快捷键,并且应该适用于任何IG (即使在包含区域上可能没有静态id,尽管我的测试用例有一个静态id)。在主解决方案之后有一些支持代码。所有这些都发生在通过静态JS文件加载页面上。
jQuery选择器只能绑定到每个页面上的一个按钮!
lps.$(window).on('keydown',function($evt) // Capture Alt + shortcut key in keydown event
{
// $evt is the jQuery simplified event
if( $evt.altKey ) // the alt-key is being pressed-and-held
{
var key = $evt.key.toUpperCase();
if( lps.shortcuts[key] )
{
if( lps.$($evt.target).is('input,textarea') ) // effect the onchange() JavaScript (more specifically, the IG version thereof)
{
// We've pressed a shortcut key combo, whilst editing a cell, so the IG-onchange-y event hasn't yet fired
var tgtEle = $evt.target,
newVal = tgtEle.value, // the value of the on-screen element, regardless of change-status
tgtId = tgtEle.id, // this tallies with an elementId exposed via the igCols below
activeFld;
// IG stuff - igId - get the IG overall id via an IG ancestor div of the target field, then losing the trailing _ig suffix
var igId = lps.$(tgtEle).parents('div.a-IG')[0].id.replace(/_ig$/,'')
igWidget= apex.region(igId).widget(),
igGrid = igWidget.interactiveGrid('getViews','grid'),
igModel = igGrid.model,
igView = igWidget.interactiveGrid('getCurrentView'),
igRecId = igView.getActiveRecordId(),
igCols = igGrid.getColumns(); // this provides meta information about the columns in the IG
for( var i=-1; ++i<igCols.length; ) // find the field name of this IG-column from target element id
{
if( igCols[i].elementId === tgtId )
{
activeFld = igCols[i].property;
break;
}
}
// Phew. Eventually setValue on the IG-cell so Apex knows the cell has changed without an onchange firing
igModel.setValue(igModel.getRecord(igRecId),activeFld,newVal);
}
$evt.originalEvent.preventDefault(); // we're consuming the event, so stop it doing some native browser trick we don't want
lps.$(lps.shortcuts[key].selector).select(); // select the to-be-clicked-element
lps.$(lps.shortcuts[key].selector).click(); // synthesise a click on the selector'd element
}
}
});支持代码预先建立我们的4个快捷键是什么:
lps.shortcuts = { // keys and their jQuery selectors
B : { selector:'button.nav-back' },
C : { selector:'button.nav-cancel' },
N : { selector:'button.nav-next' },
S : { selector:'button.nav-save' },
};要向按钮添加悬停标题的代码:
// Advertise the shortcut with a 'title' attribute (appears as popup tip when hover over the element)
for( var key in lps.shortcuts ){
lps.$(lps.shortcuts[key].selector).attr('title','Alt+'+key);
}https://stackoverflow.com/questions/72097045
复制相似问题