Jqgrid ui 自动完成:禁用区分大小写

发布于 2024-12-07 02:49:33 字数 2170 浏览 1 评论 0原文

在我的 JQgrid 中,我有一个区分大小写的 ui atocomplete 列。

例如,我的网格中有 2 个项目:Ivan 和 ivan,如果我输入“i”,自动完成功能将仅返回 ivan。 我试图在源代码中创建一个函数:但我失败了,因为我的ajax调用总是返回对象对象而不是项目。有什么想法吗?

自动完成代码:

$(elem).autocomplete({
                      delay: 0,
                      minLength: 0,

                      source: function (req, response) {
                          alert(req);
                          $.ajax({
                              mtype: "post",
                              url: '@Url.Action("GetBrands")',
                               dataType: "json",
                              async: false,
                              cache: false,
                              data: { term: req },
                              success: function (data) {
                                  alert(data);

                                  var re = $.ui.autocomplete.escapeRegex(req.term);

                                  var matcher = new RegExp("^" + re, "i");

                                  response($.grep(data, function (item) { return matcher.test(item.value); }));

                              }

                          });

                      },

控制器端代码:

 public virtual JsonResult GetBrands(string term)
    {
        if (term == null) term = string.Empty;

        var vendorId = _service.GetVendorIdByUsername(GetUserName());

        var brands = _service.GetBrandsByVendor(vendorId);
        var brand = new BrandsViewModel();
        brand.BrandName = "Opret ny Brand...";
        brands.Add(brand);

        foreach (var brandsViewModel in brands)
        {

            if (brandsViewModel.BrandName == "Intet")
            {
                brandsViewModel.BrandName = "";
            }
        }

        return Json((from item in brands
                     where item.BrandName.Contains(term)
                     select new
                                {
                                    value = item.BrandName
                                    //votes = item.Votes,
                                }).ToArray(),
                    JsonRequestBehavior.AllowGet);
    }

In my JQgrid i have a ui atocomplete column which are Case sensitive.

For example i have 2 Items in my grid: Ivan and ivan, if i type "i" autocomplete will return only ivan.
I have tryed to make a function inside of source: but i failed since my ajax call always return object Object instead of an item. Any ideas?

Code for autocomplete:

$(elem).autocomplete({
                      delay: 0,
                      minLength: 0,

                      source: function (req, response) {
                          alert(req);
                          $.ajax({
                              mtype: "post",
                              url: '@Url.Action("GetBrands")',
                               dataType: "json",
                              async: false,
                              cache: false,
                              data: { term: req },
                              success: function (data) {
                                  alert(data);

                                  var re = $.ui.autocomplete.escapeRegex(req.term);

                                  var matcher = new RegExp("^" + re, "i");

                                  response($.grep(data, function (item) { return matcher.test(item.value); }));

                              }

                          });

                      },

Controller side code:

 public virtual JsonResult GetBrands(string term)
    {
        if (term == null) term = string.Empty;

        var vendorId = _service.GetVendorIdByUsername(GetUserName());

        var brands = _service.GetBrandsByVendor(vendorId);
        var brand = new BrandsViewModel();
        brand.BrandName = "Opret ny Brand...";
        brands.Add(brand);

        foreach (var brandsViewModel in brands)
        {

            if (brandsViewModel.BrandName == "Intet")
            {
                brandsViewModel.BrandName = "";
            }
        }

        return Json((from item in brands
                     where item.BrandName.Contains(term)
                     select new
                                {
                                    value = item.BrandName
                                    //votes = item.Votes,
                                }).ToArray(),
                    JsonRequestBehavior.AllowGet);
    }

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

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

发布评论

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

评论(1

甩你一脸翔 2024-12-14 02:49:33

比较时将其全部转换为一种情况:

public virtual JsonResult GetBrands(string term)
    {
        if (term == null) term = string.Empty;
        term = term.ToLower();
        var vendorId = _service.GetVendorIdByUsername(GetUserName());

        var brands = _service.GetBrandsByVendor(vendorId);
        var brand = new BrandsViewModel();
        brand.BrandName = "Opret ny Brand...";
        brands.Add(brand);

        foreach (var brandsViewModel in brands)
        {

            if (brandsViewModel.BrandName == "Intet")
            {
                brandsViewModel.BrandName = "";
            }
        }

        return Json((from item in brands
                     where item.BrandName.ToLower().Contains(term)
                     select new
                                {
                                    value = item.BrandName
                                    //votes = item.Votes,
                                }).ToArray(),
                    JsonRequestBehavior.AllowGet);
    }

convert it all to one case when compering:

public virtual JsonResult GetBrands(string term)
    {
        if (term == null) term = string.Empty;
        term = term.ToLower();
        var vendorId = _service.GetVendorIdByUsername(GetUserName());

        var brands = _service.GetBrandsByVendor(vendorId);
        var brand = new BrandsViewModel();
        brand.BrandName = "Opret ny Brand...";
        brands.Add(brand);

        foreach (var brandsViewModel in brands)
        {

            if (brandsViewModel.BrandName == "Intet")
            {
                brandsViewModel.BrandName = "";
            }
        }

        return Json((from item in brands
                     where item.BrandName.ToLower().Contains(term)
                     select new
                                {
                                    value = item.BrandName
                                    //votes = item.Votes,
                                }).ToArray(),
                    JsonRequestBehavior.AllowGet);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文