1

I am using bootstrap 4, and I have two list-groups in containers side by side. I am trying to show items in container 2 upon selection of a list item from container 1. How do I populate this?

Each row in my right container, determine how many items my second container should populate. We can use values for now.

<div class="col-6">
  <div class="card ">
      <div class="card-header py-2">Group Name</div>
      <div class="list-group">
          <a href="#" class="list-group-item py-0 list-group-item-action">Container Left 7 items</a>
          <a href="#" class="list-group-item py-0 list-group-item-action">Cras justo odio 10 items</a>
          <a href="#" class="list-group-item py-0 list-group-item-action">Dapibus ac facilisis 17 items</a>
          <a href="#" class="list-group-item py-0 list-group-item-action">Morbi leo risus 12 items</a>                            
      </div>
  </div>
</div>
<div class="col-6">
  <div class="card">
      <div class="card-header py-2">Items</div>
      <div class="list-group">
          <a href="#" class="list-group-item py-0 list-group-item-action">Container Right</a>
          <a href="#" class="list-group-item py-0 list-group-item-action">Cras justo odio</a>
          <a href="#" class="list-group-item py-0 list-group-item-action">Dapibus ac facilisis in</a>
          <a href="#" class="list-group-item py-0 list-group-item-action">Morbi leo risus</a>                            
      </div>
  </div><!--class="card"-->
</div><!--class="col-6"-->

I have this plnkr example

Can you help? Also, I will need a vertical scroll bar after 9 items or long. And want selection to remain highlighted in blue. Container 2 is to update when items from container 1 is selected, otherwise remain empty.

  • How familiar are you with jquery or javascript? – Iskandar Reza Nov 21 '18 at 06:52
  • Just noticed your comment up there, sorry was away during holidays. I have used the method suggested below and I have given some updates. To answer your question, I am not very familiar, I usually google for what I need and try to use them. – YouGotA.BigEgo Nov 27 '18 at 19:40

1 Answers1

1

The following answer is the simplest solution just to give you the idea, of course there are other better solutions.

In this answer, I used simple Jquery code and some tiny html adjustments to the code you posted in Plunker, you can adjust it to what ever syntax you need.

Regarding the container selection, you can add a data-set attribute in each anchor you are using in both containers. The value of data-set in the left container represents the number of items the right side must contain. The value of data-set in the right container represents a number of items.

Here is the html

<div class="col-6">
  <div class="card ">
    <div class="card-header py-2">Container Left</div>
    <div id="leftContainer" class="list-group">
      <a href="#" data-set="2" class="list-group-item py-0 list-group-item-action">Container Left 7 items</a>
      <a href="#" data-set="4" class="list-group-item py-0 list-group-item-action">Cras justo odio 10 items</a>
      <a href="#" data-set="3" class="list-group-item py-0 list-group-item-action">Dapibus ac facilisis 17 items</a>
      <a href="#" data-set="5" class="list-group-item py-0 list-group-item-action">Morbi leo risus 12 items</a>                            
    </div>
  </div>
</div>
<div class="col-6">
  <div class="card">
    <div class="card-header py-2">Container Right</div>
    <div id="rightContainer" class="list-group" style="height:225px; overflow-y: scroll">
      <a href="#" data-set="1" class="list-group-item py-0 list-group-item-action">1</a>
      <a href="#" data-set="2" class="list-group-item py-0 list-group-item-action">2</a>
      <a href="#" data-set="3" class="list-group-item py-0 list-group-item-action">3</a>
      <a href="#" data-set="4" class="list-group-item py-0 list-group-item-action">4</a>                            
      <a href="#" data-set="5" class="list-group-item py-0 list-group-item-action">5</a>
      <a href="#" data-set="6" class="list-group-item py-0 list-group-item-action">6</a>
      <a href="#" data-set="7" class="list-group-item py-0 list-group-item-action">7</a>
      <a href="#" data-set="8" class="list-group-item py-0 list-group-item-action">8</a>
      <a href="#" data-set="9" class="list-group-item py-0 list-group-item-action">9</a>
      <a href="#" data-set="10" class="list-group-item py-0 list-group-item-action">10</a>
      <a href="#" data-set="11" class="list-group-item py-0 list-group-item-action">11</a>
      <a href="#" data-set="12" class="list-group-item py-0 list-group-item-action">12</a>
      <a href="#" data-set="13" class="list-group-item py-0 list-group-item-action">13</a>
      <a href="#" data-set="14" class="list-group-item py-0 list-group-item-action">14</a>
    </div>
  </div><!--class="card"-->
</div><!--class="col-6"-->

Note the rightContainer div styling, this will solve the vertical scroll (second part of your question), but of course you will need to adjust it to height of each item after finishing it styling.

Here is the Jquery event function that will do the selection trick

$("#leftContainer > a").click(function(event){
  event.preventDefault();
  $("#leftContainer > a").removeClass("active");
  $(this).addClass("active");
  var leftDataSet = parseInt($(this).attr("data-set"));
  $("#rightContainer > a").removeClass("active");
  $("#rightContainer > a").hide();
  $("#rightContainer > a").each(function(){
    if(leftDataSet >= parseInt($(this).attr("data-set"))){
      $(this).show();
    }
  });
});

Note that I used the active bootstrap class for the selection highlight. And for the third part of your question the highlight part you can use this

$("#rightContainer > a").click(function(event){
  event.preventDefault();
  $(this).toggleClass("active");
});

For the last part of your question you can either add a d-none class to all anchors in the html at the beginning or you can add the following line at the beginning of your javascript file before the events above

$("#rightContainer > a").hide();

Hope it was helpful

Hossam
  • 123
  • 6