Search code examples
javascriptajaxcoldfusioncfajaxproxy

Ajax call to coldfusion component using cfajaxproxy


I´m stuck with the follow code, hope somebody can help or give me some advice: basically what i´m doing is to call a cf function using ajax: when the user write an id in the field "artid" information related to that id will appear in the others cfinput. the above code works fine.

<cfajaxproxy bind="cfc:portal_dgmu.pruebas.addPerson.test.getData({artid@keyup})" onsuccess="showData">

<script>

function showData(d) {
var data = {}
for(var i=0; i < d.COLUMNS.length; i++) {
    data[d.COLUMNS[i]] = d.DATA[0][i]
}
document.getElementById('artname').value = data["ARTNAME"]
document.getElementById('description').value = data["DESCRIPTION"]
document.getElementById('price').value = data["PRIZE"]

}
</script>
<html>
<cfform>
id: <cfinput type="text" name="artid" id="artid"><br/>
nombre: <cfinput type="text" name="artname" id="artname" readonly="true"><br/>
descripcion: <cftextarea name="description" id="description" readonly="true"></cftextarea><br/>
precio: <cfinput type="text" name="price" id="price" readonly="true"><br/>
</cfform>
</html>

i also have the follow code:

<script>
function addFields(){
        var number = document.getElementById("member").value;
        var container = document.getElementById("container");
        while (container.hasChildNodes()) {
            container.removeChild(container.lastChild);
        }
        for (i=0;i<number;i++){
            container.appendChild(document.createTextNode(i+1));
            var input = document.createElement("input");
            input.type = "text";
            input.name = "member" + i;

    var boton = document.createElement("input");
            boton.name = "boton" + i;       

            container.appendChild(input);
    container.appendChild(boton);
            container.appendChild(document.createElement("br"));
        }
    }
</script>

<html>
Enter a number of persons: (max. 10)<input type="text" id="member" size="3" name="member" value="">
<a href="#" id="filldetails" onclick="addFields()">Agregar</a>
<div id="container"/>
</html>

the above code is just adding text fields depending on the number that the user's entered,it also works fine.

My problem is that i just can´t figure out how to integrate both of them, i mean what i need to do is depending on the number that the user types were deployed text fields and the first one must type an id which will bring the data related to that ID.

Thank you so much!!


Solution

  • Here's a example using jquery that covers everything you want to do.

    i changed the ajax request to fire on change of the input field instead of keyup but you could change that easily if required.

    The cfc might need changing if you using cf < 9 and I've only tested it in firefox but it should work in other browsers.

    index.cfm

    <html>
        <head>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function(){
                    var formToCopy = $('<form/>')
                                        .append($('<input/>', {'name': 'dataId', 'type': 'text', 'placeholder': 'Some Id Here'}))
                                        .append($('<input/>', {'name': 'testInput', 'type': 'text', 'readonly': 'readonly'}))
                                        .append($('<textarea/>', {'name': 'testArea', 'readonly': 'readonly'}));
    
                    $('#howMany').on('change', null, null, function(e){
                        var numToAdd = $(this).val();
    
                        if(isNaN(numToAdd)){
                            return;
                        }
                        $('#container').html('');
                        for(var i=0; i < numToAdd; i++){
                            $(formToCopy).clone().appendTo('#container');
                        }
                    });
    
                    $('#moar').on('click', null, null, function(e){
                        $(formToCopy).clone().appendTo('#container');
                    });
    
                    $('#less').on('click', null, null, function(e){
                        $('#container form:last').remove();
                    });
    
                    $(document).on('change', '#container form input[name="dataId"]', null, function(e){
                        if($(this).val().length === 0){
                            return;
                        }
    
                        var $formToFill = $(this).closest('form');
                        var ajaxSuccessFunc = function(data){
                            for(var key in data){
                                var item = data[key];
                                var $field = $formToFill.find('input[name="'+key+'"], textarea[name="'+key+'"]');
                                if($field.length === 1){
                                    $field.val(item);
                                }
                            }
                        };
    
                        $.ajax({
                            'url': '/test.cfc',
                            'dataType': 'json',
                            'data': {
                                        'method': 'getData',
                                        'id': $(this).val()
                                    },
                            'success': ajaxSuccessFunc
                        })
                    });
                });
            </script>
        </head>
        <body>
            <label>How Many? <input type="text" id="howMany" /></label>
            <p><a href="#zzz" id="moar">+</a> / <a href="#zzz" id="less">-</a></p>
            <div id="container">
    
            </div>
        </body>
    </html>
    

    test.cfc

    <cfcomponent output="false">
    
        <cffunction name="getData" access="remote" output="false" returnformat="json">
            <cfargument name="id" type="string" required="true">
            <cfscript>
                local.ret = {};
    
                ret["testInput"] = rand();
                ret["testArea"] = "blah blah";
                return ret;
            </cfscript>
        </cffunction>
    
    </cfcomponent>