sfDtAjaxPlugin - 0.9.0

Symfony plugin with Ajax controls

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Dependencies Releases Changelog Contribute
Show source

sfDtAjaxPlugin - Ajax Components

Author

Copyright (C) 2008 Jacques Philip.

License

MIT

Prerequisites

Prototype v 1.6+ is required
Script.aculo.us controls

Controls

SelectAutocompleter:

screen shot

Similar to AutoCompleter but pops up a select element instead of divs.

This makes it a lot more suited for Ajax incremental searches in a database or other indexed data source.

The improvements are:

  • The number of choices is not limited by the number of rows displayed since the select element can scroll. (The number of rows is customizable)

  • The select element holds a key-value pair for each choice, so records can be located by primary key.

  • If cache mode is enabled (the default), the search is incremental in the cache and an Ajax request is made only if a match is not found.

  • The search can be case sensitive or not.

  • The match can be anywhere or at the beginning of the string.

  • The Ajax action needs only to send key-value pairs in JSON format instead of HTML code to save bandwidth and speed up calls numerous records are returned.

Browser compatibility

Tested on IE 6 and 7, Firefox 2, Opera 9 on Windows.
Let me know if you find compatibility problems on other platforms.

Installation

Install plugin as usual.

Create a link or virtual directory called sfDtAjaxPlugin, pointing to the web folder of the plugin.

Settings and customization

Options are passed as parameters of the helper function and customization can be made in the Ajax action.

Tag options

  • id: This option will be used for the id of the text input field. (Default is same as the field name you pass in the helper function)

  • value_id: And id for the hidden field that will receive the value property of the option selected. This field will be created by the helper. (Default is the name passed in the helper, incremented by '_auto_complete_value')

  • Other tags: Of course, you can include any html tag that will be passed to the text input field.

Javascript options

The differences with AutoCompleter are:

  • token: Removed

  • row_count: The number of rows displayed in the select element. (Default is 7)

  • use_cache: If true, the results will be cached on the client. (Default is true)

  • case_sensitive: If true, the search in the cache is case sensitive. (Default is false)
    Note that you still have to implement a case sensitive search on the server in the Ajax action.

  • match_beginning: If true, the match will be only at the beginning of the string. (Default is true)
    Note that you still have to implement a corresponding search on the server in the Ajax action. Something like 'LIKE token%' versus 'LIKE %token%' for a search anywhere in the string.

The Ajax action

The action must return an array of key-value pairs in JSON format.
There must be no field names or HTML code in there.

Here is an example with doctrine showing that the text displayed in the select element can be a combination of several fields. (The function CONCAT_WS is My Sql specific and requires portability for expressions turned off)

public function executeAutocomplete()
  {
    $arr = Doctrine_Query::CREATE()
      ->select("d.id, CONCAT_WS('-', d.name, d.birth_year) dog_name")
      ->from('Dog d')
      ->where('d.name LIKE ?', $this->getRequestParameter('dog_name') .  '%')
      ->addWhere('d.kennel_id = ?', Utils::getCurrentKennelId())
      ->orderBy('name, birth_year')
      ->setHydrationMode(Doctrine::FETCH_ARRAY)
      ->execute();

    //Remove the field names
    $result = array();
    foreach ($arr as $rec) {
        $result[] = array_values($rec);
    }      

    $this->renderText(json_encode($result));
    return sfView::NONE;
  }

Usage

<?php use_helper('Form', Javascript', 'DtAjax') ?>
<?php echo form_tag('admin_dog/show_auto', array('method' => 'GET')) ?>
Find a dog by name:<br/>
<?php echo input_select_auto_complete_tag('dog_name', '',
  'admin_dog/autocomplete',
  array('id'    => 'the_id'),
  array('row_count' => '10')
) ?>
<?php echo submit_tag('Find') ?>