有趣的是:使用jqgrid实现jquery autoComplete,而不会覆盖令人兴奋的searchOptions

时间:2012-01-11 08:07:53

标签: jquery-ui search jqgrid jquery-autocomplete

奥列格 - 你在这里吗?

我正在使用jqGrid,我是根据类型在colModel中设置我的searchoption

像这样:

var columnModel = [{ name: 'ID', index: 'ID', sortable: true,searchoptions: { sopt: ['gt']}},
{ name: 'FirstName', index: 'FirstName', sortable: true, searchoptions: { sopt: ['cn']} },
{ name: 'LastName', index: 'LastName', sortable: true ,searchoptions: { sopt: ['ge']}}
];

现在我加载网格后,我想使用下面的代码,以便在网格的搜索框中添加自动完成功能:

for (var i = 0; i < columnModel.length; i++) {
    var nameCol = columnModel[i].name;
    myGrid.jqGrid('setColProp', nameCol,
            {
                searchoptions: {
                    dataInit: function (elem) {
                        $(elem).autocomplete({
                            source: function (request, response) {
                                autoFillSearch(request, response, $(elem).attr('name'));
                            },
                            minLength: 1
                        });
                    }
                }
            });
}
myGrid.jqGrid('filterToolbar', { stringResult: true, searchOnEnter: true });

autoFillSearch函数是这样的:

function autoFillSearch(request, response, columnToSearchName) {
    var paramters = {
        colName: columnToSearchName,
        prefixText: request.term
    };
    $.ajax({
        url: './ViewNQueryData.asmx/AutoCompleteSearch',
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(paramters),
        success: function (data) {
            response($.each(data.d, function (index, value) {
                return {
                    label: value,
                    value: index
                }
            }));
        }
    });
}

问题是新的colModels有第二次创建的搜索选项而没有我希望他们拥有的特定“sopt”......

我有没有办法改变第二个搜索选项,以便它从原始colMadel获得“sopt”选项?

提前谢谢。

1 个答案:

答案 0 :(得分:0)

答案是:

for (var i = 0; i < columnModel.length; i++) {
    var nameCol = columnModel[i].name;
    var soptOption = columnModel[i].searchoptions.sopt;
    myGrid.jqGrid('setColProp', nameCol,
                {
                    searchoptions: {
                        sopt: soptOption,
                        dataInit: function (elem) {
                            $(elem).autocomplete({
                                source: function (request, response) {
                                    autoFillSearch(request, response, $(elem).attr('name'));
                                },
                                minLength: 1
                            });
                        }
                    }
                });
}