0

我已按照 http://www.liferaysavvy.com/2014/02/liferay-aui-auto-complete-list.html进行自动完成,我正在从服务器获取以下格式的数据(在代码中:

 alert('jsonArray came: ' + jsonArray))

给出以下,

   [
    {
        "roleName": "Organization User",
        "roleId": 10147
    },
    {
        "roleName": "Site Admin",
        "roleId": 16883
    },
    {
        "roleName": "Student",
        "roleId": 18139
    },
    {
        "roleName": "Parent",
        "roleId": 18146
    },
    {
        "roleName": "Faculty",
        "roleId": 18153
    },
    {
        "roleName": "SchooloneStudent",
        "roleId": 27701
    }
]

,但从代码中: new A.AutoCompleteList 它无法获取数据。谁能帮我?

<aui:script>
AUI().use('autocomplete-list','aui-base','aui-io-request','autocomplete-filters','autocomplete-highlighters',function (A) {
A.io.request('<%=getRoles%>',{
dataType: 'json',
method: 'GET',
on: {
success: function(event, id, obj) {

var instance = this ;
var data = instance.get('responseData');
var jsonArray = JSON.stringify(data);
alert('result came: ' + data);
alert('jsonArray came: ' + jsonArray);
try {
new A.AutoCompleteList(
{
allowBrowserAutocomplete: 'false',
activateFirstItem: 'true',
inputNode: '#<portlet:namespace />ListOfRoles',
resultTextLocator: 'roleName',
render: 'true',
resultHighlighter: 'phraseMatch',
resultFilters:['phraseMatch'],
source:jsonArray,
});
}catch(e) {
alert('not working : ' + e);
}
}

}
});

});
</aui:script>
4

1 回答 1

0

我认为您无需表示响应,因为您已经收到 JSON 格式的响应。可能有一些数据不匹配......试试这个:

<aui:script>
AUI().use('autocomplete-list','aui-base','aui-io-request','autocomplete-filters','autocomplete-highlighters',function (A) {
A.io.request('<%=getRoles%>',{
            dataType: 'json',
            method: 'GET',
            on: {
                success: function(event, id, obj) {

                    try {
                    new A.AutoCompleteList(
                            {
                            allowBrowserAutocomplete: 'false',
                            activateFirstItem: 'true',
                            inputNode: '#<portlet:namespace />roleNames',
                            resultTextLocator: 'roleName',
                            render: 'true',
                            resultHighlighter: 'phraseMatch',
                            resultFilters:['phraseMatch'],
                            source:this.get('responseData'),
                            autoLoad:false,
                            sync:false,
                            });
                        }catch(e) {
                        alert('not working: ' + e);
                    }
                }

                }
            });

});
</aui:script>

谢谢,

~钱丹

于 2014-12-24T05:20:16.113 回答