2016-08-30 5 views
0

私は、データがjquery Datatableに表示されるASP.NET Webページの開発に取り組んでいます。私はデータを表示することができますが、私が直面している問題は、情報 "表示のエントリ"が間違った値を表示しています。jquery-datatable:間違っている "<total>のエントリの<range>を表示"メッセージが表示されます

フィドラー:https://jsfiddle.net/8f63kmeo/9/

HTML:

<table id="CustomFilterOnTop" class="display nowrap" width="100%"></table> 

JS

var Report4Component = (function() { 
    function Report4Component() { 
     //contorls 
     this.customFilterOnTopControl = "CustomFilterOnTop"; //table id 
     //data table object 
     this.customFilterOnTopGrid = null; 
     //variables 
     this.result = null; 
    } 
    Report4Component.prototype.ShowGrid = function() { 
     var instance = this; 
     //add footer 
     $('#' + instance.customFilterOnTopControl) 
      .append('<tfoot><tr><th colspan="2" class="total-text">Total</th><th class="total-value"></th></tr></tfoot>'); 
     //create the datatable object 
     instance.customFilterOnTopGrid = $('#' + instance.customFilterOnTopControl).DataTable({ 
      columns: [ 
       { data: "Description", title: "Desc" }, 
       { data: "Status", title: "Status" }, 
       { data: "Count", title: "Count" } 
      ], 
      "paging": true, 
      scrollCollapse: true, 
      "scrollX": true, 
      scrollY: "300px", 
      deferRender: true, 
      scroller: true, 
      dom: '<"top"Bf<"clear">>rt <"bottom"<"Notes">i<"clear">>', 
      buttons: [ 
       { 
        text: 'Load All', 
        action: function (e, dt, node, config) { 
         instance.ShowData(10000); 
        } 
       } 
      ], 
      initComplete: function (settings) { 
       var api = this.api(settings); 
       //now, add a second row in header which will hold controls for filtering. 
       $(api.table().header()).append('<tr role="row" id="FilterRow">' + 
        '<th>Desc</th>' + 
        '<th>Status</th>' + 
        '<th>Count</th>' + 
        '</tr>'); 
       //add input controls for filtering 
       $('#FilterRow th', api.table().header()).each(function() { 
        var title = $('#' + instance.customFilterOnTopControl + ' thead th').eq($(this).index()).text(); 
        $(this).html('<input type="text" onclick="return false" placeholder="Search ' + title + '" class="form-control" />'); 
       }); 
       //todo: refactor this code. this is for displaying the scrollbar below the tfoot instead of tbody 
       //when multiple tables are present, use tablename.find to get the specific class object 
       //this code is not tested with other options 
       $('.dataTables_scrollBody').css({ 
        'overflow-x': 'hidden', 
        'border': '0' 
       }); 
       $('.dataTables_scrollFoot').css('overflow', 'auto'); 
       $('.dataTables_scrollFoot').on('scroll', function() { 
        $('.dataTables_scrollBody').scrollLeft($(this).scrollLeft()); 
       }); 
      }, 
      footerCallback: function (tfoot, data, start, end, display) { 
       var api = this.api(); 
       if (instance.result == null || instance.result.Total == undefined) { 
        return; 
       } 
       $(api.column(2).footer()).html(instance.result.Total); 
      } 
     }); 
     $("div.Notes").html('<div class="alert alert-warning">This is a notes section part of the table dom.</div>'); 
    }; 
    Report4Component.prototype.BindEvents = function() { 
     var instance = this; 
     $("#FilterRow th input").on('keyup change', function() { 
      instance.customFilterOnTopGrid 
       .column($(this).parent().index() + ':visible') 
       .search("^" + $(this).val(), true, false, true) //uses regular expression and checks only for starts with 
       .draw(); 
     }); 
    }; 
    Report4Component.prototype.ShowData = function (limit) { 
     if (limit === void 0) { limit = 100; } 
     var instance = this; 
     instance.customFilterOnTopGrid.clear(); //latest api function 
     instance.result = instance.GetData(limit); 
     instance.customFilterOnTopGrid.rows.add(instance.result.RecordList); 
     instance.customFilterOnTopGrid.draw(); 
    }; 
    Report4Component.prototype.GetData = function (limit) { 
     //structure of the response from controller method 
     var resultObj = {}; 
     resultObj.Total = 0; 
     resultObj.RecordList = []; 
     for (var i = 1; i <= limit; i++) { 
      resultObj.Total += i; 
      var record = {}; 
      record.Description = "This is a test description of record " + i; 
      record.Status = ["A", "B", "C", "D"][Math.floor(Math.random() * 4)] + 'name text ' + i; 
      record.Count = i; 
      resultObj.RecordList.push(record); 
     } 
     return resultObj; 
    }; 
    return Report4Component; 
}()); 
$(function() { 
    var report4Component = new Report4Component(); 
    report4Component.ShowGrid(); 
    report4Component.BindEvents(); 
    report4Component.ShowData(); 
}); 
function StopPropagation(evt) { 
    if (evt.stopPropagation !== undefined) { 
     evt.stopPropagation(); 
    } 
    else { 
     evt.cancelBubble = true; 
    } 
} 

問題:

以下のスナップショットでは、あなたがグリッドに表示さ8つのレコードがあることがわかりますけど、カウントは100の1から1として示していることは、1 100

enter image description here

の8にする必要があります

観察:あなたはページのサイズを変更する場合は

、カウントが正しく表示されているようです。私は、描画ごとにウィンドウのサイズ変更イベントをトリガーしたくありません。この問題を処理するAPIはありますか?

期待:

私はこの問題を修正する必要がありますどのように?どんな提案も感謝しています。

+0

なぜページネーション情報が必要なのですか?ページングではなくスクロールを使用しているようです。私の最初の勘違いは、スクロールのために間違った情報を取得することです。表示行の数に基づいて動的に更新することはありません。 –

+0

「表示中...」というメッセージは、ユーザーがスクロールしてデータを表示しているときのユーザーの現在の位置を示します。この例を参照してください:https://datatables.net/extensions/scroller/examples/initialisation/simple.html –

+1

ああ、私はスクロールがそれを行うことができたことを実現しませんでした。興味深いのは、私が実際の例をフィドルでサイズ変更すると、数字が正しいことです。 –

答えて

1

JSFiddleでちょっとした(heh)をして、私はこの問題の理由を検出したと思います。問題の

概要:

スクロールは、すべての行(rows.add()を使用して代わりの行を持つテーブルを初期化する)の添加は、テーブルの物理的寸法が増加していることに気づかないとなるいくつかの問題があります、最初の0行テーブルが現在のサイズであると考えています。これにより、表示行の数が誤って計算されます。 draw()は、テーブルだけを再作成しないため、正しいデータではないため、役に立たないでしょう。テーブルの内部寸法がスクロール・オーバーラーに正しく与えられていません。これは、テーブルを完全に破棄して再作成せずにデータをロードする方法では修正できません。

回避策策:あなたが作成し、データをロードする前に、あなたのテーブル/イベントをロードするので

は、何とかスクロールは、テーブルのY寸法は、0と(表が作成された変更されたことを認識していません行数は1以上の余裕がないと考えています)。私は本当にあなたがスクロールを更新するために何をすべきかはっきりしていませんが、回避策の解決策が見つかりました。この回避策が気に入らない場合は、テーブルの構築中にデータをロードする可能性を検討する必要があります。どのように多くのあなたがよある9行の余地があることを信じるように

<table id="CustomFilterOnTop" class="display nowrap" width="100%"> 
<tr><td>asdf</td><td>asdf</td><td>asdf</td></tr> 
<tr><td>asdf</td><td>asdf</td><td>asdf</td></tr> 
<tr><td>asdf</td><td>asdf</td><td>asdf</td></tr> 
<tr><td>asdf</td><td>asdf</td><td>asdf</td></tr> 
<tr><td>asdf</td><td>asdf</td><td>asdf</td></tr> 
<tr><td>asdf</td><td>asdf</td><td>asdf</td></tr> 
<tr><td>asdf</td><td>asdf</td><td>asdf</td></tr> 
<tr><td>asdf</td><td>asdf</td><td>asdf</td></tr> 
<tr><td>asdf</td><td>asdf</td><td>asdf</td></tr> 
</table> 

このトリックスクロール、:私がやったすべてが追加ダミー行して、HTMLテーブルを変更され

を参照してくださいthis fiddle最初に持っていた。基本的に初期テーブルを300pxの定義に埋め込んでいます。

テーブルが全く同じように動作するように見えますが、正しいページング情報が表示されるようになりました。これらの行は、テーブルを初期化してロードするときに破棄されるため、スクロールバーが多くの行を予期していることがわかるように、フィラーにすぎません。

これは非常に「ハックイン」ですが、少なくとも次はどこを見ていくべきか、一時的な回避策については十分に理解していればうれしいです。

もっと良い解決策は、初期化時にデータをテーブルにロードすることです。それは、それを行う標準的な方法です。

+0

しかし、私はダミー行を追加したくありません。私はこの問題を解決するのに役立つdatatableのいくつかのAPI /メソッドがあるはずだと推測しています... –

+0

問題はdatatablesがあなたに何か間違って修正することができます。テーブルへのデータの挿入方法が非標準(必ずしも間違っているとは限りません)であるため、スクロールが正しく動作しなくなります。私はテーブルを再描画するのは驚いていますが、テーブルを修正することはできませんが、データをテーブルにロードする方法やダミーの行を変更したり、データの後に何らかの形でテーブルを再ロードしたりしないと、ロードされる。 –

+1

@VimalanJayaGanesh少なくとも問題の診断に役立つことを願って要約を追加しました。私はDataTablesフォーラムでこのことについて投稿することをお勧めします。これは、WorkAoundがあっても、DataTablesコードの(マイナーな)バグのようです。 –

関連する問題