Solved

How to organize a large number of checkboxes into columns (using Twitter Bootstrap V3)

Posted on 2015-02-02
5
965 Views
Last Modified: 2016-02-25
I have a large number of checkboxes ...

 				<div class="control-group">
					<label for="location" class="control-label">Location: </label>
					<div class="controls">
						<div class="input-append">
							<?php
							
							$tagsdb = explode(',',$_REQUEST['locations']);
							
							foreach ($GLOBALS['all_towns'] AS $tag) {
								$tagid = str_replace(' ', '_', $tag);
								if(in_array($tag, $tagsdb)) $checked = ' CHECKED'; else unset($checked);
								echo "<label class='inline' style='padding-right:8px;'><input type='checkbox' id='location__{$tagid}' name='location[]' value='$tag'{$checked}> $tag</label>";
							}
							?>
						</div>
					</div>
				</div>

Open in new window


What's the best way to automatically organize them into columns...
0
Comment
Question by:Mark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 43

Accepted Solution

by:
Rob earned 500 total points
ID: 40585091
Add the "row" class to your outer <div>.

Add  the relevant column sizing  to each <label> element e.g.

http://jsbin.com/bicogu/1/edit?html,js,output

<div class="input-append row">
      <label class='inline' style='padding-right:8px;'><input type='checkbox' id='location__{$tagid}' name='location[]' value='$tag'>$tag</label>
...
</div>

Open in new window


I've added the class via script but you could easily do this as you're outputting the php

$(function() {
  // for testing, change to col-xs-[num] to see other configurations
  $('.inline').addClass('col-xs-4');
});

Open in new window


in php:
foreach ($GLOBALS['all_towns'] AS $tag) {
	$tagid = str_replace(' ', '_', $tag);
	if(in_array($tag, $tagsdb)) $checked = ' CHECKED'; else unset($checked);
	echo "<label class='inline col-xs-4' style='padding-right:8px;'><input type='checkbox' id='location__{$tagid}' name='location[]' value='$tag'{$checked}> $tag</label>";
}

Open in new window

0
 

Author Comment

by:Mark
ID: 40585334
I will try this in the morning, thanks!
0
 

Author Closing Comment

by:Mark
ID: 40586641
WOW, works perfect, thanks a million
0
 

Author Comment

by:Mark
ID: 40586654
Anyway to make it collapse into one row or two depending on the size of the screen? (on my phone it just bunches together into 3 columns and also some of the checkboxes break off from the labels line:

Example:

[]
My checkbox label
0
 
LVL 43

Expert Comment

by:Rob
ID: 40587321
Sure, just change the class to col-sm instead of xs
0

Featured Post

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question