JQgrid 与 jquery UI 自动完成验证

发布于 2024-12-05 16:21:05 字数 2609 浏览 0 评论 0原文

我所拥有的是 jqgrid,它在列中使用 jQueryUI 自动完成功能。一切都很完美,除了一件小事,如果用户选择写“fsdfsdfsfasf”并按回车键,我的程序当然会惊慌失措并告诉我这样的项目不存在。

我想要的是进行某种验证,因此如果用户输入自动完成列表中不存在的某些数据,程序应自动将自动完成文本设置为 "" 就像这里一样: http://view.jquery.com/trunk/plugins/autocomplete/demo/ 在“月”字段。

由于我使用的是 jQueryUI 自动完成而不是 jquery 自动完成,我无法使用 mustmatch 选项。

{ 
    name: 'Brand', 
    index: 'Brand', 
    align: 'left', 
    width: 50, 
    sortable: true, 
    editable: true, 
    edittype: 'text', 
    editoptions: {
        dataInit:
        function (elem) {
            $(elem).autocomplete({
                delay: 0,
                minLength: 0,
                source: '@Url.Action("GetBrands")',
                minChars: 0,
                max: 12,
                autoFill: true,
                mustMatch: true,
                matchContains: false,
                scrollHeight: 220,
                formatItem: function(data, i, total) {
                    return data[0];
                },
                select: function (event, ui) {
                    if (ui.item.value == "Opret ny Brand...") {
                        $(function () {
                            var dialogDiv = $('#dialog');
                            var viewUrl = '@Url.Action("CreateBrand")';

                            $.get(viewUrl, function (data) {
                                dialogDiv.html(data);

                                var $form = $("#updateCarForm");
                                $form.unbind();
                                $form.data("validator", null);

                                //Check document for changes
                                $.validator.unobtrusive.parse(document);

                                //Re add validation with changes
                                $form.validate($form.data("unobtrusiveValidation").options);

                                //open dialog
                                dialogDiv.dialog('open');
                            });

                        });
                    }
                }

            })
            .data("autocomplete")._renderItem = function (ul, item) {
                return $("<li></li>")
                    .data("item.autocomplete", item)
                    .append("<a><span style='display:inline-block;width:60px;'><b>" +
                        item.value + "</b></span></a>")
                    .appendTo(ul);
           };
        }
    }
},

What I have is jqgrid which uses jQueryUI autocomplete in a column. Everything works perfect except 1 little thing, if user choose to write "fsdfsdfsfasf" and press enter my program will of course freak out and tell me that such item does not exist.

What I want is to make some kind of validation, so if user enter some data which does not exist in autocomplete list program should automatically put autocomplete text to be "" just like here: http://view.jquery.com/trunk/plugins/autocomplete/demo/ in the "month" field.

Since I am using jQueryUI autocomplete and not jquery autocomplete i cant use the mustmatch option.

{ 
    name: 'Brand', 
    index: 'Brand', 
    align: 'left', 
    width: 50, 
    sortable: true, 
    editable: true, 
    edittype: 'text', 
    editoptions: {
        dataInit:
        function (elem) {
            $(elem).autocomplete({
                delay: 0,
                minLength: 0,
                source: '@Url.Action("GetBrands")',
                minChars: 0,
                max: 12,
                autoFill: true,
                mustMatch: true,
                matchContains: false,
                scrollHeight: 220,
                formatItem: function(data, i, total) {
                    return data[0];
                },
                select: function (event, ui) {
                    if (ui.item.value == "Opret ny Brand...") {
                        $(function () {
                            var dialogDiv = $('#dialog');
                            var viewUrl = '@Url.Action("CreateBrand")';

                            $.get(viewUrl, function (data) {
                                dialogDiv.html(data);

                                var $form = $("#updateCarForm");
                                $form.unbind();
                                $form.data("validator", null);

                                //Check document for changes
                                $.validator.unobtrusive.parse(document);

                                //Re add validation with changes
                                $form.validate($form.data("unobtrusiveValidation").options);

                                //open dialog
                                dialogDiv.dialog('open');
                            });

                        });
                    }
                }

            })
            .data("autocomplete")._renderItem = function (ul, item) {
                return $("<li></li>")
                    .data("item.autocomplete", item)
                    .append("<a><span style='display:inline-block;width:60px;'><b>" +
                        item.value + "</b></span></a>")
                    .appendTo(ul);
           };
        }
    }
},

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜雨飘雪 2024-12-12 16:21:05

您可以通过提供一个函数作为自动完成的 source 参数的参数并自行执行 AJAX 请求来实现此功能。类似于:

$("#auto").autocomplete({
    source: function(request, response) {
        // Manually perform the AJAX request:
        var element = this.element;
        $.get('@Url.Action("GetBrands")', request, function (results) {
            // No results? Clear out the input field.
            if (!results.length) {
                element.val('');
            }
            // Notify the widget of the results.
            response(results);
        });
    }
});

(未经测试。您可能需要调整 AJAX 调用)

如果需要,我可以提供远程示例,但概念与使用本地数据源的示例相同:http://jsfiddle.net/andrewwhitaker/Dgfgr/

You can implement this functionality by providing a function as an argument to the source parameter of autocomplete and doing the AJAX request yourself. Something like:

$("#auto").autocomplete({
    source: function(request, response) {
        // Manually perform the AJAX request:
        var element = this.element;
        $.get('@Url.Action("GetBrands")', request, function (results) {
            // No results? Clear out the input field.
            if (!results.length) {
                element.val('');
            }
            // Notify the widget of the results.
            response(results);
        });
    }
});

(Untested. You may need to tweak the AJAX call)

I can provide a remote example if necessary, but the concept is the same with an example using a local data source: http://jsfiddle.net/andrewwhitaker/Dgfgr/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文