Spring和JQuery:自动完成并由ID保存[编辑]

时间:2019-01-15 04:47:15

标签: jquery spring model-view-controller autocomplete

我的数据库中有一个角色数据列表:

角色:

|role_id|role_name|description |deleted_status|
###############################################
|1      |Admin01  |Role Admin  |0             |
|2      |Admin02  |Role Admin  |0             |
|3      |Admin03  |Role Admin  |0             |
|4      |Staff01  |Role Staff  |0             |
|5      |Staff02  |Role Staff  |0             |
|6      |Manager01|Role Manager|0             |
|7      |Manager02|Role Manager|0             |

我已经创建了自动完成脚本,但是以某种方式它仅保存了role_name。在这里,我想通过role_id保存它。

jsp:

<input type="hidden" id="autoRoleId" path="roleId" />
<input type="text" id="autoRoleName" path="roleName" />

javascript:

$(document).ready(function() {
$('#autoRoleName').autocomplete({
    serviceUrl: '${pageContext.request.contextPath}/autoCompleteRoleX',
    paramName: "role",
    delimiter: ",",
    transformResult: function(response) {
        return {
            suggestions: $.map($.parseJSON(response), function(item){
                return {
                    data: item,
                    value: item.roleName,
                    result: item.roleName
                };
            })
        };
    }
}).bind("result", function(data, value) {
    $("#autoRoleId").val(value.id);
});
});

但是保存后,保存的值role_id为null。

这是控制器:

@RequestMapping(value="/autoCompleteRoleX", method=RequestMethod.GET)
public @ResponseBody
List<Role> getAutoCompleteRole(@RequestParam("role") String role) {
    List<Role> result = new ArrayList<Role>();

    try {
        String[] likeCols = {"roleName"};
        Object[] likePars = {role};

        String[] eqCols = {"deletedStatus"};
        Object[] eqPars = {Integer.valueOf(0)};

        String[] sortColumns = {"roleId"};

        List<Object> objects = roleService.searchDataLike(Role.class, likeCols, likePars, eqCols, eqPars, 0, 10);

        for (Object obj : objects) {
            result.add((Role) obj);
        }

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

    return result;
}

非常感谢。

0 个答案:

没有答案