简体   繁体   中英

Using same tooltipster-tooltip on dynamic elements for dynamic contents

I really like tooltipster, but I have a question now: I will generate a dynamic list of items and on each item should be a tooltip. The content of this tooltip should be dynamic with an ID, which I send through tooltip to the page inside the tooltip. But for sure, with my current code, I just get the dynamic content in every tooltip from the LAST ID of the list. So, I don't know, how to use tooltipster and send for every list item the correct ID for the dynamic content in the tip.

Here are my codes:

PHP of the list item(s):

<script type="text/javascript">
<?php
$NoteRelativeNoteIDphpVar = $FMID;
echo "var NoteRelativeNoteIDphpVariable = 'FMRelativeID={$NoteRelativeNoteIDphpVar}';";
?>
</script>
<div id="relative<?=$RID?>" class="relativeitem">
<div id="relativeitemrelative"><?=$FMRELATIVE?></div>
<div id="relativeitemname"><?=$FMNAME?></div>
<div id="relativeitemnote"><div id="iconnamenoterelative" class="FMRelativeNote" title="LoadRelativeNote"><img src="img/icon-note.png" class="iconnamenoterelativeimg" /></div></div>
<div id="relativeitemedit"><div id="iconeditrelative" style="background-color:<?=$CATCOL?>;"><a href="forms/Achgrelative.php?aid=<?=$RID?>" title="EDIT RELATIVE OF '<?=strtoupper($ANAME)?>'" class="box"><img src="img/icon-edit.png" class="iconrelativeimg" /></a></div></div>
</div>

Generated Example-HTML of the list item(s):

<script type="text/javascript">
var NoteRelativeNoteIDphpVariable = 'FMRelativeID=23';</script>
<div id="relative1" class="relativeitem">
<div id="relativeitemrelative">Father</div>
<div id="relativeitemname">Louis</div>
<div id="relativeitemnote"><div id="iconnamenoterelative" class="FMRelativeNote" title="LoadRelativeNote"><img src="img/icon-note.png" class="iconnamenoterelativeimg" /></div></div>
<div id="relativeitemedit"><div id="iconeditrelative" style="background-color:#247ac2;"><a href="forms/Achgrelative.php?aid=1" title="EDIT RELATIVE OF 'POLLY'" class="box"><img src="img/icon-edit.png" class="iconrelativeimg" /></a></div></div>
</div>
<script type="text/javascript">
var NoteRelativeNoteIDphpVariable = 'FMRelativeID=21';</script>
<div id="relative2" class="relativeitem">
<div id="relativeitemrelative">Sister</div>
<div id="relativeitemname">Twinny</div>
<div id="relativeitemnote"><div id="iconnamenoterelative" class="FMRelativeNote" title="LoadRelativeNote"><img src="img/icon-note.png" class="iconnamenoterelativeimg" /></div></div>
<div id="relativeitemedit"><div id="iconeditrelative" style="background-color:#247ac2;"><a href="forms/Achgrelative.php?aid=2" title="EDIT RELATIVE OF 'POLLY'" class="box"><img src="img/icon-edit.png" class="iconrelativeimg" /></a></div></div>
</div>

JS of used tooltipster:

$(document).ready(function() {
    $('.FMRelativeNote').tooltipster({
        theme: 'tooltipster-shadow',
        position: 'right',
        contentAsHTML: true,
        onlyOne: true,
        interactive: true,
        functionInit: function(origin, content) {

            if (content === 'LoadRelativeNote') {
                //alert(RelativeNoteID);
                // when the request has finished loading, we will change the tooltip's content
                $.ajax({
                    type: 'POST',
                    url: 'inc/FMRelativeInfo.php',
                    data: NoteRelativeNoteIDphpVariable,
                    success: function(data) {
                        origin.tooltipster('content', data);
                    }
                });

                // this returned string will overwrite the content of the tooltip for the time being
                return 'Wait while the content is loading...';
            }
            else {
                // return nothing : the initialization continues normally with its content unchanged.
            }
        }
    });
});

So, I just need help please, to find the way to send always the correct ID from the PHP-list item to the tooltipster and not the LAST ID.

PS: You can see in the screenshots-image, that the content is always the LAST content of the list, not the different one!

在此处输入图片说明

with a little help by the support of tooltipster, I found the solution on my own:

PHP:

<div id="iconnamenoterelative" class="FMRelativeNote" title="<?=$FMID?>">

HTML:

<div id="relative1" class="relativeitem">
<div id="relativeitemrelative">Father</div>
<div id="relativeitemname">Louis</div>
<div id="relativeitemnote"><div id="iconnamenoterelative" class="FMRelativeNote" title="23"><img src="img/icon-note.png" class="iconnamenoterelativeimg" /></div></div>
<div id="relativeitemedit"><div id="iconeditrelative" style="background-color:#247ac2;"><a href="forms/Achgrelative.php?aid=1" title="EDIT RELATIVE OF 'POLLY'" class="box"><img src="img/icon-edit.png" class="iconrelativeimg" /></a></div></div>
</div>
<div id="relative2" class="relativeitem">
<div id="relativeitemrelative">Sister</div>
<div id="relativeitemname">Twinny</div>
<div id="relativeitemnote"><div id="iconnamenoterelative" class="FMRelativeNote" title="21"><img src="img/icon-note.png" class="iconnamenoterelativeimg" /></div></div>
<div id="relativeitemedit"><div id="iconeditrelative" style="background-color:#247ac2;"><a href="forms/Achgrelative.php?aid=2" title="EDIT RELATIVE OF 'POLLY'" class="box"><img src="img/icon-edit.png" class="iconrelativeimg" /></a></div></div>
</div>

JS:

$(document).ready(function() {
    $('.FMRelativeNote').tooltipster({
        theme: 'tooltipster-shadow',
        position: 'right',
        contentAsHTML: true,
        onlyOne: true,
        interactive: true,
        functionInit: function(origin, content) {

            if (content != '') {
                //alert(RelativeNoteID);
                // when the request has finished loading, we will change the tooltip's content
                $.ajax({
                    type: 'POST',
                    url: 'inc/FMRelativeInfo.php',
                    data: "FMRelativeID=" + content,
                    success: function(data) {
                        origin.tooltipster('content', data);
                    }
                });

                // this returned string will overwrite the content of the  tooltip for the time being
                return 'Wait while the content is loading...';
            }
            else {
                // return nothing : the initialization continues normally with its content unchanged.
            }
        }
    });
});

THANK YOU and maybe it helps somebody too! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM