Home > Mobile >  How to arrange Bootstrap alerts into two columns?
How to arrange Bootstrap alerts into two columns?

Time:11-20

I have a very simple to-do like page where I can enter members and remove them or mark them as done. The members are Bootstrap alerts, so far so good.

My problem is, that the alert/members are too wide and should be in two or three columns, to be able to view more of them on the screen.

This is where I get stuck, because the grid system doesn't want it. I don't know what am I missing...

Can someone please help me, how to rearrange the member list into two or three columns?

I tried with the columns with the team1 button.

This is the JSFiddle URL for it: https://jsfiddle.net/voriand/zsfo65wb/

$(document).ready(function() {
  $('.btn-addmember').click(function() {
    if ($('#text').val().length != 0) {
      var x = $('#memberlist').html();
      var y =
        `<div >
        <a href="#"  data-dismiss="alert" aria-label="close">×</a>
                            `   $('#text').val()   `</div>`;
      $('#memberlist').html(y   x);
      $('#text').val("");
    } else alert("Please, enter the name of the member");
  });
  
  //Adding members of team1
  $('.btn-team1').click(function() {
    if ($('#team1').val().length != 0) {
      var members = $('#team1').val().split('\n');

      for (var i = 0; i < members.length; i  ) {
        var x = $('#memberlist').html();
        var y =
          `<div ><div >
                                        <a href="#"  data-dismiss="alert" aria-label="close">×</a>
                                        <b>`   members[i]   `</b></div></div>`;
        if (members[i].trim().length > 0) {
          $('#memberlist').html(y   x);
        }
      }
    }
  });

  //Adding members of team2
  $('.btn-team2').click(function() {
    if ($('#team2').val().length != 0) {
      var members = $('#team2').val().split('\n');

      for (var i = 0; i < members.length; i  ) {
        var x = $('.container').html();
        var y =
          `<div >
                                        <a href="#"  data-dismiss="alert" aria-label="close">×</a>
                                        <b>`   members[i]   `</b></div>`;
        if (members[i].trim().length > 0) {
          $('.container').html(y   x);
        }
      }
    }
  });
  // When Member is clicked
  $(document).on('click', '.alert', function() {
    if ($(this).css('text-decoration-line') == "none") {
      $(this).css('text-decoration-line', 'line-through');
      $(this).css('background-color', '#dddddd');
      $(this).css('border-color', '#dddddd');
    } else {
      $(this).css('text-decoration-line', 'none');
      $(this).css('background-color', '#dff0d8');
      $(this).css('border-color', '#d6e9c6');
    }
  });
});
.container {
  width: 80%;
  height: auto;
}

.foot {
  position: fixed;
  left: 10%;
  bottom: 0;
  width: 80%;
  text-align: center;
}

.form-group {
  height: 0px;
  visibility: hidden;
}

.copyright {
  width: 100%;
  text-align: right;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<center>

  <div class="foot">
    <div class="row">
      <div class="col-sm-1"> </div>
      <div class="col-sm-10">
        <!-- Input for members -->
        <div class="input-group">
          <input type="text" class="form-control" placeholder="Add member" id="text">
          <div class="input-group-btn">
            <button class="btn btn-success btn-addmember">
                <i class="glyphicon glyphicon-plus">
                </i></button>
          </div>
          <div class="input-group-btn">
            <button class="btn btn-success btn-team1">
                <i class="glyphicon glyphicon-plus">
                </i> TEAM1</button>
          </div>
          <div class="input-group-btn">
            <button class="btn btn-success btn-team2">
                <i class="glyphicon glyphicon-plus">
                </i> TEAM2</button>
          </div>
        </div>
        <br>
        <br>
      </div>
    </div>
    <div class="form-group">
      <label for="exampleFormControlTextarea1">Team1</label>
      <textarea class="form-control" id="team1" rows="3">
        vivamus eget 
        lacus vitae 
        mi vulputate 
        varius integer 
        suscipit orci 
        condimentum
        vestibulum
        </textarea>
      <label for="exampleFormControlTextarea1">Team2</label>
      <textarea class="form-control" id="team2" rows="3">
        vivamus eget 
        lacus vitae 
        mi vulputate 
        varius integer 
        suscipit orci 
        condimentum
        vestibulum
        </textarea>
    </div>
    <p class="copyright">Created by: Me - <a href="mailto:[email protected]">[email protected]</a></p>
  </div>

  <br>
  <h2 class="text text-success">Team Members</h2>
  <br>

  <div class="container-fluid">
    <div id="memberlist" class="row">

    </div>
  </div>
</center>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

  1. clarify HTML:

    • use CSS margin property instead of <br>
    • use the text-center class instead of center tag
    • place footer at the end of HTML in the <footer> tag
    • use .container instead of .container-fluid and empty columns
    • use hierarchy .form-inline > .input-group > .input-group-btn to organize inputs and buttons as inline form
  2. CSS:

    • I've removed some CSS instructions and restored original behavior of Bootstrap's container. I recommend to use Bootstrap 4 or 5 with flex-boxes to achieve more flexible possibilities.
    • I've added styles for form-inline block to improve its layout on he narrow screen.
  3. Redo JS:

    • wrap the alert HTML code in a separate function addMember
    • add one more function for adding of teams by the textarea's id
    • simplify click code for the buttons with a help of these two functions
    • use .toggleClass() instead of .css() to change the alerts appearance on click
    • use .append() to add new alert at the end of the list
  4. Make columns:

    • Wrap alerts in the column block to place several alerts in a line.
    • Add two classes col-xs-6 col-sm-4 to organize alerts in 2 columns on mobile and in 3 columns on the wide screen.
    • Now Bootstrap's dismissible alerts are not enough, because we have to remove the column block too. So we can remove the alert-dismissible class and the data-dismissattribute. I've added alternative member closing code by jQuery too.

https://codepen.io/glebkema/pen/jOLJYJE

$(document).ready(function() {
  var memberList = $("#memberlist");

  memberList.on("click", ".alert", function() {
    $(this).toggleClass("member-clicked");
  });

  memberList.on("click", ".close", function() {
    var memberColumn = $(this).parent().parent();
    memberColumn.fadeOut();
  });

  $(".btn-addmember").click(function() {
    var newMember = $("#text").val().trim();
    if (newMember) {
      addMember(newMember);
    } else {
      alert("Please, enter the name of the member");
    }
    $("#text").val("");
  });

  $(".btn[data-team]").click(function() {
    addTeam($(this).data("team"));
  });

  function addMember(member) {
    member = member.trim();
    if (member) {
      memberList.append(
        `<div ><div >`  
        `<span  aria-label="close">&times;</span>`  
        member  
        `</div></div>`
      );
    }
  }

  function addTeam(id) {
    var team = $("#"   id).val().trim();
    if (team) {
      var members = team.split("\n");
      console.log(members);
      for (var i = 0; i < members.length; i  ) {
        addMember(members[i]);
      }
    }
  }
});
.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  right: 0;
}

/* create a class for .toggleClass() */
.alert.member-clicked {
  text-decoration-line: line-through;
  background-color: #ddd;
  border-color: #ddd;
}

/* use margin instead of <br> */
.copyright {
  margin-top: 10px;
}

/* use these values not only when the screen is wider than 768 pixels */
.form-inline.form-members .input-group {
  display: inline-table;
  vertical-align: middle;
}
.form-inline.form-members .input-group .input-group-btn {
  width: auto;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container">
  <h2 class="text text-success text-center">Team Members</h2>
  <div id="memberlist" class="row"></div>
</div>

<footer class="footer">
  <div class="container">
    <!-- Input for members -->
    <div class="form-inline form-members">
      <div class="input-group">
        <input type="text" class="form-control" placeholder="Add member" id="text">
        <div class="input-group-btn">
          <button class="btn btn-success btn-addmember"><i class="glyphicon glyphicon-plus"></i></button>
        </div>
      </div>
      <button class="btn btn-success" data-team="team1"><i class="glyphicon glyphicon-plus"></i> TEAM1</button>
      <button class="btn btn-success" data-team="team2"><i class="glyphicon glyphicon-plus"></i> TEAM2</button>
    </div>

    <div class="form-group hidden">
      <label for="exampleFormControlTextarea1">Team1</label>
      <textarea class="form-control" id="team1" rows="7">
        1 vivamus eget 
        1 lacus vitae 
        1 mi vulputate 
        1 varius integer 
        1 suscipit orci 
        1 condimentum
        1 vestibulum
      </textarea>

      <label for="exampleFormControlTextarea1">Team2</label>
      <textarea class="form-control" id="team2" rows="7">
        2 vivamus eget 
        2 lacus vitae 
        2 mi vulputate 
        2 varius integer 
        2 suscipit orci 
        2 condimentum
        2 vestibulum
      </textarea>
    </div>

    <p class="copyright text-right">Created by: Me - <a href="mailto:[email protected]">[email protected]</a></p>
  </div>
</footer>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

However much your layout can be improved, other answers here have already did that for you. But if you're looking to just break the alert into 3 columns without having to change your entire code, just add the class col-sm-4 (which will produce 3 columns on the viewport from the small screen size) on your JS line where you assign the value to y. So that particular statement now becomes:

var y = "<div class='alert alert-success alert-dismissible fade in'><a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>" $('#text').val() "</div>";

  • Related