【问题标题】:JQGrid set value in cell based on conditionJQGrid根据条件在单元格中设置值
【发布时间】:2013-09-10 09:56:44
【问题描述】:

我需要根据条件设置JQgrid单元格值,假设变量值为1则需要设置自行车,如果为2则需要设置为汽车,依此类推。

谁能解释一下如何做到这一点?

【问题讨论】:

    标签: jqgrid jqgrid-formatter jqgrid-inlinenav mvcjqgrid


    【解决方案1】:

    您需要使用formatter 来完成工作。将其包含在以下行中并开始开发功能

    例如。

    {name:'vehicle', index:'vehicle', width:100, align:"center", editable: true, formatter:formatvehicle}
    

    方法开发:

    function formatvehicle (cellvalue, options, rowObject)
    
        {
           if (cellvalue == 1)
            return "Bike";
           else if (cellvalue == 2)
            return "Car";
           else {
           }
    
        }
    

    或者尝试使用开关

    function formatvehicle (cellvalue, options, rowObject) {
    
     switch (cellvalue) {
            case 1:
              return "Bike";
                break;
            case 2:
             return "Car";
                break;
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用格式化程序实现此目的

      <script>
      jQuery("#grid_id").jqGrid({
      ...
         colModel: [ 
            ... 
            {name:'price', index:'price', width:60, align:"center", editable: true, formatter:vehicalFmatter},
            ...
         ]
      ...
      });
      
      function vehicalFmatter (cellvalue, options, rowObject)
      {
         if (cellvalue == 1)
          return "Bike";
         else if (cellvalue == 2)
          return "Car";
      }
      </script>
      

      参考:http://www.trirand.com/jqgridwiki/doku.php?id=wiki:custom_formatter

      【讨论】: