1

I am stuck in using tabs widget in Magento2. I have not found any resource that teaches about this. The only resource is this: DevDocs But this is useless. It is only a reference of options and methods, but not how to use it.

I have this in attributes.phtml of my custom theme:

<div id="Tabs1">
 <ul>
 <?php $count = 0 ?>
 <?php foreach ($_additional as $_data): ?>
 <li data-role="title"><a href="#tabs-<?php echo ++$count ?>"><?php echo $block->escapeHtml(__($_data['label'])) ?></a></li>
 <?php endforeach; ?>
 </ul>
 <?php $count = 0 ?>
 <?php foreach ($_additional as $_data): ?>
 <div id="tabs-<?php echo ++$count ?>" data-role="content">
 <p><?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></p>
 </div>
 <?php endforeach; ?>
</div>
<script type="text/javascript">
require([
 'jquery',
 'jquery/ui'
], function($){
 $(function () {
 $("#Tabs1").tabs();
 }); 
});
</script>

But that does not work. Can you show me where is the error, please?

Thanks Jaime

Prince Patel
23.1k10 gold badges102 silver badges124 bronze badges
asked Feb 23, 2016 at 16:21

3 Answers 3

5

You have error on the page because you miss your tabs widget in dependencies list (at list if you want to use Magento's tabs widget).

Correct:

require([
 'jquery',
 'mage/tabs'
], function($){

But I can suggest more convenient way:

<div data-mage-init='{"mage/tabs":{}}'>
 <ul>
 <?php foreach ($_additional as $_key => $_data): ?>
 <li data-role="title"><a href="#tabs-<?php echo $_key ?>">...
 <?php endforeach; ?>
 </ul>
 <?php foreach ($_additional as $_key => $_data): ?>
 <div id="tabs-<?php echo $_key ?>" data-role="content">
 ...
 </div>
 <?php endforeach; ?>
</div>

mage/tabs.js will be loaded by Magento's App and initialized on needed element.

answered Feb 23, 2016 at 19:50
3
  • Thanks but it did not work. It does simply nothing. By the way, jquery/ui includes tabs widget as I have checked in jquery-ui.js. But i tested both ways. After page is loaded, the data-mage-init attribute is removed which indicates something is happening, but that is the only behaviour. I tried also what appears in this page: github.com/magento/magento2/blob/c58d2d/app/code/Magento/…. But it did not work either. Not even errors are shown in console output. Commented Feb 23, 2016 at 20:18
  • 1
    The problem was that I have not included default css styles. I included them and it looks pretty now :-) Commented Feb 24, 2016 at 20:49
  • How do you include those styles? Commented Aug 8, 2017 at 10:27
2

Works for me if I remove the second $(function () {}) wrap like so:

<script type="text/javascript">
 require([
 'jquery',
 'jquery/ui'
 ], function($){
 $("#Tabs1").tabs();
 });
</script>
7ochem
7,61516 gold badges54 silver badges82 bronze badges
answered Dec 6, 2016 at 10:45
0

Follow this url to solve the widget tab.

https://devdocs.magento.com/guides/v2.4/javascript-dev-guide/widgets/widget_tabs.html

for active class css

.nav-tabs li[aria-expanded="true"]{background:#f26c4f !important;} .nav-tabs li[aria-expanded="true"] a{background:#f26c4f !important;color:#fff !important;}
answered Apr 7, 2021 at 4:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.