using variable in autocomplete return

82 Views Asked by At

I have a function to work with my autocomplete, however I need to be able to change the return columns dynamic, does somebody knows this syntax? So in this example I want to replace "i.id" by i.return_id. I have tried i.return_id, i[return_id], i.[return_id], i.[" + return_id + "], i." + return_id + ".... running out of option.

function _autocomplete(element, type, pageid, tableid,return_name, return_id) {" +
                "alert(element);" +
                "if (type == 2) {" +
                "$(element) .autocomplete({" +
                "   source: function(request, response) {" + 
                "   var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), \"i\"); " +
                "   $.ajax({" + 
                "       url: 'dataServices/objects.asmx/InvokeData'" +
                "       ,data: JSON.stringify({ q: request.term ,pid:pageid,tid:tableid,filter:JSON.stringify([{field:{label:\"\",value:\"return_name\"},operate:{label:\"\",value:\"ct\"},value:{label:\"\",value:\"\"+request.term+\"\"}}]),order:\"\",cid:38, sid: 2,jqGridSubId:\"\",jqGridLevel:0  })" +
                "       ,dataType: 'JSON'" +
                "       ,type: 'POST'" +
                "       ,contentType: 'application/json;charset=utf-8'" +
                "       ,success: function(data)" +
                "           {" +
                "           _dataStore = null; _dataStore = data;" +
                "           response($.map(data.d, function (i, item) { " + 
                "               return { " +
                "                   label: \"[\" + i.id + \"] \" + item[\" + return_name + \"], value: \"[\" + i.id + \"] \" + i.[\" + return_name + \"] " + 
                "                       }" + 
                "                                                       }" +
                "                   ))" +
                "           }" +
                "       });" +
                "}})}" +
1

There are 1 best solutions below

2
On

If I understand your code correctly, you are trying to only return records that match return_id, which is passed it. Can you try this...

response($.map(data.d, function (i, item) { 
    if (i.id == return_id) {
        return { 
            label: \"[\" + i.id + \"] \" + item[\" + return_name + \"], value: \"[\" + i.id + \"]
                    + i.[\" + return_name + \"]
        }
    } 
}));