Monday, February 28, 2011

Wrapping Grids in HTML with jQuery

So I was looking for a quick way to make an interface that allows me to dynamically add or remove the same type of child item to a grid-like parent element, where you have a certain number of items per row and they wrap around automagically. Anyway, I came up with a fun little jQuery function to automate it. Here we go:


function gridize(parent,children_per_row) {
var i=0;
$(parent).children().each( function() {
$(this).css("float","left");
if(i++ % children_per_row == 0) {
$(this).css("clear","left");
} else {
$(this).css("clear","none");
}
}
);
}


To use it, just toss in the id of your parent div and call gridize each time you add or remove an element. I used the jQuery templates plugin for defining and adding children.

Here's some scaffolding to get it working. This part isn't tested since I modified it on the fly from my working code, so YMMV:


$.template( "child_template", 
"<div id=\"child_${id}\">\
Whatever you want.\
        <input type=\"button\" onclick=\"del_child(${id})\" value=\"X\">\
</div>");


var current_id = 0;


function add_child() {
$.tmpl("child_template", {id: current_id}).appendTo("#parent");


        current_id++;

update_grid();
}


function del_child(id) {
$("#child_" + id).remove();

update_grid();
}


function update_grid() {
        gridize("#parent",3);
}


And some HTML:

<h1>A grid of items</h1>
<div id="parent">
</div>
<div style="clear: both;">
<input type="button" value="Add Child" onclick="add_child()">
</div>

Have fun!

Monday, December 27, 2010

Multidimensional Resolutions

Rumors of my lack of blogmanship were not very exaggerated, but here, have a post anyway. As the title suggests, this is a rather cliched list of resolutions for 2011, encompassing a few different areas of development. I may or may not follow anything on this list, but my goal here is to put some bite-size chunks that I may actually be able to accomplish in 12 months. We'll see.

Physical

  • Starting the beginning of spring semester, start and complete the six-week 100 pushups program. This isn't too excessive of a program, as it starts small and ramps up in difficulty. When completed, I should be able to do 100 consecutive pushups, which I would say is a good basic fitness measure.
  • Ditto with the 50 pullups program, which will probably take 2-3 times as long to finish.
  • Something ab-ish.
  • Something cardio. Interval training perhaps.
  • After completing the first two programs, start working towards one-handed pushups and pullups.
  • Go from 'ok' shape to 'great' shape by camp this summer. I have 5 months to get this done, so that's really not that unreasonable.
  • This may not happen until later this year, but I'd like to train back into basketball.
  • Dietary: I've already gone gluten-free (because I had to,) but now I'd really like to spend more time cooking, with real food. That means much less processed, much more produce and 'real food.'
  • Sleep. Regularly and at the same time every night. This may be the hardest thing on this list.


Mental

  • READ. Can't overstate this one. If I don't have something non-school related to consume, my sanity will be at risk.
  • Take enough introvert time everyday. This will unfortunately entail staying ahead of the game in my classes.
  • Nerd Alert: Learn a new programming language. They say you should learn one a year. Top contenders right now are Scala, Go, and R.

Spiritual

  • Restating from mental, the introvert time thing. I need to find meaningful ways to actually relax to keep myself anywhere near emotionally healthy.
  • Paint. Create. Compose. Yes please.
  • Some type of meaningful devotions. I've found that my traditional Bible reading schedule just doesn't work, and has been slipping more. I need to mix it up, and also make my prayers less cliche and more meaningful. This one wont necessarily be hard except in coming up with the right ideas.
There. I don't think there's anything completely undoable about that list. At the same time, it should give me something to look back on when I undoubtedly start procrastinating.

Sunday, November 14, 2010

Meaning

Every weekend for Sabbath School, our custom is to go around the room give summaries of our weeks. This time, for some reason, quite a few people didn’t seem to remember what they did that week, or there was nothing remarkable to report. I was one of them. Call me narcissistic (I prefer intuitive), but once in awhile I’ll say something that sticks with me.

“Wow, we all lead really unfulfilled lives.”

Tonight as I’m preparing for bed, I’ve started reading a book about Kirsten Wolcott, the student missionary who was murdered in Yap. This is difficult for me to read for multiple reasons, but it has definitely got me thinking about meaning. Our weeks, our days, our seconds should not go unnoticed, especially by ourselves. There is a higher purpose to our lives, and the more we ignore that or shrug it off the fewer stories we’ll have to tell. The fewer songs we’ll have to sing. Her life had meaning because she created the stories, and sang the most important song of all, the song of her life.

So I thought if I can pour my soul out once in awhile, even in such a public place, maybe I can start to tell my own stories. This blog will definitely not always be serious. There will hopefully be stories, and art, and music, uncountable bad puns, and probably even a few technological asides, but my greatest hope is that I can accurately reflect my character, and start to piece together where that fits in the human puzzle.

If you’d like to be a part of my story, read on.