监听表格行数变化 [英] listening for table rows count change

查看:657
本文介绍了监听表格行数变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是html表格。我的要求是,我想听取行数的变化。有没有办法实现这一点?



注意:我没有在这里添加/删除行函数来实现。



此外,我已经尝试过了,

  $(#myTable> tbody> tr)。 (length,function(){
alert(row count changed ..)
});

但是,上面的代码对我不起作用。你们中的任何人都知道该怎么做?

解决方案

目前唯一的跨浏览器方式是使用setInterval和可能的自定义事件。 b
$ b

  setInterval(function(){
var $ t = $(#myTable),
rowCount = $ t。 data(rowCount),
rowLength = $ t.find(tbody)。children()。length;
if(rowCount& rowCount!== rowLength){
$ t.trigger(rowcountchanged)。data(rowCount,rowLength);
}
else if(!rowCount){
$ t.data(rowCount,rowLength );
}

},50);
$(#myTable)on(rowcountchanged,function(){
alert($ (this).data(rowCount));
});

我建议您在更改行数时手动触发事件,而不是在可能的情况下使用间隔。
$ b

更新:

使用jqGrid 刷新事件。

  var rowCount = $(#gridid> tbody> tr)。 
jQuery(#gridid)。jqGrid({
...
refresh:function(){
var currCount = $(#gridid> tbody> tr) .length;
if(currCount!== rowCount){
alert(Number of rows changed!);
rowCount = currCount;
}
},
...
});

如果您在点击时手动删除任何行,您将需要在那里运行您的代码,除非它也正在重新加载网格。


I was using the html table. My requirement is, i want to listen for the changes of rows count. Is there anyway to achieve this?

Note: I don't have the adding/deleting a row functions here to achieve.

Moreover that, i have tried this,

$("#myTable>tbody>tr").watch("length", function () {
    alert("row count changed..")
});

But, the above code doesn't work for me. Could any of you know how to do this?

Thanks in advance,

-Raja.

解决方案

The only cross-browser way to do this at the moment would be with a setInterval and possibly a custom event.

setInterval(function() {
    var $t = $("#myTable"),
        rowCount = $t.data("rowCount"),
        rowLength = $t.find("tbody").children().length;
    if (rowCount && rowCount !== rowLength) {
        $t.trigger("rowcountchanged").data("rowCount", rowLength);
    }
    else if (!rowCount) {
        $t.data("rowCount", rowLength);
    }

}, 50);​
$("#myTable").on("rowcountchanged",function(){
    alert($(this).data("rowCount"));
});

I would suggest triggering the event manually when you change the number of rows rather than using an interval if possible.

UPDATE:
Use the jqGrid refresh event.

var rowCount = $("#gridid>tbody>tr").length;
jQuery("#gridid").jqGrid({
...
   refresh: function(){ 
      var currCount = $("#gridid>tbody>tr").length;
      if (currCount !== rowCount) {
          alert("Number of rows changed!");
          rowCount = currCount;
      }
   },
...
});

If you are manually deleting any rows on click, you will need to run your code there too unless it is also reloading the grid.

这篇关于监听表格行数变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆