jqGrid中的动态列

时间:2010-08-05 13:34:02

标签: jqgrid jqgrid-asp.net

我尝试使用动态列与jqGrid一起使用取得了有限的成功。限制在于我可以从控制器控制列名和格式。但是当我这样做时,我无法获取数据。

您是否有一个小样本解决方案,显示两个呼叫的控制器代码。

4 个答案:

答案 0 :(得分:0)

您实际上可以正常绑定列,但您可以使用jquery在运行时显示/隐藏它们。例如,我必须在管理员用户的jqgrid中显示链接列,对于需要隐藏列的普通用户,请按以下方式实现此目的。

            $("#grid").showCol("Link");
            $("#grid").hideCol("Link");

           $("#grid").trigger("reloadGrid");

答案 1 :(得分:0)

JQGrid(action, caption, 920, 400, loadtext);

function columnsData(Data) {

var str = "[";
for (var i = 0; i < Data.length; i++) {
    str = str + "{name:'" + Data[i] + "', index:'" + Data[i] + "', editable: true}";
    if (i != Data.length - 1) {
        str = str + ",";
    }
}
str = str + "]";
return str;
}

function JQGrid(action, caption, width, height, loadtext) {

var grid = $("#tblGrid");
var lastsel;
var editurl = '/PayInvoice/GridSave';
$.ajax({
    url: action,
    dataType: "json",
    mtype: 'POST',
    beforeSend: function () {
        $("#dvloading").show();
    },
    success: function (result) {
        if (result) {
            if (!result.Error) {
                var colData = columnsData(result.column);
                colData = eval('{' + colData + '}');
                grid.jqGrid('GridUnload');
                grid.jqGrid({
                    url: action,
                    datatype: 'json',
                    mtype: 'POST',
                    colModel: colData,
                    colNames: result.column,
                    // multiselect: true,
                    width: width,
                    height: height,
                    rowNum: 20,
                    rowList: [20, 40, 60],
                    loadtext: loadtext,
                    pager: '#tblGridpager',
                    sortorder: "asc",
                    viewrecords: true,
                    gridview: true,
                    altRows: true,
                    cellEdit: true,
                    cellsubmit: "remote",
                    cellurl: '/PayInvoice/GridSavecell',
                    beforeSubmitCell: function (id, cellname, value, iRow, iCol) {
                        objedit(id, cellname, value);
                        return { id: id, cellname: cellname, value: value, iRow: iRow, iCol: iCol };
                    },
                    afterSaveCell: function (id, cellname, value, iRow, iCol) {
                        objedit(id, cellname, value);
                        return { id: id, cellname: cellname, value: value, iRow: iRow, iCol: iCol };
                    },
                    caption: caption
                });
            }
        }
    },
    error: function (xhr, ajaxOptions, thrownError) {
        if (xhr && thrownError) {
            alert('Status: ' + xhr.status + ' Error: ' + thrownError);
        }
    }, complete: function () {
        $("#dvloading").hide();
    }
});

}

function objedit(id, cellname, value) {

var flag = 0;
for (var i = 0; i < index; i++) {
    if (obj[i][0] == id && obj[i][1] == cellname) {
        obj[i] = [id, cellname, value]
        flag++;
    }
}
if (flag == 0) {
    obj[index] = [id, cellname, value];
    index++;
}

}

答案 2 :(得分:0)

JQGrid1.Columns.FromDataField(ColumnName).Visible = false;
JQGrid1.Columns.FromDataField(ColumnName).HeaderText = "Sample";

答案 3 :(得分:0)

我们走了;

 $("#datagrid").jqGrid({
        //url: "user.json",
        //datatype: "json",
        datatype: "local",
        data: dataArray,
        colNames:getColNames(dataArray[0]),
        colModel:getColModels(dataArray[0]),
        rowNum:100,
        loadonce: true,
        pager: '#navGrid',
        sortname: 'SongId',
        sortorder: "asc",
        height: "auto", //210,
        width:"auto",
        viewrecords: true,
        caption:"JQ GRID"
    });

    function getColNames(data) {
        var keys = [];
        for(var key in data) {
            if (data.hasOwnProperty(key)) {
                keys.push(key);
            }
        }

        return keys;
    }

    function  getColModels(data) {
        var colNames= getColNames(data);
        var colModelsArray = [];
        for (var i = 0; i < colNames.length; i++) {
            var str;
            if (i === 0) {
                str = {
                    name: colNames[i],
                    index:colNames[i],
                    key:true,
                    editable:true
                };
            } else {
                str = {
                    name: colNames[i],
                    index:colNames[i],
                    editable:true
                };
            }
            colModelsArray.push(str);
        }

        return colModelsArray;
    }