« Previous entry | Next entry » Browse > Snippets

Skip to comments (4) Ajax: Edit text as ComboBox
Posted by komielan on May 05 2006 @ 01:17  :: 4139 unique visits

Same functionality as an editable text except it will dynamically build a ComboBox.
From this page, there is a static example as well as a brief explanation.

Thanks for the inspiration!

Note that this example is dependent upon the Prototype JavaScript framework, version 1.4.0.

JS : CB_editinplace.js
CODE: JAVASCRIPT
function makeEditable(id, table)
{
    var success = function(t){setData(t, id, table);}
    var failure = function(t){dataFail(t, id, table);}
    var myAjax = new Ajax.Request('editcb.php', {method:'post', postBody:'&getdp=true&fieldName=' + id, onSuccess:success, onFailure:failure});

}
function dataFail(t, id)
{
    $(id).innerHTML = 'Data Provider failed ...';
}
function setData(t, id, table)
{
    Event.observe(id, 'click', function(){edit($(id), table, t.responseText)}, false);
    Event.observe(id, 'mouseover', function(){showAsEditable($(id))}, false);
    Event.observe(id, 'mouseout', function(){showAsEditable($(id), true)}, false);
}

function edit(obj, table, cb)
{
    Element.hide(obj);
    new Insertion.After(obj, cb);
    Event.observe(obj.id+'_edit_cb', 'change', function(){saveChanges(obj, table)}, false);
    //Event.observe(obj.id+'_edit_cb', 'mouseout', function(){cleanUp($(obj), true)}, false);
}

function showAsEditable(obj, clear)
{
    if (!clear)
        Element.addClassName(obj, 'editable');
            else
                Element.removeClassName(obj, 'editable');
}

function saveChanges(obj, table)
{
    var new_content = escape($F(obj.id+'_edit_cb'));
    var kids = document.getElementById(obj.id+'_edit_cb').childNodes;
    for(n in kids)
    {
        if(kids[n].value == new_content)
        {
            //alert('found: ' + kids[n].innerHTML);
            var label = kids[n].innerHTML;
        }
    }
    obj.innerHTML = "Saving...";
    Element.addClassName(obj, 'changed');
    cleanUp(obj, true);
   
    var success = function(t){editComplete(t, obj);}
    var failure = function(t){editFailed(t, obj);}
    var url = 'editcb.php';
    var pars = '&tableName='+table+'&ajax=editinplace&id='+obj.id+'&content='+new_content+'&label='+label;
    //alert("pars: " + pars);
    var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});
}

function cleanUp(obj, keepEditable)
{
    Element.remove(obj.id+'_edit_cb');
    Element.show(obj);
    if (!keepEditable) showAsEditable(obj, true);
    //new Effect.Highlight(obj, { duration: 3.0 });
}

function editComplete(t, obj)
{
    highlight(obj, t.responseText, 1500)
}
function highlight(o, s, n)
{
    //$('statusContainer').style.visibility = "visible";
    Element.addClassName(o, 'changed');
    o.innerHTML = s;
    setTimeout( "document.getElementById('"+ o.id +"').className = '';", n );
}
function editFailed(t, obj)
{
    obj.innerHTML = 'Could not save...';
    cleanUp(obj);
}

PHP : editcb.php
CODE: PHP
<?php
   
    # this file echos either the newly selected label or echos a static dropdown list.
    # Normally part of a larger class, these routines would be updating DB's
   
    if(isset($_POST['getdp']))
    {
        echo '<select name="state" id="state_edit_cb"><option value="AL">Alabama</option><option value="AK">Alaska</option><option value="AZ">Arizona</option><option value="AR">Arkansas</option><option value="CA">California</option><option value="CO">Colorado</option><option value="CT">Connecticut</option><option value="DE">Delaware</option><option value="DC">Dist of Columbia</option><option value="FL">Florida</option><option value="GA">Georgia</option><option value="HI">Hawaii</option><option value="ID">Idaho</option><option value="IL">Illinois</option><option value="IN">Indiana</option><option value="IA">Iowa</option><option value="KS">Kansas</option><option value="KY">Kentucky</option><option value="LA">Louisiana</option><option value="ME">Maine</option><option value="MD">Maryland</option><option value="MA">Massachusetts</option><option value="MI">Michigan</option><option value="MN">Minnesota</option><option value="MS">Mississippi</option><option value="MO">Missouri</option><option value="MT">Montana</option><option value="NE">Nebraska</option><option value="NV">Nevada</option><option value="NH">New Hampshire</option><option value="NJ">New Jersey</option><option value="NM">New Mexico</option><option value="NY">New York</option><option value="NC">North Carolina</option><option value="ND">North Dakota</option><option value="OH">Ohio</option><option value="OK">Oklahoma</option><option value="OR">Oregon</option><option value="PA">Pennsylvania</option><option value="RI">Rhode Island</option><option value="SC">South Carolina</option><option value="SD">South Dakota</option><option value="TN">Tennessee</option><option value="TX">Texas</option><option value="UT">Utah</option><option value="VT">Vermont</option><option value="VA">Virginia</option><option value="WA">Washington</option><option value="WV">West Virginia</option><option value="WI">Wisconsin</option><option value="WY">Wyoming</option></select>';
    }else
    {
        $fp = fopen('state.txt', 'w') or die("Failed to open file for writing.");
        $label = $_POST['label'];

        if(fwrite($fp, $label))
        {
            echo $label;
        }else
        {
            echo "Failed to update the text";
        }
    }
   
?>

PHP : CBEdit.inc.php
CODE: PHP
<?php

    class CBEdit
    {
        var $label = '';
        function CBEdit($idName, $label)
        {
            if($label == '') $label = 'Select ...';
            $this->label = $label;
            $this->getEditInPlaceCode($idName, $label);
        }
       
        function getEditInPlaceCode($idName, $label)
        { echo '<div id="'.$idName.'" name="'.$idName.'" title="Click here to edit">'.$label.'</div>'; }
    }
?>

CSS : simple.css
CODE: CSS
body
{
    font: 13px Tahoma, Verdana;
    background: #ccc;
    color: #000;
    margin: 0;
    padding: 0;
}
p
{
    font: 11px Tahoma, Verdana;
}
.wrap
{
    background: #f8f8f8;
    border: 5px solid #fff;
    clear: both;
    margin: 25px 15%;
    padding: .7em 1em;
    width:450px;
   
    margin-left:-225px;
    position:absolute;
    left:50%;
}

/*CB EDIT*/
.editable
{
    cursor:pointer;
    background: #FEFFBF;
    border: 1px solid #b2b2b2;
    color: #000;
    padding: 2px;
    width:250px;
}
.changed
{
    background: #ffcfcf;
    border: 1px solid #b2b2b2;
    color: #000;
    padding: 2px;
    width:250px;
}
#example
{

    padding: 15px;
}


CODE: HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <title>Editing Text In Place as a ComboBox</title>
    <script src="prototype_1.4.js" type="text/javascript"></script>
    <script src="CB_editinplace.js" type="text/javascript"></script>
    <link rel="stylesheet" href="simple.css" type="text/css" />
</head>

<body>
    <div class="wrap">


    <h2>Editing Text In Place as a ComboBox</h2>
    <div id="example">
    <?php
        require_once('CBEdit.inc.php');
        $label = file_get_contents('state.txt');
        $states = new CBEdit('state', $label);
       
    ?>

    </div>
    <script> makeEditable('state', 'user'); </script>

   
</div>
</body>
</html>

4 comments posted so far
Add your own »

1. On Apr 14 2009 @ 13:30 guest wrote:

Find information about tiffany ,gucci ,chanel and other jewelry online shopping at online shopping ,
jewelry,craft,antique,daily news online collection at Online Collector ,Tiffany Jewelry including Tiffany Necklaces,Tiffany Rings, and tiffany bracelets…
Guide To Buy Tiffany Product ,   fashion jewelry provide,Tiffany,Oxette,Swarovski,CHANEL Jewelry Information
Find the discount gucci shoes



Louis Vuitton is luxury gifts, French fashion, the replica Louis Vuitton Handbag is woman best friend.Monogram Groom.
Offers Discount Louis Vuitton handbags and Louis Vuitton bags and all other designer handbags,free global fast shipping,low price and top quality.Monogram Jokes,Monogram Suede cheap Louis Vuitton
Louis Vuitton.

Looking For Gucci Shoes ? Gucci Store provide gucci Mens shoes,gucci Womens shoes
Wonderful Gucci shoes sale Gucci men's shoes and Gucci women's shoes at discount Gucci Shoes prices.
cheap gucci Shoes
Gucci Shoes and gucci clothing Spring - Summer 2009, Prada Shoes and prada clothing from the Latest Collection 2009 and Dolce Gabbana Clothing 2009
Gucci Loafers
Gucci Sneakers

Louis Vuitton Handbags
UGGs
Louis Vuitton Handbags
Gucci Shoes
Louis Vuitton
UGG Boots
Louis Vuitton Handbags
gucci shoes
Monogram Groom
Discount Louis Vuitton
UGG Boots

Add a new comment

Name:
Password: (leave empty for anonymous comment)
 
View formatting tags Comment: