【问题标题】:custom formatter for editable cells is not working properly on selecting that cell in jqgrid可编辑单元格的自定义格式化程序在 jqgrid 中选择该单元格时无法正常工作
【发布时间】:2012-03-25 12:24:46
【问题描述】:

我正在使用自定义格式化程序来显示可编辑单元格的单元格数据。如果我选​​择该单元格并选择任何其他单元格,单元格数据将消失并且其他单元格变得不可编辑。如果我使用取消格式化程序也无法正常工作.

我的代码是:

jQuery("#tree").jqGrid({
    url:'json/jsonSamplePots.json',
    datatype: "json",
    mtype:'GET',
    colNames: ["id", "no.", "name"],
    colModel: [
        {name:'id',width: 30, editable:false, align:"right",sortable:false, hidden: true, key: true},
        {name:'no',width:80, editable:false, align:"left", sortable:true, sorttype:"int"},
        {name:'name', width:150, editable:true, sortable:true, sorttype:"text",formatter:resourceFormatter},
],
    rowNum:10,
    rowList:[10,20,30],
    treeGridModel:'adjacency',
    treeGrid: true,
    cellEdit: true,
    ExpandColumn:'name',
    cellsubmit : 'clientArray'});

resourceFormatter=function(cellvalue, options, rowObject)
{
var strResources='';
if( null != rowObject.name )
{
    $.each(rowObject.name,function(i,Assignment)
    {
        if(Assignment)
        {
            for(i=0;i<Assignment.length;i++)
            {
                if(i!=0)
                {
                    strResources=strResources+",";
                }

                strResources=strResources+Assignment[i].employeeName+'['+Assignment[i].assignPercent+']';
            }
        }   

    }); 
}
return strResources;}

我的 JSON 是::

{
"list": [
    {
        "id": 16731,
        "no": "1",
        "name": {
            "resources": [
                {
                    "employeeID": 103,
                    "employeeName": "Gowri",
                    "assignPercent": 100
                },
                {
                    "employeeID": 108,
                    "employeeName": "Paul",
                    "assignPercent": 50
                },
                {
                    "employeeID": 111,
                    "employeeName": "Sarfaraz",
                    "assignPercent": 50.5
                }
            ]
        }
    }
]}

【问题讨论】:

  • JSON 数据似乎有误。 list 属性必须被引用:"list"。如果您使用命名属性resources,则"name":["resources": 部分也是错误的,您只能在对象内部执行此操作,因此它应该类似于"name":{"resources":"name":[{"resources":。您能否发布固定的 JSON 数据,然后我会回答您的主要问题?我建议您验证 JSON 数据here
  • @Oleg:谢谢您的回复。我修改了 Json 数据并发布了正确的 JSON。

标签: jquery jqgrid


【解决方案1】:

在您的情况下,custom formatter 的选择似乎我错了。问题是自定义格式化程序不仅会在初始网格加载期间被调用,而且可以在以后调用。所以在我看来jsonmap 的用法更好:

{name: 'name', width: 250, editable: true,
    jsonmap: function (obj) {
        var prop, name = obj.name, assignment, resource, values = [], i, n;
        for (prop in name) {
            if (name.hasOwnProperty(prop)) {
                assignment = name[prop];
                if ($.isArray(assignment)) {
                    for (i = 0, n = assignment.length; i < n; i++) {
                        resource = assignment[i];
                        values.push(resource.employeeName + '[' +
                            resource.assignPercent + ']');
                    }
                }
            }
        }
        return values.join(', ');
    }}

你也必须定义jsonReader

jsonReader: {
    repeatitems: false,
    root: "list"
}

此外,填充 TreeGrid 特定属性也很重要。您可以在beforeProcessing 回调中填充属性的一部分。在下面的示例中,我在 beforeProcessing 中填写了所有 TreeGrid 特定属性:

beforeProcessing: function (data) {
    var i, list = data.list, n, item;
    if ($.isArray(list)) {
        for (i = 0, n = list.length; i < n; i++) {
            item = list[i];
            if (typeof item.level === "undefined") { item.level = 0; }
            if (typeof item.parent === "undefined") { item.parent = null; }
            if (typeof item.isLeaf === "undefined") { item.isLeaf = false; }
            if (typeof item.expanded === "undefined") { item.expanded = false; }
            if (typeof item.loaded === "undefined") { item.loaded = true; }
        }
    }
}

你找到的修改后的demo here:

更新:我在演示中将本地 cell editing 更改为本地 inline editing,因为单元格编辑不支持使用 TreeGrid。

【讨论】:

  • @lakshmi:不客气!顺便说一句,您每天有权投票支持大约 30 个问题或答案(请参阅 here)。投票的主要目的是帮助其他人找到常见问题的解决方案。搜索引擎密集使用投票。所以我建议你使用权限来帮助stackoverflow的其他访问者。
【解决方案2】:

您使用的 json 有几个错误。下面试试。

{"list":{"id":16731,"no":"1","name":"resources","employeeID":116,"employeeName":"lakshmi","assignPercent":50.5},"employeeID":118,"employeeName":"abc","assignPercent":50.5}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多