2011-12-18 268 views
1

我有这样每行每行JqGrid与RadioButton。jqGrid单选按钮选择单行

... 
{ name: 'select', label: 'select', width: 50, formatter:radio} 

和用于无线电格式化功能:

function radio(value, options, rowObject){ 
    var radioHtml = '<input type="radio" value=' + value + ' name="radioid" />'; 
    return radioHtml; 
} 

当我尝试选择来自的jqGrid i.ee.,单选按钮其仅使用该功能选择的singlerow:

$(function() { 
    $("#<%=editButton.ClientID%>").click(function() { 
     var ids = $("#table").jqGrid('getCol', 'select', true); 
     alert(ids) 
     for (var i = 0; i < ids.length; i++) { 
      //alert(ids[i].id) 
      if (ids[i].id != '') { 
       var idx = $("#table").jqGrid('getCell', ids[i].id, 'select'); 
      } 
      // alert(idx); 
     } 
    }); 
}); 

正在获取网格中的所有行,而不是单个选定的行。

如果格式化程序是复选框但不是收音机,相同的函数效果很好。有什么遗漏吗?

UPDATE:

colModel: [ 
        { name: 'select', label: 'select', width: 50, 
         formatter: function radio(cellValue, option) { 
          return '<input type="radio" name="radio_' + option.gid +  '" />'; 
         } 
        }, 
        { name: 'code', label: 'Branch Code', width: 250 }, 
        { name: 'name', label: 'Branch Name', width: 250 }, 
        { name: 'status', label: 'Group Status', width: 250 }, 
       ], 

功能Click处理程序:

 $("#<%=editButton.ClientID%>").click(function() { 
      alert('M'); 
      var $selRadio = $('input[name=radio_' + $table[0].id + ']:checked'), $tr; 
      alert('I'); 
      if ($selRadio.length > 0) { 
       $tr = $selRadio.closest('tr'); 
       if ($tr.length > 0) { 
        alert("The id of selected radio button is " + $tr.attr('id')); 
       } 
      } else { 
       alert("The radio button is not selected"); 
      } 
     }); 

回答

3

在我看来,从$("#<%=editButton.ClientID%>").click当前的代码太复杂。你可以以更简单的方式做你所需要的。

首先,我建议您使用<radio>按钮的name属性,该属性取决于网格的ID(请参阅the answer)。它可以像下面

formatter: function (cellValue, option) { 
    return '<input type="radio" name="radio_' + option.gid + '" />'; 
} 

你可以选择单选按钮的ID用下面的代码

$("#<%=editButton.ClientID%>").button().click(function() { 
    var $selRadio = $('input[name=radio_' + $grid[0].id + ']:checked'), $tr; 
    if ($selRadio.length > 0) { 
     $tr = $selRadio.closest('tr'); 
     if ($tr.length > 0) { 
      alert("The id of selected radio button is " + $tr.attr('id')); 
     } 
    } else { 
     alert("The radio button is not selected"); 
    } 
}); 

the demo这证明这一点:

enter image description here

+0

您好奥列格,没有成功这条线心不是执行变量$ selRadio = $( '输入[名称= radio_' + $表[0] + .ID ']:检查'),$ TR; “表”是我的网格ID。 – 2011-12-18 16:33:39

+0

@Samuel:你的意思是“这条线没有执行”?点击句柄是不是被调用,或者你在行中得到一个异常,或者'$ selRadio'返回总是带有'length' = 0的对象?你使用了哪个'formatter'?你是否在单选按钮的名称中包含了'option.gid'?你应该把我的演示和青春完全比较。我的演示在你的环境中工作吗?如果它是工作,你应该找到你的代码的区别。 – Oleg 2011-12-18 18:12:16

+0

嗨奥列格,已更新我提出的更改你的问题..对不起,没有描述完整的错误。该代码没有执行后提到的线没有例外,名词定义的值。我的意思是我在线路前后放置了一个简单的警报。 – 2011-12-18 18:19:42

-1

我需要一个更简单的解决方案,所以我想出了这种使用内置多选的方式,而不是添加另一个列到网格。

var gridSelector = $("#myGrid"); 

multiselect : true, 
beforeSelectRow: function(rowid, e) 
{ 
    $(gridSelector).jqGrid('resetSelection'); 
    return (true); 
}, 
beforeRequest : function() { 
    $('input[id=cb_myGrid]', 'div[id=jqgh_myGrid_cb]').remove(); 
}, 
loadComplete : function() { 
    $('input[id^="jqg_myGrid_"]').attr("type", "radio"); 
},