2011-12-18 270 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"); 
},