ExtJS Javascript Library for Beginners!

The Ext Javascript library for beginners, as I learn ExtJS so will you!

Posts Tagged ‘css

Vertical Checkbox Groups

with one comment

While working on my usual project at work I encountered the need to solve the problem of having check boxes display in a vertical list, across several columns from a non numerically indexed PHP array. I came up with two solutions; one used Ext JS and one was plain PHP with some CSS thrown in for good measure. One thing I noticed was that this task is not as easy as it seems.

Take for example the following array.

$list =   array('AL'=>"Alabama",
'AK'=>"Alaska",
'AZ'=>"Arizona",
'AR'=>"Arkansas",
'CA'=>"California",
'CO'=>"Colorado",
'CT'=>"Connecticut",
'DE'=>"Delaware",
'DC'=>"District Of Columbia",
'FL'=>"Florida",
'GA'=>"Georgia"

... and so on

I won’t paste the entire code as it’d just waste space.

To get that into a 4 column list proved rather tricky.  My first attempt was using an Ext.FormPanel, combined with PHP to generate the array. Not a glamorous solution by any means but it worked .

Here’s the code. It’s pretty self explanatory. are in mind the list variable from above.

<script>

/**
* here we loop our $list variable creating some JSON objects
* which match the specification for an Ext.Checkbox
* The PHP you see just stops the last , being added to the list, IE chokes on that!
*
*/
var _list_items = [
< ?php $lastKey = count($list); $i = 0; foreach($list as $program_id => $program_name): $i++?>
{
boxLabel: '< ?php echo ucwords($program_name);?>',
name: 'programs[]',
inputValue : '< ?php echo $program_id;?>'
}< ?php echo ($i !== $lastKey) ? "," : "";?>
< ?php endforeach;?>
];

Ext.onReady(function(){

/*
Create our FormPanel variable in the usual panel way.
*/
var _fp = new Ext.FormPanel({
renderTo: Ext.getBody(),
frame:true,
title : 'States',
method: 'POST',
url : 'post.php', // dummy page, which spits back the variables passed through
width: 795,
autoHeight:true,
bodyStyle : {
padding: '10px'
},
/**
After seeing the examples of this it's pretty easy, the one thing to rememember is if you don't want a random
Colon (:) to appear in the top left when you enter no fieldLabel, use hideLabel, I think there's a bug for the Ext Boys.

*/
items : [{
xtype: 'checkboxgroup',
hideLabel : true,
id : "program_checkboxes",
itemCls: 'x-check-group-alt',
columns: 5,
vertical: true,
items: _list_items // our php generated list.
}],
// add some funky buttons. bbar is such a cool config option!
bbar : [
'->',
new Ext.Button({
text : 'Clear',
handler : function(){
_fp.getForm().reset();
},
style : {
display: 'inline',
margin : '0px 5px'
}
}),
new Ext.Button({
text: 'Save',
style : {
display: 'inline',
margin : '0px 5px'
},
handler: function(){
if(_fp.getForm())
{
_fp.getForm().submit();
}
}

})]
});
});

</script>

So that should be sufficient to get your Ext.FormPanel displaying check boxes vertically. What surprised me though, was the amount of processing it took to render! Reloading that simple script caused my Firefox to freeze on several occasions. Not sure why!

Anyway here’s how it looked;

ext-formpanel-checkboxes

After the serveral crashes I decided to try some PHP to lighten my mood.  The solution I came up with is a little tricky to get your head around but, is very flexible, and doesn’t crash the browser so much. All it takes is a little PHP and CSS.

<div id="form-container">

< ?php

// set the maximum columns as a hard limit.

$max_cols = 4;

// determine how many rows to output per column
$perCol = round(ceil(count($list) / $max_cols));

// create two numerically indexed array of keys and values.
// this is important, because our initial array is indexed by strings, we need to re index them.
$names = array_values($list);
$ids = array_keys($list);

// now its a simple case of looping and creating a column out of a div each time.
?>

< ?php for ($i = 0; $i < $max_cols; $i++):?>
<div class="programcol">
<ul>
< ?php for ($js = 0; $js < $perCol; $js++):?>
< ?php if(strlen(current($names)) > 0):?>
<li>
<input type="checkbox" name="programs[]" value="<?php echo current($ids);?/>">
<label>< ?php echo ucwords(current($names));?></label>
</li>
< ?php endif;?>
< ?php next($names); next($ids);?>
< ?php endfor;?>
</ul>
</div>
< ?php endfor;?>
</div>

< ?php
/**
* We loop down each time, iterating the array in sections, making a column out of each section.
* I'm sure this can be improved upon!
*/
?>

That generates the divs’ holding a list of columns, now to sprinkle on a little CSS to get the vertical styling.

<style>
body
{
font-family:tahoma;
font-size:10px;
}

#form-container
{
width:795px;
min-height:250px;
max-height:500px;
float:left;

}

.programcol
{
float:left;
width:195px;
height:auto;
text-align:left;
display:inline;
clear:none;

}

.programcol li
{
margin: 4px 0px;
height:12px;
padding:0.25em
}

.programcol li label
{
margin-left:4px;
position:relative;
top:-2px
}

</style>

…and we’re all good, we have vertical checkboxes across several columns! viola

php-vertical-checkboxes

Hope you liked this.

James

Written by jameshd

4th February, 2009 at 10:21 pm

My Fake Page Rank Bar with CSS

leave a comment »

Hey,

The new year is getting off to a slow start, but here’s something that maybe of use to someone…We had a problem showing a PageRank bar on our site, something to do with IE bitching about non secure items, so to save an API call and bandwidth I made this. Now we just get the PageRank and display some div’s!


< ?php
$pagerank = rand(0,10);

?>

<style>
div.pagerank-holder
{
height:5px;
width:50px;
background-color:#f7f7f7;
border:1px #848484 solid;
float:left;
}

.rank
{
background-color:#009933;
height:5px;
float:left;
}

</style>
<p>Page rank: < ?php echo $pagerank;?> </p>
<div class="pagerank-holder">
<div class="rank" style="width:<?php echo ($pagerank/10)*100;?>%"></div>
</div>

Written by jameshd

5th January, 2009 at 4:21 pm

Posted in Randoms

Tagged with , ,

Follow

Get every new post delivered to your Inbox.