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

时间:2011-09-30 08:27:03

标签: javascript jquery jquery-ui jqgrid autocomplete

在我的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);
    }

1 个答案:

答案 0 :(得分:1)

在编译时将其全部转换为一个案例:

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);
    }