jqGrid中未格式化的行数据

时间:2013-04-17 14:31:08

标签: jquery jqgrid

this question Oleg中描述了如何从jqGrid的行id中检索行数据。

在我的代码中,我想显示有关subGridRowExpanded中一行的更多数据:

standardGrid.subGridRowExpanded = function (subgridId, rowId) {
    var dataFromTheRow = jQuery('#CompanyGrid').jqGrid('getRowData', rowId);
    var html = '<span style="padding-left:80px">' + dataFromTheRow.CompanyName + ' ' + 
        dataFromTheRow.StatusDesc + '</span>';
    $("#" + subgridId).html(html);
}

我的一个主网格列中有一个自定义格式化程序,它返回格式化为链接的数据:

var colModel = [
    { name: 'CompanyID', label: 'Id', width: 30, align: "right" },
    { name: 'CompanyName', label: 'Company Name', search: true, formatter: CompanyLink },
    ...

function CompanyLink(cellValue, options, rowdata, action) {
    return '<a href="CompanyProfile.aspx?CompanyId=' + rowdata.CompanyID + '">' + 
            rowdata.CompanyName + '</a>';
}

当我使用上述答案检索数据时,它会显示在我的子网格格式,锚点和所有内容中。我希望它只返回原始数据。

所以,我尝试在col模型中添加一个unformat。根据{{​​3}}代码,我添加了

function CompanyUnLink(cellValue, options) {
    return $(cellValue).text();
}

并将CompanyName行的colModel更改为

{ name: 'CompanyName', label: 'Company Name', search: true, formatter: CompanyLink, unformat: CompanyUnLink },

但是unformat例程传递了一个未使用锚点格式化的cellValue。

我将unformat更改为

function CompanyUnLink(cellValue, options) {
    // weird, the unformat actually passes in the raw data, and not the decorated one
    return cellValue;
}

它有效。但这对我来说似乎不对。有没有更好或更正确的方法呢?

以下是完整代码的一个小例子:

<table id="datalist"></table> 
<script type="text/javascript">
function CompanyLink(cellValue, options, rowdata, action) {
    return '<a href="CompanyProfile.aspx?CompanyId=' + rowdata.CompanyID + '">' + rowdata.CompanyName + '</a>';
}

function CompanyUnLink(cellValue, options, rowdata, action) {
    // weird, the unformat actually passes in the raw data, and not the decorated one
    return cellValue;
}


$("#datalist").jqGrid({
    datatype: 'local',
    colModel: [
                { name: 'CompanyID', label: 'Id', width: 30, align: "right" },
                { name: 'CompanyName', label: 'Company Name', search: true, formatter: CompanyLink },
            ],
    caption: "SubGrid Format Test",
    subGrid: true,
    subGridRowExpanded: function (subgridId, rowId) {
        var dataFromTheRow = $('#datalist').jqGrid('getRowData', rowId);
        var html = '<span style="padding-left:10px">' + dataFromTheRow.CompanyName + ' ' + dataFromTheRow.Info + '</span>';
        $("#" + subgridId).html(html);
    }
});

var mydata = [
        { CompanyID: 1, CompanyName: "Company1", Info: "Info on Company 1" },
        { CompanyID: 2, CompanyName: "Company2", Info: "Info on Company 2" },
        { CompanyID: 3, CompanyName: "Company3", Info: "Info on Company 3" },
        { CompanyID: 4, CompanyName: "Company4", Info: "Info on Company 4" },
    ];

for (var i = 0; i <= mydata.length; i++)
    $("#datalist").jqGrid('addRowData', i + 1, mydata[i]);

</script>

subGrid行显示用锚点装饰的CompanyName,即它不是原始行数据。

如果更改colModel以调用unformat例程

    colModel: [
                { name: 'CompanyID', label: 'Id', width: 30, align: "right" },
                { name: 'CompanyName', label: 'Company Name', search: true, formatter: CompanyLink, unformat: CompanyUnLink },
            ],

文字不再装饰。但unformat例程的cellValue以未修饰的方式传递,这让我觉得错误。

0 个答案:

没有答案