Archive for February, 2012

Dynamically populate select box from JSON in javascript/jQuery

JSON [JavaScriptObjectNotation] has become the defacto standard for web-consumers these days. I also interact with it in a frequent manner.
I will try to give some simple example on using JSON with old school javascript and jquery.
Lets have the following JSON for our example:

var cars=[{"color":"red","made":"toyota","model":"corrola","mileage":"10000"},{"color":"silver","made":"honda","model":"accord","mileage":"160000"},{"color":"white","made":"nissan","model":"maxima","mileage":"12200"}];

Just ordinary simple data to play with.
Using Old School Javascript:
Assuming there is select box with id “dynamic_slct”

var options = '';
slctbox = document.getElementById('dynamic_slct');
for(var i=0 ; i<cars.length ; i++)
{
    var label = cars[i]['made'];
    var model = cars[i]['model']; //or whatever the value you want to show
    var opt = document.createElement('option);
    slctbox.options.add(new Option(label, model));
}

As simple as that, the ‘cars’ should be available on the page being accessible to the javascript that is populating the options. if you are working on PHP, for example, spitting the json_encode(array) would provide what is needed for this

Uisng jQuery

var opt="";
$.each(cars, function(){
   var label = this['made'];
   var model = this['model];
   opt += ""+model+"";
});
$('#dynamic_slct').html(opt);

That isIT

, ,

Leave a Comment

Setting autoindent tab and shift width on vim/vi

Vim/vi is a real handy tool for updating any file on many linux destros including the favored ubuntu.
While working you might want to set the tab width [say 4 chars or whatever] along with shift size. Also how about to auto indent whenever you hit enter- all easy..
look for your vimrc – a config file for vim, it would be in

 /etc/vim/vimrc 

by default on ubuntu or
if you do

locate vimrc 

on your terminal it would show you the path,
Then do add the following lines in it

set autoindent
set shiftwidth=4
set tabstop=4

Well, you can change the the value of the tabs and shift as you want it..
happy vim’ing

, , ,

Leave a Comment

VI/VIM not colorizing syntax like html or php

I am brushing my old pc and installing ubuntu on it. As I was installing php-mysql and related packages I noticed that the vim/vi editor is not highlighting html or php syntaxes.
That would be it if you are running a bit older version of the ubuntu. Upgrading vim will take care of the problem:

sudo apt-get install vim-genome
sudo apt-get install vim

then go to the vimrc file and – it would be in /etc/vim/vimrc by default and uncomment the line syntax on

then you should get your vim rocking!

, ,

Leave a Comment

You will have to enable the component called ‘universe’

Have you got this message while using apt-get install on your ubuntu? if so, here is how to get away with it,
go to System->administration->Software Sources and the first tab you would see would be then check on the community maintained opensource (universe)
That would take of it.

, ,

Leave a Comment

Follow

Get every new post delivered to your Inbox.