简体   繁体   English

jQuery DataTable中数据未更新

[英]data is not updating in jquery datatable

Hi im using jquery datatable( https://datatables.net/ ). 您好,我使用jquery datatable( https://datatables.net/ )。 im adding some values in database and refreshing the datatable but datatable is showing old values. 我在数据库中添加了一些值并刷新了数据表,但数据表显示了旧值。 once i refresh the browser then it is showing updated value. 一旦刷新浏览器,它就会显示更新的值。 can anyone help on this issue? 任何人都可以在这个问题上提供帮助吗?

     function display_comments(){
       $.ajax({
        url: getcomments_URL,
        method: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function(data, status, xhr) {

                $.each(circuitPremisesJson, function(index, item) {

                    var option = "<tr><td>" + item.comments + "</td><td>" + item.comment_TYPE + "</td><td>" + item.lname + "</td><td>" + item.action_PERFORMED + "</td><td>" + item.entry_TIME + "</td></tr>";

                    $('#div_comment_list tbody').append(option);
                });
                $('#div_comment_list').DataTable({destroy: true});                  

        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(ajaxOptions);
        }
    });
    }

    $("#add_comments").click(function(){
    // here i have ajax code for add comments in database


    display_comments();  / refreshing the datatable 

    });

Errors and changes: 错误和变更:

  • You have one unknown here: getcomments_URL I guess that is some global variable somewhere? 您在这里有一个未知数: getcomments_URL我想这是某个地方的全局变量?
  • circuitPremisesJson was not in your code, I assume it is the data returned, this replaced that with data circuitPremisesJson不在您的代码中,我假设它是返回的数据,这被替换为data
  • Invalid comment added second / display_comments(); / refreshing the datatable 无效的注释添加第二/ display_comments(); / refreshing the datatable display_comments(); / refreshing the datatable in that line display_comments(); / refreshing the datatable行中的数据表
  • you can use the success and error but I find the .done and .fail looks cleaner to me. 您可以使用successerror但是我发现.done.fail对我来说看起来更干净。
  • multiple $('#div_comment_list') so I cached that selector in c 多个$('#div_comment_list')所以我在c缓存了该选择器
  • I did not like option as a name for a table row string so I changed it 我不喜欢将option作为表行字符串的名称,所以我将其更改
  • var newTableRow =""; creation of variables in a loop is not optimal so I added this line 在循环中创建变量不是最佳选择,所以我添加了这一行

Code

function display_comments() {
  $.ajax({
    url: getcomments_URL,
    method: "GET",
    contentType: "application/json; charset=utf-8",
    dataType: "json"
  }).done(function(data, status, xhr) {
    var c = $('#div_comment_list');
    var newTableRow ="";
    $.each(data, function(index, item) {
      newTableRow = "<tr><td>" + item.comments + "</td><td>" + item.comment_TYPE + "</td><td>" + item.lname + "</td><td>" + item.action_PERFORMED + "</td><td>" + item.entry_TIME + "</td></tr>";
      c.find('tbody').append(newTableRow);
    });
    c.DataTable({
      destroy: true
    });
  }).fail(function(jqXHR, textStatus, errorThrown) {
    alert(textStatus);
  });
}

$("#add_comments").click(function() {
  // here i have ajax code for add comments in database
  display_comments(); // refreshing the datatable
});

EDIT: Update Datatable portion 编辑:更新数据表部分

Just to concentrate on the Datatable update portion, I created a new fiddle. 为了专注于数据表更新部分,我创建了一个新提琴。 That whole thing is here: 整个事情都在这里:

Here are the key portions of that. 这是关键部分。 I like to use namespaces for my Javascript when it gets complex so let's do that: 我喜欢在Javascript变得复杂时使用命名空间,因此我们可以这样做:

var myApp = myApp || {};

Then add an object called "commentTable" to hold our Datatable: 然后添加一个名为“ commentTable”的对象来保存我们的数据表:

myApp.components = {
  commentTable: {},
  ... more components 

These are the key rows: (see how I useed the namespace myApp.components.commentTable ) 这些是关键行:(请参阅如何使用命名空间myApp.components.commentTable

myApp.components.commentTable.row.add(commentRow);
myApp.components.commentTable.draw();

That first line adds rows to our new Datatable object. 第一行将行添加到新的Datatable对象。 The second redraws the table with the new data. 第二次重绘带有新数据的表。 That adds the row directly to the table, you could do that and be done. 这样就可以将行直接添加到表中,您可以做到并完成。

Here is my code above reworked for that: 这是我上面修改过的代码:

.done(function(data, status, xhr) {
      $.each(data, function(index, item) {
          var commentRow = {
              "comments": item.comments,
              "comment_TYPE": item.comment_TYPE,
              "lname": item.lname,
              "action_PERFORMED": item.action_PERFORMED,
              "entry_TIME": item.entry_TIME
        }
        myApp.components.commentTable.row.add(commentRow);
      });
      myApp.components.commentTable.draw();
    })

The key to that above is my declaration of the comment rows objects in my namespaced data here: 上面的关键是我在这里的命名空间数据中声明了注释行对象:

myApp.data = {
  commentsURL: "https://www.example.com/comments/get",
  commentcolumns: [{
    title: 'Comments',
    data: 'comments'
  }, {
    title: 'Type',
    data: 'comment_TYPE'
  }, {
    title: 'Last Name',
    data: 'lname'
  }, {
    title: 'Action',
    data: 'action_PERFORMED'
  }, {
    title: 'Time',
    data: 'entry_TIME'
  }],
  commentdata: [{
    "comments": "Caught some fish in the lake.",
    "comment_TYPE": "Fisherman",
    "lname": "Wonka",
    "action_PERFORMED": "caught",
    "entry_TIME": new Date(2016, 4, 21, 18, 45, 23, 9)
  },...more rows
  ],
  ...more data stuff
};

Here is where/how I declared the Datatable using my namespace: 这是我使用名称空间声明数据表的位置/方式:

myApp.components.commentTable = $('#exampleemtpy').DataTable({
  data: myApp.data.columndata,
  columns: myApp.data.commentcolumns
});

Markup for the table (all of it) 表格的标记(全部)

OR it could be populated with data as my second table is in the fiddle. 或者,当我的第二张表摆在小提琴中时,它可能会填充数据。

See this all in action here: (Note this has a bit more as I wanted to actually test adding rows, SO, I added a form and manual methods to insert since I did NOT have any ajax server; ignore that all and focus on the lines noted above) 在此处查看全部操作:(请注意,这还有一点点,因为我想实际测试添加行,因此,由于没有任何Ajax服务器,因此添加了要插入的表单和手动方法;请忽略所有内容,并专注于上面提到的行)

Fiddle to play with: https://jsfiddle.net/MarkSchultheiss/gwawccqt/4/ 小提琴玩: https : //jsfiddle.net/MarkSchultheiss/gwawccqt/4/

Please check this link on how to Reload the datatable data from the Ajax data source. 请检查此链接,以了解如何从Ajax数据源重新加载数据表数据。 https://datatables.net/reference/api/ajax.reload() https://datatables.net/reference/api/ajax.reload()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM