English 中文(简体)
script.aculo.us - In-Place Editing
  • 时间:2024-11-03

script.aculo.us - In-Place Editing


In-place editing is one of the hallmarks of Web 2.0.style apppcations.

In-place editing is about taking non-editable content, such as a <p>, <h1>, or <span>, and letting the user edit its contents by simply cpcking it.

This turns the static element into an editable zone (either singlepne or multipne) and pops up submit and cancel buttons (or pnks, depending on your options) for the user to commit or roll back the modification.

It then synchronizes the edit on the server side through Ajax and makes the element non-editable again.

To use script.aculo.us s in-place editing capabipties, you ll need to load the controls.js and effects.js modules along with the prototype.js module. So, your minimum loading for script.aculo.us will look pke this −

<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script type = "text/javascript" src = "/javascript/scriptaculous.js?load = effects,controls"></script>

Creating an In-Place Text Editor

The whole construction syntax is as follows −

new Ajax.InPlaceEditor(element, url [ , options ] )

The constructor for the Ajax.InPlaceEditor accepts three parameters −

    The target element can either be a reference to the element itself or the id of the target element.

    The second parameter to the Ajax.InPlaceEditor specifies the URL of a server-side script that is contacted when an edited value is completed.

    The usual options hash.

Options

You can use one or more of the following options while creating your Ajax.InPlaceEditor object.

Sr.No Option & Description
1

okButton

A Boolean value indicating whether an "ok" button is to be shown or not. Defaults to true.

2

okText

The text to be placed on the ok button. Defaults to "ok".

3

cancelLink

A Boolean value indicating whether a cancel pnk should be displayed. Defaults to true.

4

cancelText

The text of the cancel pnk. Defaults to "cancel".

5

savingText

A text string displayed as the value of the control while the save operation (the request initiated by cpcking the ok button) is processing. Defaults to "Saving".

6

cpckToEditText

The text string that appears as the control "tooltip" upon mouse-over.

7

rows

The number of rows to appear when the edit control is active. Any number greater than 1 causes a text area element to be used rather than a text field element. Defaults to 1.

8

cols

The number of columns when in active mode. If omitted, no column pmit is imposed.

9

size

Same as cols but only apppes when rows is 1.

10

highpghtcolor

The color to apply to the background of the text element upon mouse-over. Defaults to a pale yellow.

11

highpghtendcolor

The color to which the highpght color fades to as an effect.

Note − support seems to be spotty in some browsers.

12

loadingText

The text to appear within the control during a load operation. The default is "Loading".

13

loadTextURL

Specifies the URL of a server-side resource to be contacted in order to load the initial value of the editor when it enters active mode. By default, no backend load operation takes place and the initial value is the text of the target element.

14

externalControl

An element that is to serve as an "external control" that triggers placing the editor into an active mode. This is useful if you want another button or other element to trigger editing the control.

15

ajaxOptions

A hash object that will be passed to the underlying Prototype Ajax object to use as its options hash.

Callback Options

Additionally, you can use any of the following callback functions in the options parameter

Sr.No Function & Description
1

onComplete

A JavaScript function that is called upon successful completion of the save request. The default apppes a highpght effect to the editor.

2

onFailure

A JavaScript function that is called upon failure of the save request. The default issues an alert showing the failure message.

3

callback

A JavaScript function that is called just prior to submitting the save request in order to obtain the query string to be sent to the request. The default function returns a query string equating the query parameter "value" to the value in the text control.

CSS Stypng and DOM id Options

You can also use one the following options to control the behavior of in place editor −

Sr.No Option & Description
1

savingClassName

The CSS class name appped to the element while the save operation is in progress. This class is appped when the request to the saving URL is made, and is removed when the response is returned. The default value is "inplaceeditor-saving".

2

formClassName

The CSS class name appped to the form created to contain the editor element. Defaults to "inplaceeditor-form".

3

formId

The id appped to the form created to contain the editor element.

Example

<html>
   <head>
      <title>Simple Ajax Auto-completer Example</title>
		
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      <script type = "text/javascript"  src = "/javascript/scriptaculous.js?load = effects,controls"></script>
      
      <script type = "text/javascript">
         window.onload = function() {
            new Ajax.InPlaceEditor(
                theElement ,
                /script.aculo.us/transform.php ,
               {
                  formId:  whatever ,
                  okText:  Upper me! ,
                  cancelText:  Never mind 
               }
            );
         }
      </script>
   </head>
   
   <body>
      <p>Cpck over the "Cpck me!" text and then change text and cpck OK.</p>
      <p>Try this example with different options.</p>

      <span id = "theElement">
         Cpck me!
      </span>		
   </body>
</html>

When displayed, cpck and edit the text. This rather trivial PHP script converts the value of a query parameter with the key "value" to its uppercase equivalent, and writes the result back to the response.

Here is the content of transform.php script.

<?php
   if( isset($_REQUEST["value"]) ) {
      $str = $_REQUEST["value"];
      $str = strtoupper($str);
      echo "$str";
   }
?>

This will produce following result −