2011-08-26 96 views
6

我有一个在我的JqGrid中从数据库加载的复选框列,所以它被检查或不加载时加载。JQgrid复选框onclick更新数据库

我想要的是:如果复选框正在被用户选中或取消选中,我想更新数据库中的相同。我不希望用户按下输入或任何东西。只有一次点击并向DB发送动作

name:'Aktiv',index:'Aktiv',width:100,edittype:'checkbox',align:'center',formatter:“checkbox”,editable:true, formatoptions:{禁用:假}

回答

16

您可以设置一个click事件处理程序的loadComplete内:

loadComplete: function() { 
    var iCol = getColumnIndexByName ($(this), 'Aktiv'), rows = this.rows, i, 
     c = rows.length; 

    for (i = 1; i < c; i += 1) { 
     $(rows[i].cells[iCol]).click(function (e) { 
      var id = $(e.target).closest('tr')[0].id, 
       isChecked = $(e.target).is(':checked'); 
      alert('clicked on the checkbox in the row with id=' + id + 
       '\nNow the checkbox is ' + 
       (isChecked? 'checked': 'not checked')); 
     }); 
    } 
} 

其中

var getColumnIndexByName = function(grid, columnName) { 
    var cm = grid.jqGrid('getGridParam', 'colModel'), i, l; 
    for (i = 1, l = cm.length; i < l; i += 1) { 
     if (cm[i].name === columnName) { 
      return i; // return the index 
     } 
    } 
    return -1; 
}; 

取而代之的alert应该使用jQuery.ajax向服务器发送有关更新复选框状态的信息。

您可以看到演示here

+0

完美的工作,现在我需要弄清楚如何发送动作进一步到我的控制器,它将发送到数据库进一步。哦,我认为我现在得到它 – Timsen

+0

谢谢!谢谢! – jose

+0

@ jose:不客气! – Oleg

1

loadComplete:function()中的一个小错误。 在演示中,您可以发现,即使在复选框被选中后,如果您在该单元格的复选框外单击,该值将从“true”更改为“false”。

为了避免这种情况,只需通过执行以下操作,仅将焦点对准复选框。

for (i = 1; i < c; i += 1) { 
    $(('input[type="checkbox"]'),rows[i].cells[iCol]).click(function (e) { 
     var id = $(e.target).closest('tr')[0].id, 
      isChecked = $(e.target).is(':checked'); 
     alert('clicked on the checkbox in the row with id=' + id + 
       '\nNow the checkbox is ' + 
       (isChecked? 'checked': 'not checked')); 
    }); 
} 

,并感谢您的回答:-)(@Oleg)帮我当然..的lot..in时间;)

+0

这似乎不起作用。使用此代码时,点击事件不会触发。请指教。 – NewHistoricForm

0

要改变基于复选框

的点击其他列的值
var weightedAvgPriceIndex = getColumnIndexByName($(this), 'WeightedAveragePrice'), 
    rows = this.rows, 
    i, 
    c = rows.length; 

for (i = 1; i < c; i += 1) { 
    $(('input[type="checkbox"]'),rows[i].cells[iCol]).click(function (e) { 
     var id = $(e.target).closest('tr')[0].id; 
     isChecked = $(e.target).is(':checked'); 
     var x = $('#' + id + ' td:eq(' + weightedAvgPriceIndex + ')').text(); 
     $('#' + id + ' td:eq(' + weightedAvgPriceIndex + ')').text(Math.abs(x) + 10); 
    }); 
}