Archive for June, 2007

h1

Drag and Drop ordering

June 29, 2007

Here is a very useful link that walks you through how to create a simple drag and drop section for a website. It creates a list and lets you move items around the list so that you can order them as you wish. With a little bit of CSS, this could turn into a really nice simple organiser.

http://dustinweber.com/cakephp/cakephp-sortable-ajax-drag-drops-the-basics/

I did find that I had to edit my javascript includes a little bit though. I had the scriptaculous libraries loaded with this tagged on at the end
?load=effects
With this in place, I was getting errors that sortable wasn’t defined. Once I removed it, it all worked perfectly.

h1

Using autocomplete and Scriptaculous

June 27, 2007

Using this fine page as a reference (http://metapundit.net/sections/blog/ajax_autocomplete_with_scriptaculous), I finally got the autocomplete function working as I wanted.
I wanted to show a venue with some extra info in brackets and when it was selected, to update a hidden input with the id of the venue from the database.

The secret to this was realising that even though I was passing the id of the link tag, it was numberical which is not allowed. So following metapundit’s advice, I added auto_ to the front of the id value. This was then stripped off with javascript and the number put into the hidden input.

Also, a nice thing about the scriptaculous autocomplete is that if you want to show extra info but not have it passed to the page, enclose it in

h1

Having problems moving from localhost to remote server

June 20, 2007

I have part of my application working nicely now and it mostly does what I want. But that is only on my localhost. When I copy it up to the remote server, most of it runs. It’s only when I try to update the page with an AJAX call that I get:

Not Found

The requested URL was not found on this server.
Apache Server at Port

I know I ran into this problem before, but can I remember how to fix it? Not on your nelly.

Oh well, at least if I get it fixed this time, it will be logged here for me to remember!

h1

Submitting a form using javascript

June 19, 2007

I have been struggling with a way to change a form depending on what category was selected. I have a number of forms that input different levels of information depending on the category.

I started off using the $ajax->observeField() function. The problem was that the category selection was half way down the form, so anything that was entered previously was being lost when the form was submitted. To change this I thought it would be a simple change to using $ajax->observeForm(). But no. That would be too easy. :)

I ended up having to code it by hand, so here is what I did.

Create a DIV to take the information from the form after it is submitted.
Create the dropdown that will be used to select the category.
Add this piece of code:

codeBlock('Event.observe("categories",
\'change\',
function(event){ new Ajax.Updater(\'form\',
\'worksheets/add/'.$worksheet['Worksheet']['sdate'].'\',
{asynchronous:true, evalScripts:true, parameters:Form.serialize(Event.element(event).form), requestHeaders:[\'X-Update\', \'form\']}) }, false);'); ?>

This observes the “categories” dropdown and when it changes it will call the add function and pass the sdate to it. It then updates the “form” DIV with the results.

h1

Using unBindModel() in CakePHP

June 15, 2007

If you have a lot of associations in your model setups, sometimes you dont want all that information to be returned in a query. The answer to this is to use the unBinModel() function. Using this you can unbind certain parts of your associations.
The easiest way to demonstrate this is with an example.

If you have a user model with the following associations:

var $hasMany = array(
'Worksheet' => array('className' => 'Worksheet'
),
'Login' => array('className' => 'Login'
),
'Expense' => array('className' => 'Expense'
)
);
var $belongsTo = array(
'Level' =>
array('className' => 'Level'),
'County' =>
array('className' => 'County'),
'Province' =>
array('className' => 'Province')
);

That is potentially a lot of information to get back each time you query the User.
So if we define

$this->User->unBindModel(array(hasMany => array('Login', 'Expense')));

before we call

$this->User->findAll();

we no longer return information from the Login and Expenses tables. This can be used for any of the associations and can cut down query times dramatically.

h1

Leave an empty option at the top of a select element

June 1, 2007

In 1.2, when you use the $form->input() function to create a select element, it no longer creates the empty value at the top.
To get this back, simply pass the empty variable back and set it as true.


echo $form->input('category_id', array('empty' => true));

Follow

Get every new post delivered to your Inbox.