请求禁用Coveo移动端卡片视图,改用列表视图(AEM 6.3环境)
Got it, let's walk through how to properly lock your Coveo search results to list view (and disable card view entirely, especially for mobile) in your AEM 6.3 template. You're already on the right track by finding that code snippet in CoveoJsSearch.js—let's break down what's going on and fix it properly.
First, let's look at the original code you found around line 17728:
if (merged.responsiveComponents.isSmallScreenWidth()) {
templates = _.filter(templates, function (tmpl) { return tmpl.layout == 'card'; });
merged.currentLayout = 'list';
this.layout = 'list';
}
That code has a clear contradiction: it filters the available templates to only keep the card layout, but then sets the active layout to list. No wonder you're seeing wonky behavior! To fix this and disable card view for small screens (mobile), adjust the code to filter templates to only keep the list layout instead:
if (merged.responsiveComponents.isSmallScreenWidth()) { // Filter out card templates entirely, keep only list layout templates = _.filter(templates, function (tmpl) { return tmpl.layout == 'list'; }); merged.currentLayout = 'list'; this.layout = 'list'; }
If you want to disable card view for all screen sizes (not just mobile), you can move this template filtering logic outside the small screen check entirely. But since you specifically mentioned mobile is better suited for list view, the above tweak should target that perfectly.
AEM 6.3 Deployment Steps
Once you've updated the code, don't forget these critical steps to make sure the change takes effect:
- Package the modified
CoveoJsSearch.jsinto your project's client library (usually under/apps/your-project/clientlibs/coveo/or a similar path) so AEM serves the updated file. - Clear your AEM dispatcher cache and local browser cache—old JS files hanging around will ruin your testing.
- Test on multiple mobile devices and screen sizes to confirm card view no longer loads, and list view is the default for small screens.
A Safer Alternative (No Core File Modifications)
Quick note: Modifying the core CoveoJsSearch.js directly can be risky because future Coveo updates might overwrite your changes. A better approach is to add custom JS that loads after Coveo's core files, overriding the layout logic without touching core code. Here's how:
Coveo.ready(function() { const searchInterface = Coveo.get('#search'); if (searchInterface) { searchInterface.on('afterInitialization', function() { // Force list view on small screens if (Coveo.ResponsiveComponents.isSmallScreenWidth()) { searchInterface.changeLayout('list'); // Remove the card layout option from the UI selector (if it exists) const cardLayoutButton = document.querySelector('.coveo-layout-option[layout="card"]'); if (cardLayoutButton) cardLayoutButton.remove(); } }); } });
Add this to a custom clientlib in your AEM project, set it to load after the Coveo clientlibs, and you'll get the same result without modifying core files—way easier to maintain long-term.
内容的提问来源于stack exchange,提问作者Mark Handy

