0

我是 MVC 的新手。使用 Grid.MVC 显示列表。我正在尝试为字符串和日期列创建自定义填充器 [Demo 1。我已经阅读了 Grid.MVC 的文档,但它不起作用。选择没有任何选项。这是我的脚本

<script>
    function CustomersFilterWidget() {
        /***
        * This method must return type of registered widget type in 'SetFilterWidgetType' method
        */
        this.getAssociatedTypes = function () {
            return ["CustomCompanyNameFilterWidget"];
        };
        /***
        * This method invokes when filter widget was shown on the page
        */
        this.onShow = function () {
            /* Place your on show logic here */
        };

        this.showClearFilterButton = function () {
            return true;
        };
        /***
        * This method will invoke when user was clicked on filter button.
        * container - html element, which must contain widget layout;
        * lang - current language settings;
        * typeName - current column type (if widget assign to multipile types, see: getAssociatedTypes);
        * values - current filter values. Array of objects [{filterValue: '', filterType:'1'}];
        * cb - callback function that must invoked when user want to filter this column. Widget must pass filter type and filter value.
        * data - widget data passed from the server
        */
        this.onRender = function (container, lang, typeName, values, cb, data) {
            //store parameters:
            this.cb = cb;
            this.container = container;
            this.lang = lang;

            //this filterwidget demo supports only 1 filter value for column column
            this.value = values.length > 0 ? values[0] : { filterType: 1, filterValue: "" };

            this.renderWidget(); //onRender filter widget
            this.loadCustomers(); //load customer's list from the server
            this.registerEvents(); //handle events
        };
        this.renderWidget = function () {
            var html = '<p><i>This is custom filter widget demo.</i></p>\
                <p>Select customer to filter:</p>\
                <select style="width:250px;" class="grid-filter-type customerslist form-control">\
                </select>';
            this.container.append(html);
        };
        /***
        * Method loads all customers from the server via Ajax:
        */
        this.loadCustomers = function () {
            var $this = this;
            $.post("/QuanLySanPham/GetTenSach", function (data) {
                $this.fillCustomers(data.Items);
            });
        };
        /***
        * Method fill customers select list by data
        */
        this.fillCustomers = function (items) {
            var customerList = this.container.find(".customerslist");
            for (var i = 0; i < items.length; i++) {
                alert(data);
                customerList.append('<option ' + (items[i] == this.value.filterValue ? 'selected="selected"' : '') + ' value="' + items[i] + '">' + items[i] + '</option>');
            }
        };
        /***
        * Internal method that register event handlers for 'apply' button.
        */
        this.registerEvents = function () {
            //get list with customers
            var customerList = this.container.find(".customerslist");
            //save current context:
            var $context = this;
            //register onclick event handler
            customerList.change(function () {
                //invoke callback with selected filter values:
                var values = [{ filterValue: $(this).val(), filterType: 1 /* Equals */ }];
                $context.cb(values);
            });
        };
    }
    $(function () {
        pageGrids.ordersGrid.addFilterWidget(new CustomersFilterWidget());
    });
</script>

这是我的控制器

QuanLyBanSachEntities db = new QuanLyBanSachEntities();
    public ActionResult Index(int? page)
    {
        //int pageNumber = (page ?? 1);
        //int pageSize = 10;
        //return View(db.Saches.ToList().OrderBy(n => n.MaSach).ToPagedList(pageNumber, pageSize));
        List<WebBanSach.Models.Sach> customerslist = db.Saches.ToList();
        return View(customerslist);
    }
    public JsonResult GetTenSach()
    {
        List<string> customerslist = db.Saches.Select(s => s.TenSach).ToList();
        return Json(customerslist, JsonRequestBehavior.AllowGet);
    }

你能给我一个关于如何填写这个选择标签的建议吗?

4

0 回答 0