代码之家  ›  专栏  ›  技术社区  ›  evildead

jqGrid动态选择字段

  •  0
  • evildead  · 技术社区  · 15 年前

    有没有办法动态修改colModel中status字段的值? 假设我们有一个col模型,其字段如下:

    ... field ... name: "state",type: "select",
    editoptions: {value: "0:state0;1:state1;2:state2;3:state3;4:state4"}
    

    所以我得到了一个有这个值的状态选择字段。但我需要动态地决定哪些selectfields是可能的。 如果当前行的状态为state0,则只应显示state0和state1。 如果状态为state1,则显示应为state0、state1和state2

    我是否可以用格式化程序来解决这个问题,或者是否有其他方法来做这样的事情。

    更困难的是,假设在我的应用程序中显示的状态通常依赖于登录的用户。 在某种程度上,用户只能看到state0、state2和state4。 这可能会变得更加复杂,因为当前用户不允许在state3和state4之间进行转换。

    然而,状态本身也是动态的。为我的应用程序中的一个对象动态生成javascript(它表示一个通用的state类)并使用这个对象在格式化程序中生成我需要的输出,会有帮助吗? 所以我可以将逻辑封装在这个对象中,如何生成输出,另外我只得到用户能够看到的状态。

    应该让我一举两得。

    具体问题的解决方案,thx到oleg:

        editoptions : {
    
        value : function(){
            //a function can be called here:
            currentRow=$("#order_items").getGridParam('selrow');
            currentState=$("#order_items").getCell(currentRow,"state");
                        nastyGeneratedThings=function(){
                                         ... do some nasty things with currentState
                                         ... and generate what you want
                                         }
                        return nastyGeneratedThings
        }
    

    我遇到了一些麻烦,因为函数只调用了一次。所以我必须在Navgrid中设置recreateForm选项。

    navGrid("#pager", {
                edit : true,
                add : true,
                del : true
            }, {
                height : 500,
                width : 500,
                // recreate the form every time when edit button is clicked.
                // Default is false.
                recreateForm : true
            }
            });
    

    之后,每次单击“编辑”时,我的函数都会激发。 希望这能帮上忙。

    1 回复  |  直到 15 年前
        1
  •  1
  •   Oleg    15 年前

    这个 value editoptions 不仅可以是字符串,还可以是函数。函数可以返回字符串“0:state0;1:状态1;2:状态2;3:状态3;4:state4“或类似 {"0":"state0", "1":"state1", "2":"state2", "3":"state3", "4":"state4"} . 顺便说一下,最后一种格式有一些优点:例如可以使用“:”、“;”值的内部。

    该函数没有参数,但您可以使用类似coll的 getGridParam('selrow') 方法和方法 getCell(rowid,iCol) getCell(rowid,"state") “state”列的当前值。

    价值 上的属性 http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editoptions ). 也许这能解决你的问题?

    推荐文章