Solved

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

Posted on 2015-02-02
5
831 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
  • 3
  • 2
5 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE 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 42

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now