如何处理jqGrid组合框列中的“值”和“文本”属性?

时间:2013-10-19 10:03:12

标签: jquery asp.net-mvc jqgrid jqgrid-asp.net mvcjqgrid

我有Trirand MVC jqGrid包,但我确信这个问题对jqGrid来说是一个整体。我有一个绑定到Id的列:

new JQGridColumn
    {
        DataField = "PaymentSchemeId",                                           
        HeaderText = "Payment Scheme",
        DataType = typeof(ParkadePaymentScheme),
        Searchable = true,
        SearchToolBarOperation = SearchOperation.IsEqualTo,
        SearchType = SearchType.DropDown,
        SearchControlID = WidgetNames.DropDownSearchPaymentScheme,
        Editable = true,
        EditType = EditType.DropDown,
        EditorControlID = WidgetNames.DropDownEditPaymentScheme
    },

在我的GridModel中(网格的视图模型)。在显示模式下,网格会按预期显示PaymentSchemeId的值。在编辑模式下,它显示所有付款方案的下拉列表,在选择一个后,显示一个名称而不是Id,但我认为这只是旁边效应。

如何设置列以在显示PaymentSchemeId属性值时绑定并存储PaymentSchemeName值?

网格呈现的脚本相当长,但我把它全部包括在内,因为害怕编写一些有意义的东西。

jQuery(document).ready(function () {
    jQuery('#IndexGrid').jqGrid({
        url: '/Parkade/IndexDataRequest?jqGridID=IndexGrid',
        editurl: '/Parkade/EditRow?jqGridID=IndexGrid&editMode=1',
        mtype: 'GET',
        datatype: 'json',
        page: 1,
        colNames: ["Edit Actions", "Id", "Name", "Payment Scheme", "Active?", "Remarks"],
        colModel: [{
            "search": false,
            "sortable": false,
            "formatoptions": {
                "editbutton": true,
                "delbutton": true,
                "keys": true
            },
            "width": 50,
            "formatter": "actions",
            "name": "",
            "index": ""
        }, {
            "searchoptions": {
                "searchhidden": true
            },
            "index": "Id",
            "hidden": true,
            "key": true,
            "name": "Id"
        }, {
            "stype": "select",
            "editable": true,
            "index": "Name",
            "searchoptions": {
                "value": ":All;Brooklyn Mall:Brooklyn Mall"
            },
            "name": "Name"
        }, {
            "editoptions": {
                "value": "3:Pay on Entry;1:Pay on Exit;2:Pay on Foot"
            },
            "editable": true,
            "stype": "select",
            "edittype": "select",
            "searchoptions": {
                "value": ":All;Pay on Foot:Pay on Foot"
            },
            "name": "PaymentSchemeId",
            "index": "PaymentSchemeId"
        }, {
            "editoptions": {
                "value": "True:Yes;False:No"
            },
            "editable": true,
            "stype": "select",
            "edittype": "select",
            "searchoptions": {
                "value": ":All;True:True"
            },
            "name": "IsActive",
            "index": "IsActive"
        }, {
            "width": 300,
            "index": "Remarks",
            "searchoptions": {
                dataInit: function (el) {
                    setTimeout(function () {
                        var ec = 'AutoCompleteRemarks';
                        if (typeof (jQuery(el).autocomplete) !== 'function') alert('JQAutoComplete javascript not present on the page. Please, include jquery.jqAutoComplete.min.js');
                        jQuery(el).autocomplete(eval(ec + '_acid'));
                    }, 200);
                }
            },
            "editable": true,
            "name": "Remarks"
        }],
        viewrecords: true,
        scrollrows: false,
        prmNames: {
            id: "Id"
        },
        headertitles: true,
        autowidth: true,
        pager: jQuery('#IndexGrid_pager'),
        loadError: jqGrid_aspnet_loadErrorHandler,
        rowNum: 20,
        rowList: [10, 20, 30],
        editDialogOptions: {
            "recreateForm": true,
            errorTextFormat: function (data) {
                return 'Error: ' + data.responseText
            },
            editData: {
                __RequestVerificationToken: jQuery('input[name=__RequestVerificationToken]').val()
            }
        },
        addDialogOptions: {
            "recreateForm": true,
            errorTextFormat: function (data) {
                return 'Error: ' + data.responseText
            },
            editData: {
                __RequestVerificationToken: jQuery('input[name=__RequestVerificationToken]').val()
            }
        },
        delDialogOptions: {
            "recreateForm": true,
            errorTextFormat: function (data) {
                return 'Error: ' + data.responseText
            },
            delData: {
                __RequestVerificationToken: jQuery('input[name=__RequestVerificationToken]').val()
            }
        },
        searchDialogOptions: {
            "multipleSearch": true,
            "recreateForm": true,
            "resize": false
        },
        viewRowDialogOptions: {},
        jsonReader: {
            id: "Id"
        },
        sortorder: 'asc',
        height: '85%',
        viewsortcols: [false, 'vertical', true]
    }).navGrid('#IndexGrid_pager', {
        "edit": false,
        "add": true,
        "del": false,
        "search": true,
        "refresh": true,
        "view": false,
        "position": "left",
        "cloneToTop": true
    }, jQuery('#IndexGrid').getGridParam('editDialogOptions'), jQuery('#IndexGrid').getGridParam('addDialogOptions'), jQuery('#IndexGrid').getGridParam('delDialogOptions'), jQuery('#IndexGrid').getGridParam('searchDialogOptions'), jQuery('#IndexGrid').getGridParam('viewRowDialogOptions')).bindKeys();

    function jqGrid_aspnet_loadErrorHandler(xht, st, handler) {
        jQuery(document.body).css('font-size', '100%');
        jQuery(document.body).html(xht.responseText);
    };
    jQuery('#IndexGrid').filterToolbar({
        "searchOnEnter": false
    });
});

/Parkade/IndexDataRequest?jqGridID=IndexGrid请求的响应是:

{"page":1,"total":1,"records":1,"rows":[{"id":"1","cell":["","1","Brooklyn Mall","2","True",""]}],"userdata":{}}

我期望PaymentSchemeId的值为2,因为我的实体有一个FK到表PaymentScheme,我将其中一个记录的PK存储在PaymentSchemeId中因此Id后缀和下拉列表的全部需要。下拉列表是服务器端填充的,正如您在JS中看到的那样:

示例和其他相关代码。我希望PaymentSchemeId的{​​{1}}值为Id。我完全得到了我的期望。下拉列表的值将使用列定义呈现为

int

我非常接近于逐字复制他们的示例,所以请不要为什么GET请求在控制器上忽略了"editoptions": { "value": "3:Pay on Entry;1:Pay on Exit;2:Pay on Foot" 查询字符串:

jqGridID=IndexGrid

在你问之前:

public JsonResult IndexDataRequest()
{
    var gridModel = new ParkadeGridModel();
    JQGridState gridState = gridModel.Grid.GetState();
    Session[SessionKeys.JqGridState] = gridState;
    SetupGrid(gridModel.Grid);
    ViewData["PaymentSchemeId_Data"] = _indexModel.PaymentSchemes;
    return gridModel.Grid.DataBind(_indexModel.Items.AsQueryable());
}

private void SetupGrid(JQGrid grid, string indexMsg = null)
{
    _indexModel = BuildIndexModel();
    grid.DataUrl = Url.Action("IndexDataRequest");
    grid.EditUrl = Url.Action("EditRow");
    grid.Columns.Insert(0, new JQGridColumn
    {
        EditActionIconsColumn = true,
        EditActionIconsSettings = new EditActionIconsSettings
        {
            SaveOnEnterKeyPress = true
        },
        HeaderText = "Edit Actions",
        Width = 50,
        Searchable = false,
        Sortable = false
    });
    SetupParkadeNameDropDown(grid);
    SetupPaymentSchemeDropDown(grid);
    SetupYesNoDropDown(grid);
}

我认为我应该使用'selector'作为此FK列的格式化程序,但我仍在尝试计算如何在我的列定义中使用Trirand的private void SetupPaymentSchemeDropDown(JQGrid parkadesGrid) { JQGridColumn paymentColumn = parkadesGrid.Columns.Find(c => c.DataField == "PaymentSchemeId"); if (parkadesGrid.AjaxCallBackMode == AjaxCallBackMode.RequestData) { var searchList = _indexModel.Items.Select(m => m.PaymentSchemeName).Distinct().Select(l => new SelectListItem { Text = l, Value = l }); paymentColumn.SearchList = searchList.ToList(); paymentColumn.SearchList.Insert(0, new SelectListItem { Text = "All", Value = "" }); var editList = _paymentSchemeRepository.Get().OrderBy(p => p.Name).ToList(); paymentColumn.EditList = editList.Select(l => new SelectListItem { Text = l.Name, Value = l.Id.ToString() }).ToList(); } } 来实现此目的,因为它们没有打扰给我们一个DropDownFormatter选项。

1 个答案:

答案 0 :(得分:1)

我无法完全重现您的问题,但我建议您执行以下两个步骤:

  • 将任何名称(例如name: "act")添加到第一个"Edit Actions"列的定义中。 jqGrid无法正确使用"name": "";
  • 中具有colModel属性的列
  • formatter: "select"添加到列PaymentSchemeId的属性中。

使用formatter: "select"后,用户会看到Pay on Foot而不是不可理解的ID 2。来自服务器的JSON响应仍应包含2,但由于使用Pay on Foot,jqGrid会将值显示为formatter: "select"。在添加或编辑操作期间将发送的数据将包含整数(1,2或3)而不是用户选择的文本(“付款时”,“退出时付款”和“步行付款”)。因此,您只能使用服务器端的整数ID,用户只能看到相应的文本。我认为这就是你想要的。

如果您希望用户只看到整数,那么您应该使用"value": "3:3;1:1;2:2"代替"value": "3:Pay on Entry;1:Pay on Exit;2:Pay on Foot"