2011-05-12 2 views
17

以外のページに登録しない]をクリックし、行と次のように行をクリックしたときに、クリックハンドラを設定している:のjQueryのDataTable - 私は<a href="http://datatables.net/index">DataTables jQuery Plugin</a>を使用している最初の

$('#dt tbody tr').click(function() { 
     alert('e'); 
}); 

これは、DataTableの結果の最初のページのために完璧に動作します。

しかし、結果の別のページに移動すると、クリックハンドラは登録されなくなります。

私の前提は、DataTablesコードがclickイベントの宣言を自分のハンドラに停止させていることですが、これは最初のページの後でのみ発生するため、珍しいようです。

などは、誰もが持っていたよう:

  1. が発生しました(理想的に解決する)この問題
  2. は、イベントが
を停止される理由隔離するためのjQuery/JSイベントプロパゲーションを追跡するための良い方法を見つけました

乾杯

+0

私の推測が間違っていました。もう1つの前提条件: 'ajaxComplete'のバインディングはすべての行にバインドされ、DataTableは賢明であり、必要になるまでブラウザー内のすべての行をレンダリングしないので無効です。したがって、Konの 'live()'の答えは正しいです。 –

+0

Chris Everittが提供する答えを見てください。それは組み込みのDataTables関数を使用し、非推奨のjquery関数を使用しません。 – Nullius

答えて

11

この問題は、1ページのアプリケーションで発生しました。ライブメソッドは私のために働いたポストバックの後を除いて。私のテーブルにはajaxを使ってテーブルが作成され、ユーザーはそれを破壊して再作成する可能性がありました。私はDataTableのを使用し、それを修正するには

$:。 「http://datatables.net/api#$」

ここでは、例のDataTableが隠し行機能に与える使用して私の修正です。

$(document).ready(function() { 
    /* 
    * Insert a 'details' column to the table 
    */ 
    var nCloneTh = document.createElement('th'); 
    var nCloneTd = document.createElement('td'); 
    nCloneTd.innerHTML = '<img src="../examples_support/details_open.png">'; 
    nCloneTd.className = "center"; 

    /* CHANGE: Remove all the expand control elements we may have added earlier 
    * or else you'll add a new column for every postback 
    */ 
    $('.expand-control').remove(); 

    /* 
    * CHANGE: Add the expand-control class to these elements, 
    * so we can remove them after a postback 
    */ 
    $(nCloneTh).addClass('expand-control'); 
    $(nCloneTd).addClass('expand-control'); 

    $('#example thead tr').each(function() { 
     this.insertBefore(nCloneTh, this.childNodes[0]); 
    }); 

    $('#example tbody tr').each(function() { 
     this.insertBefore( nCloneTd.cloneNode(true), this.childNodes[0]); 
    }); 

    /* 
    * Initialse DataTables, with no sorting on the 'details' column 
    */ 
    var oTable = $('#example').dataTable({ 
     "aoColumnDefs": [ 
      { "bSortable": false, "aTargets": [ 0 ] } 
     ], 
     "aaSorting": [[1, 'asc']] 
    }); 

    /* Add event listener for opening and closing details 
    * Note that the indicator for showing 
    * which row is open is not controlled by DataTables, 
    * rather it is done here 
    */ 

    /* CHANGE: Here I use jQuery.dataTable.$ instead of 
    * jQuery('#example tbody td img'), 
    * this is what preserves the event handler on the 2nd (etc) 
    * pages after a postback 
    * Note the use of on instead of live, recommended over live as of 1.7 
    */ 
    oTable.$('tr').find('img').on('click', function() { 
     var nTr = $(this).parents('tr')[0]; 
     if (oTable.fnIsOpen(nTr)) 
     { 
      /* This row is already open - close it */ 
      this.src = "../examples_support/details_open.png"; 
      oTable.fnClose(nTr); 
     } 
     else 
     { 
      /* Open this row */ 
      this.src = "../examples_support/details_close.png"; 
      oTable.fnOpen(nTr, fnFormatDetails(oTable, nTr), 'details'); 
     } 
    }); 
}); 
+0

これは受け入れられた答えである必要があります。jquery live()のような廃止された関数を使用することは決して良い考えではありません。 +1 – Nullius

+0

更新ありがとう。 –

+0

これはまた、このブログの投稿で提案されています:http://ricardocovo.com/2010/09/02/asp-mvc-delete-confirmation-with-ajax-jquery-ui-dialog/(削除確認ダイアログのテクニックをjQueryデータ型); ...削除リンクは最初のページの後でページの作業を停止しました。 $( 'delete-link')の代わりに$( 'delete-link')を使う代わりに、上記のアイデアを使って問題を解決しました: 'click'、function(){...};その時点で削除リンクが再び動作するようになりました。 –

12

私は、イベントハンドラバインディングが最初に読み込まれた行にのみ適用されていると推測しています。しかし、行のコレクションがマークアップで再レンダリングされると、イベントハンドラはもう消えません。

jQueryのlive()機能を確認してください。重要なのは、イベントハンドラはセレクタ基準を満たすすべての要素に「現在および将来」バインドされていることです。

+0

もちろん、歓声。奇妙なことに、最初のページ(すべての行が動的にロードされていました)にもかかわらず、私はこれが答えだと思います。まもなく私は確認して受け入れます。 –

+2

click()ハンドラを定義する前にローをロードすると意味があります。 – Kon

+0

もちろん、私は 'ajaxComplete'ハンドラに' click'を置いていました.DataTablesがすべての行をロードしていて、それを隠していると思っていました。ただし、これはすべてのDataTableレンダリング(残りはメモリ内に保持される)なので、最初のページにのみ添付されます。 live( 'click'、function() 'などはそれを修正します(実際には" now and future "ビットのために' ajaxComplete'の外で宣言できます) –

5

すべてのDataTables行のボタンで同じ問題が発生しましたが、クリックイベントは結果の最初のページの後のボタンでは機能しませんでした。崑は、正しい分析(ありがとう崑)を与えたが、例のコードをお探しの方のために、ここで私のために働いたものです:助け

$('.myButton').live('click', function() { 
    var id = $(this).closest("tr").attr("id"); 
    var string = 'div_id=' + id; 
    alert(string); 
     // string sent to processing script here 
}); 

希望を!

+0

ありがとうございましたコードは答えが常に役に立ちます – bladefist

+0

感謝します。それはうまくいきませんでした – rashid

3

liveは廃止予定ですので、「.on」を使用することをおすすめします。

これはあなたの問題を解決する必要があります:それは非常に効率的ではないよう

$(document).on('click', '.myButton', function() { 
    var id = $(this).closest("tr").attr("id"); 
    var string = 'div_id=' + id; 
    alert(string); 
     // string sent to processing script here 
}); 

あなたは、いくつかの親要素で文書を交換することができます。おそらくあなたのテーブルを含むdivを使ってみてください。

1

私の答えは@Chris Everittの回答と似ていますが、若干の違いがあります。それを見たい人のためだけに..ここに行く..テーブル内のすべてのimg(DOMのattrの)イベントを登録

var oTable = $('#masterTable').dataTable({ 

       "aLengthMenu": [[5,10, 25, 50, 100 , -1], [5,10, 25, 50, 100, "All"]], 
       "iDisplayLength" : 10, 
       "aoColumnDefs": [ 
          {"sWidth": "25%", "aTargets": [ 0 ] }, 
          {"sWidth": "10%", "aTargets": [ 1 ] }, 
          {"sWidth": "10%", "aTargets": [ 2 ] }, 
          {"sWidth": "10%", "aTargets": [ 3 ] }, 
          {"sWidth": "10%", "aTargets": [ 4 ] }, 
          {"sWidth": "10%", "aTargets": [ 5 ] }, 
          {"sWidth": "15%", "aTargets": [ 6 ] }, 

          {"sClass": "align-left" , "aTargets": [ 0,1,4, 2,3,5,6] }    
         ], 

      "aoColumns": [ 
       { "bSortable": true }, 
       null, null, null,null, null, 
       { "bSortable": true } 
      ] 

      }); 

-

oTable.$('td').each(function() { 

        $(this).on('click','img', function() { 
         var nTr = $(this).parents('tr')[0]; 
         if (oTable.fnIsOpen(nTr)) 
         { 
          /* This row is already open - close it */ 
          this.src = "${pageContext.request.contextPath}/theme/v_1_0/app-images/details_open.png"; 
          oTable.fnClose(nTr); 
         } 
         else 
         { 
          /* Open this row */ 
          this.src = "${pageContext.request.contextPath}/theme/v_1_0/app-images/details_close.png"; 



          var html = '<div> Placeholder here.. </div>'; 

          oTable.fnOpen(nTr, html, 'details'); 

          } 
        });  
関連する問題

 関連する問題