0

Hint: Formating as raw won't fix my problem - See attachement respectively picture: enter image description here

The following code will implement tooltip using attribute title of method tag().

Unfortunately, there is no possibility rendering HTML-tags inside the title. Any ideas(links are welcome) how to implement tooltip handling my intention to render HTML tags? Is there any other option implementing tooltip-box outside attribute title, so that I am able to render HTML-tags

    [
        'attribute' => $dummy,
        'label' => Yii::t('app', 'Charakterisierung'),
        'format' => 'raw',
        'vAlign' => 'middle',
        'value' => function($model) {
            if (!(empty($model->person->personentypDominant->typ_name))) {
                $tag = Html::tag('span', 'Tooltip-Touch Me!', [
                            // html-tags won't be rendered in title
                            'title' => $model->person->personentypDominant->typ_empfehlung],
                            'data-toggle' => 'tooltip',
                            'data-placement' => 'left',
                            'style' => 'white-space:pre;border:1px solid red;'
                ]);
                return $tag . "<br>" . $model->person->personentypDominant->typ_verhaltensmerkmal_im_team_1 . "," . $model->person->personentypDominant->typ_verhaltensmerkmal_bei_stress_3 . "," . $model->person->personentypDominant->typ_verhaltensmerkmal_am_arbeitsplatz_4;
            }
        }
    ],
tklustig
  • 483
  • 6
  • 24

2 Answers2

6

The Html::tag() takes 3 parameters like below

<?= Html::tag($name, $content = '', $options = []) ?>

Then if you look into the documentation for the bootstrap Tooltip options there is an option named HTML

Insert HTML into the tooltip. If false, jQuery's text method will be used to insert content into the DOM. Use text if you're worried about XSS attacks.

Which has the default value as false and you have to set it manually to true.

So apparently, you have 3 problems

  • You are closing the options ] on the line 'title' => $model->person->personentypDominant->typ_empfehlung ],

  • There isn't any attribute with the name vAlign for yii\grid\DataColumn untill unless you are using kartik\grid\DataColumn then it's ok.

  • You are not initializing tooltip with the option HTML true.

First of all, add the following to the top of your view or inside the layout file if want to apply these setting allover

$js = <<<SCRIPT
/* To initialize BS3 tooltips set this below */
$(function () { 
   $('body').tooltip({
    selector: '[data-toggle="tooltip"]',
        html:true
    });
});
SCRIPT;
// Register tooltip/popover initialization javascript
$this->registerJs ( $js );

Change your code for GridView to the following, i assume that $model->person->personentypDominant->typ_empfehlung , has the HTML that you are trying to render

[
    'attribute' => $dummy ,
    'label' => Yii::t ( 'app' , 'Charakterisierung' ) ,
    'format' => 'raw' ,
    'value' => function($model) {
        if ( !(empty ( $model->person->personentypDominant->typ_name )) ) {
            $tag = Html::tag ( 'span' , 'Tooltip-Touch Me!' , [
                   // html-tags won't be rendered in title
                   'title' => $model->person->personentypDominant->typ_empfehlung ,
                   'data-placement' => 'left' ,
                   'data-toggle'=>'tooltip'
                   'style' => 'white-space:pre;border:1px solid red;'
            ] );
            return $tag . "<br>" . $model->person->personentypDominant->typ_verhaltensmerkmal_im_team_1 . "," . $model->person->personentypDominant->typ_verhaltensmerkmal_bei_stress_3 . "," . $model->person->personentypDominant->typ_verhaltensmerkmal_am_arbeitsplatz_4;
        }
    }
];

EDIT

You need to use the column format as raw when using the gridview otherwise the tooltip won't render.

"format"=>"raw"

Edit 2

Make Sure you are not Using AdminLTE theme with jquery UI as they have a conflict SEE ISSUE.

The causes of conflict on the jquery UI tooltip and the bootstrap tooltip are jQuery UI tooltip overwrite the Bootstrap tooltip maybe they use the same namespace and function name.

Add the following code in your javascript (solution from here)

var bootstrapTooltip = $.fn.tooltip.noConflict();
$.fn.bstooltip = bootstrapTooltip;
$('element').bstooltip();
var bootstrapTooltip = $.fn.tooltip.noConflict();
$.fn.bstooltip = bootstrapTooltip;
$('element').bstooltip();

Demo

$(document).ready(function() {
  var bootstrapTooltip = $.fn.tooltip.noConflict();
  $.fn.bstooltip = bootstrapTooltip;
  $('#mybtn').bstooltip();
})
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div style="margin:100px;">
  <button id="mybtn" title="my tooltip showing now">hover me</button>
</div>

The point that needs attention is that we must put the jqueryui.js before bootstrap.js, so you have to rearrange your .js files in the $js array inside the AppAsset file you are using to load all the scripts.

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
  • 1
    Thx a lot for this. Works pretty fine! – tklustig Mar 04 '18 at 09:13
  • :) happy to help @tklustig – Muhammad Omer Aslam Mar 04 '18 at 09:14
  • There is any attribute with the name vAlign for kartik\grid\DataColumn. I'm using GridView of kartik! – tklustig Mar 05 '18 at 14:09
  • ahh , wasnt mentioned so i thought your are using default gridview , thats y mentioned `yii\grid\DataColumn` will update in the answer too – Muhammad Omer Aslam Mar 05 '18 at 14:15
  • Formating as raw won't fix my problem(see Hint above) – tklustig May 17 '18 at 07:54
  • tooltip will be rendered, independent of using raw or html. html-tags won't be rendered in both cases – tklustig May 17 '18 at 08:12
  • nopes that is incorrect, that using none of the above will show the tooltip, until unless you use `raw` you wont be able to render the tooltip, inside the gridview @tklustig , – Muhammad Omer Aslam May 17 '18 at 10:46
  • So I assume, that some CSS-rule of AdminLTE prevents rendering html-tags! I have no idea, of course,which one this colud be. Will U be able to render html-tags using AdminLTE,too?? – tklustig May 17 '18 at 14:44
  • Well, `AdminLTE` already uses `bootstrap 3.x` i do not think that it would be the reason as I have used it in the previous project and never faced this problem, i am also a bit confused why would it not work at your end @tklustig can you try a hardcoded `html` string to render inside the `title` tag with `
    ` and `
      ` tags although the `ul` tags are already rendered but for the sake of troubleshooting just try rendring the hardcoded html does it work.
    – Muhammad Omer Aslam May 17 '18 at 14:49
  • No usnig code like this: 'title' => '
    • Hallo
    ', won't render html-tags P.S.: Changing layoutfile will render html-tags, but I need AdminLTE!
    – tklustig May 17 '18 at 15:24
  • kindly mark this answer as correct again @tklustig hope you have no concerns over it – Muhammad Omer Aslam May 17 '18 at 15:56
  • Solved! After having removed app.min.js and copied adminlte.js into js folder and integrated this js-file in my asset-class, html-tags finally will be rendered! Thx for ur efforts helping me! Gave u reputation twice, in both threads! Greetings to Agypt! – tklustig May 17 '18 at 15:59
  • i appreciate that :) @tklustig Greetings – Muhammad Omer Aslam May 18 '18 at 09:00
0

Please note that now bootstrap 3.4.1 and 4.3.1 has a sanitizer that block most of HTML tags by default.

If your tooltip (or popover) data-content contain one or more HTML tags not listed in the default whitelist, you need to add them manually:

var myDefaultWhiteList = $.fn.tooltip.Constructor.DEFAULTS.whiteList

// To allow table elements
myDefaultWhiteList.table = []

// To allow td elements and data-option attributes on td elements
myDefaultWhiteList.td = ['data-option']

// You can push your custom regex to validate your attributes.
// Be careful about your regular expressions being too lax
var myCustomRegex = /^data-my-app-[\w-]+/
myDefaultWhiteList['*'].push(myCustomRegex)

// initialize your tooltip
$(function () {
    $('[data-toggle="tooltip"]').tooltip({
        // set your custom whitelist
        whiteList: myDefaultWhiteList
    });
});