package WebGUI::Operation::MLajax; use strict; use WebGUI::SQL; use JSON; use Data::Dumper; # @(#) Program: $File: //depot/testcenter/webgui7/WebGUICustom/lib/WebGUI/Operation/MLajax.pm $ # @(#) Function: Provides JSON formatted output to build autocomplete forms # @(#) Version: $Revision: #8 $ # @(#) $Change: 3518 $ # @(#) Datum: $Date: 2011/07/07 $ # @(#) Author: $Author: mlammers $ webguide@innovate.de # @(#) =head1 NAME Package WebGUI::Macro::MLajax =head1 DESCRIPTION Operation for executing SQL select-type statements and returning e.g. JSON formatted output for generating autocompleted forms =head2 www_ajaxinput =head3 SQL In this version an SQL statement depends on input params: tab, fld, fmt and term These are combined to: "select $fld from $tab where $fld like '%$term%' "; term is build by using jQuery and activating or changing the form field =head3 format Describes how to format the results of the SQL statement. 'array' delivers a JSONed array refence 'hash' delevers a JSON of an ArrayRefOfHashRefs =head3 Minimum Url The Url send by an input form as example: ?op=ajaxinput&tab=assetData&fld=title&term=abc =head2 Example use Build a SQLReport. Filling-in the form, sends ajax-requests to the server and actualises the input-field. =head3 Description in SQL-Report:
WebGUI Autocomplete Example

Start typing the name of a WebGUI asset

=head3 SQL in SQLReport Select distinct title, url from assetData where title like '%^FormParam(searchfield);%' order by title =head3 Metadata in SQLReport =head2 INSTALL Put this op in your WebGUI/Operation directory and activete it by putting "ajaxinput" => "MLajax", in Operation.pm (getOperations) Use Firebug (Network) to test, what happens =cut #------------------------------------------------------------------- sub www_ajaxinput { my $session = shift; my $dbh = $session->db; my $tab = $session->form->param('tab'); my $fld = $session->form->param('fld'); my $fmt = $session->form->param('fmt') || "array"; my $term = $session->form->param('term'); my $output; my $rows; my $sql = "select distinct $fld from $tab where $fld like ? order by $fld"; if ( $fmt eq "hash" ) { $rows = $dbh->buildArrayRefOfHashRefs( $sql, [ "%".$term."%" ] ); $output = JSON::to_json($rows); } else { $rows = $dbh->buildArrayRef( $sql, [ "%".$term."%" ] ); $output = JSON::to_json($rows); } return $output; } ## end sub www_ajaxinput 1;