Can't figure out to make unique ID's for block
Permalink
I am hoping someone here can help me with a programming issue that I just can;t seem to get my head around.
I have a page list block on a page, and I am using the 4column carousel template. The page has the following elements (with lots of code in-between each line)
I put one block on the page and it is fine. The left and the right buttons work as expected, using the javascript, which is controled by the loop
Now, when I add a 2nd block it does not work properly because the javascript is referring to the same ID of #fourcolumn-carousel, so the 2nd block's js acts on the first block only.
So, that was the problem.
My soluton was to give unique names to the classes and ids...
and adjust the js accordingly, for example
This worked, but with three major flaws 1) instead of the row of thumbs all moving to the left by one, the all moved to the left by 5, I assume because the jQuery wildcard is finding each instance in the fours divs, resutling in the same end result, but visually horrible. 2) if there are more than 4 images, when the images are moving to the left, the height of the carousel div doubles to accomodate the 5th, 6th, etc, images. When it stops, the height goes back to normal. In other words, the layout gets totally destroyed.
So, the help I need is simply, how can I make each carousel have it's own ID's that won't crash into each other?
I have tried wrapping the entire carousel in a uniquely named div and accessing each element from that div with full paths to the target, but that did not work.
I have made blocks with unique ID's before, but clearly I am missing something with this block.
Here is the complete code in it's original form (from the "Framework" template)
and view.js
I have a page list block on a page, and I am using the 4column carousel template. The page has the following elements (with lots of code in-between each line)
<?php <div class="pagelistwrapper"> <div class="pagelistcont"> <div class="row"> <div class="carousel slide" id="fourcolumn-carousel"> <div class="carousel-inner"> <a class="left carousel-control" href="#fourcolumn-carousel" data-slide="prev"> <a class="right carousel-control" href="#fourcolumn-carousel" data-slide="next"> </div> </div> </div> </div> </div> ?>
I put one block on the page and it is fine. The left and the right buttons work as expected, using the javascript, which is controled by the loop
for (var i=1;i<4;i++) { $('#fourcolumn-carousel .item').each(function(){ .... }
Now, when I add a 2nd block it does not work properly because the javascript is referring to the same ID of #fourcolumn-carousel, so the 2nd block's js acts on the first block only.
So, that was the problem.
My soluton was to give unique names to the classes and ids...
<?php <div class="pagelistwrapper_<?=$uid?>"> <div class="pagelistcont_<?=$uid?>"> <div class="row_<?=$uid?>"> <div class="carousel slide_<?=$uid?>" id="fourcolumn-carousel_<?=$uid?>"> <div class="carousel-inner_<?=$uid?>"> <a class="left carousel-control_<?=$uid?>" href="#fourcolumn-carousel_<?=$uid?>" data-slide="prev"> <a class="right carousel-control_<?=$uid?>" href="#fourcolumn-carousel_<?=$uid?>" data-slide="next"> </div> </div> </div> </div> </div> ?>
and adjust the js accordingly, for example
for (var i=1;i<4;i++) { $('[id^=fourcolumn-carousel] .item').each(function(){ .... }
This worked, but with three major flaws 1) instead of the row of thumbs all moving to the left by one, the all moved to the left by 5, I assume because the jQuery wildcard is finding each instance in the fours divs, resutling in the same end result, but visually horrible. 2) if there are more than 4 images, when the images are moving to the left, the height of the carousel div doubles to accomodate the 5th, 6th, etc, images. When it stops, the height goes back to normal. In other words, the layout gets totally destroyed.
So, the help I need is simply, how can I make each carousel have it's own ID's that won't crash into each other?
I have tried wrapping the entire carousel in a uniquely named div and accessing each element from that div with full paths to the target, but that did not work.
I have made blocks with unique ID's before, but clearly I am missing something with this block.
Here is the complete code in it's original form (from the "Framework" template)
<?php defined('C5_EXECUTE') or die("Access Denied."); $th = Loader::helper('text'); $c = Page::getCurrentPage(); $dh = Core::make('helper/date'); /* @var $dh \Concrete\Core\Localization\Service\Date */ $i = 0; ?> <?php if ( $c->isEditMode() && $controller->isBlockEmpty()) { ?> <div class="ccm-edit-mode-disabled-item"><?php echo t('Empty Page List Block.')?></div> <?php } else { ?> <div class="pagelistwrapper"> <?php if ($rssUrl): ?> <a class="pull-right" href="<?php echo $rssUrl ?>" target="_blank" class="ccm-block-page-list-rss-feed"><i class="fa fa-rss"></i></a> <?php endif; ?> <?php if ($pageListTitle): ?>
Viewing 15 lines of 97 lines. View entire code block.
and view.js
if($(window).width() < 767){ $('#fourcolumn-carousel').carousel({ interval: 0, }); }else{ $('#fourcolumn-carousel').carousel({ interval: 3000, }); } var maxheight = 0; (function(){ if ($(window).width() > 767){ $('#fourcolumn-carousel .item').each(function(){ var itemToClone = $(this); for (var i=1;i<4;i++) {
Viewing 15 lines of 44 lines. View entire code block.
I think the problem I am trying to fix is the same, in concept, as the one you were fixing by using the $bID, it's just that for this block it seems the solution is not as clear. The reason I used $uid instead of $bID is to ensure that I always have a unique ID in every instance, but I think this is unnecessary overkill in my case as each block will be edited, thereby creating a new $bID
Hi ntisithoj,
I will take a look at this this weekend.
Please do not create multiple forum posts for the same issue. They make the forum less organized and may get marked as spam.
I will take a look at this this weekend.
Please do not create multiple forum posts for the same issue. They make the forum less organized and may get marked as spam.
Thanks, and apologies for the sloppy posting.
@ntisithoj
It looks like you removed the Page List carousel on your site.
If you decide to use the carousel again, I would try taking the CSS and JavaScript from the template view.css and view.js and move it to the view.php file. This will allow you to replace each occurrence of "#fourcolumn-carousel" with "<?php echo '#fourcolumn-carousel-' . $bID; ?>".
It looks like you removed the Page List carousel on your site.
If you decide to use the carousel again, I would try taking the CSS and JavaScript from the template view.css and view.js and move it to the view.php file. This will allow you to replace each occurrence of "#fourcolumn-carousel" with "<?php echo '#fourcolumn-carousel-' . $bID; ?>".
Ah.. yes I did remove it :/
Your suggestion of putting the script on the PHP was quite effective and so simple I am embarrassed I did not think of it. But I still am not understanding why the selector wild cards did not work... but, as some of this code is within bootstrap I assume that simply changing a class/id name could have consequences, which would explain why it is still acting odd even though your solution fixed the main problem.
Thanks
Your suggestion of putting the script on the PHP was quite effective and so simple I am embarrassed I did not think of it. But I still am not understanding why the selector wild cards did not work... but, as some of this code is within bootstrap I assume that simply changing a class/id name could have consequences, which would explain why it is still acting odd even though your solution fixed the main problem.
Thanks
However, I did face a similar problem once and ended up creating unique ids by including the block's $bID in the html ids; eg:
If that's on the right track, you may later run into a problem if you copy-and-paste blocks because the bID of the copy will be the same as the original (unless it has been edited). There's a way around this, but I'll spare you the details unless relevant.