如何在Drupal 8中更新我的视图而无需回到顶部? [英] How to update my View in Drupal 8 without scrolling back to the top?
问题描述
我试图在Drupal 8中刷新我的视图,而不用以下代码重新加载页面:
I'm trying to refresh my View in Drupal 8 without reloading the page with this code :
(function ($, Drupal) {
'use strict';
setInterval(function() {
$('.view-message-activity-stream-timeline-public').trigger('RefreshView');
}, 10000);
})(jQuery, Drupal);
有效。
我的问题:
每次刷新视图时,该页面就会回到视图顶部。
Each time the view is refreshed, that page scolls back to the top of the view.
页面刷新还有另一个问题。当我单击主页底部的 Afficher plus链接时,第2页显示在第1页的下面,但是刷新页面后它将自动关闭。
There is another problem with the refresh of the page. When I click the "Afficher plus" link at the bottom of the home page, page 2 appears below page 1, but it automatically closes when the page is refreshed.
< a href = https://www.s1biose.com/ rel = nofollow noreferrer> https://www.s1biose.com/
推荐答案
您的问题是由于 ViewAjaxController
将 ScrollTopCommand
加到每次加载/重新加载视图时,ajax响应。您可以在方法 ajaxView
中找到所涉及的行:
Your issue is caused by the ViewAjaxController
adding up the ScrollTopCommand
to the ajax response every time it loads/reloads a view. You can find the involved lines in the method ajaxView
:
if (isset($pager_element)) {
$response->addCommand(new ScrollTopCommand(".js-view-dom-id-$dom_id"));
$view->displayHandlers->get($display_id)->setOption('pager_element', $pager_element);
}
希望可以通过以下方式更改发送给客户端的命令数据:执行 hook_ajax_render_alter
,只需删除 viewsScrollTop
命令,使其不再被触发:
Hopefully it is possible to alter the command data that is sent to the client by implementing the hook_ajax_render_alter
, just remove the viewsScrollTop
command so that it is no longer triggered:
function <MODULE>_ajax_render_alter(array &$data) {
$view_name = '<view_name>';
$view_dom_id = '<view_dom_id>';
$selector = '.js-view-dom-id-' . $view_dom_id;
foreach ($data as $key => $value) {
if ($value['command'] === 'viewsScrollTop' && $value['selector'] === $selector) {
unset ($data[$key]);
break;
}
}
}
如果需要匹配 view_dom_id
与 view_name
,您可以搜索设置
$ data数组中的命令,例如。 $ cmd ['settings'] ['views'] ['ajaxViews']
具有如下结构,请注意,数组键正是根据views_dom_id构建的:
If you need to match the view_dom_id
with the view_name
, you can search for the settings
command in the $data array, eg. $cmd['settings']['views']['ajaxViews']
has a structure that looks like the following, note the array key precisely being built up from the views_dom_id :
[ajaxViews] => Array
(
[views_dom_id:<views_dom_id>] => Array
(
[view_name] => <view_name>
[view_display_id] => <view_display_id>
[view_dom_id] => <views_dom_id>
)
)
这篇关于如何在Drupal 8中更新我的视图而无需回到顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!