【问题标题】:How to refresh table contents in div using jquery/ajax如何使用 jquery/ajax 刷新 div 中的表格内容
【发布时间】:2015-07-27 02:39:51
【问题描述】:

一旦从方法调用函数,我需要您的帮助才能刷新我的 html 中的 div id="mytable"。目前,一旦使用下面的行调用它,我就会加载整个页面。

在我的 java 方法中,我使用下面一行来调用一个 javascript 方法:

RequestContext.getCurrentInstance().execute("autoRefresh()"); 

html代码:

<script type="text/javascript">
    function autoRefresh() {
        window.location.reload();
    }
</script>

<div id='mytable'>
    <h1 id='My Table'>
        <table></table>
    </h1>
</div>

【问题讨论】:

  • 您需要在服务器上创建一个端点,当您向其发出 AJAX 请求时,该端点返回填充 div 元素所需的 HTML。就目前而言,您的问题太广泛了。
  • 我没有正确得到您的答案,请您解释一下

标签: javascript jquery ajax xhtml


【解决方案1】:

您可以加载部分 HTML 页面,在您的情况下是 div#mytable 中的所有内容。

setTimeout(function(){
   $( "#mytable" ).load( "your-current-page.html #mytable" );
}, 2000); //refresh every 2 seconds

更多信息请阅读此http://api.jquery.com/load/

更新代码(如果您不希望它自动刷新)

<button id="refresh-btn">Refresh Table</button>

<script>
$(document).ready(function() {

   function RefreshTable() {
       $( "#mytable" ).load( "your-current-page.html #mytable" );
   }

   $("#refresh-btn").on("click", RefreshTable);

   // OR CAN THIS WAY
   //
   // $("#refresh-btn").on("click", function() {
   //    $( "#mytable" ).load( "your-current-page.html #mytable" );
   // });


});
</script>

【讨论】:

  • 我应该把上面的内容放在我的 autorefresh() 函数中吗?
  • 不要,我忘了在那里添加 setTimeout() 。我已经更新了上面的代码。它每 2 秒刷新一次
  • 让我试试然后回来.....我需要导入或包含任何 jquery 库吗?
  • @Uma,是的,它可以,(1)你应该有你的行的唯一标识符,(2)你可以更改脚本以用新的替换你的特定行。只需修改选择器并选择具有行 ID 的行。或者你可以使用 reactjs 或 vuejs,很容易让你的表格总是随着你的变化而更新
  • @NoobDEV-GBL,尝试查看控制台日志中的错误。我想它会给你一个提示
【解决方案2】:

使用此代码

$(".table").load(location.href + ".table");

不要忘记在 .table 之前留出空格 例如:$(".table").load(location.href + "SPACE.table")

【讨论】:

  • 我真的不明白为什么有人 -1 你,这正是我需要根据我的代码和确切的问题。来自我的 +1。
  • 我认为他没有检查代码。由,非常感谢。
【解决方案3】:
<div class="card-body">
            <div class="table-responsive">
              <table class="display dataTable no-footer" id="notificationTable" role="grid" aria-describedby="basic-1_info">
                <thead>
                  <tr>
                    <th>ID</th>
                    <th> Title</th>
                    <th> Brief</th>
                    <th>Category</th>
                    <th>To</th>
                    <th>By</th>
                    <th>Edit</th>
                    <th>Delete</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    

                  </tr>
                </tbody>
              </table>
            </div>
          </div>
let notificationTable = $('#notificationTable').DataTable({
      "processing": true,
      // 'scrollX': true,
      "serverSide": true,
      "ordering": false,
      dom: 'Bfrtip',
      buttons: [{
        extend: 'copy',
        exportOptions: {
          columns: '0,1,3,4'
        }
      },
      {
        text: 'CSV',
        className: "csvGenerate",
        action: function (e, dt, node, config) {
          getCSVFile();
        }
      },
      {
        text: 'Excel',
        className: "excelMyButtonsToHide",
        action: function (e, dt, node, config) {
          getExcelFile();
        }
      }, {
        extend: 'print',
        exportOptions: {
          columns: '0,1,3,4'
        }
      },
        'colvis'-
      ],
      language: {
        buttons: {
          colvis: '<span class="icon icon-eye" style="font-size: x-small;"/>'
        }
      },
      'columnDefs': [{
        "visible": false,
        "targets": []
      }],
      "ajax": {
        "url": '/system/admin/notifications/allNotifications?fromDate='+ fromStart +'&toDate=' +endDate +'&orderCol=' + ord + '&column=' + col,
        "type": "GET",
        dataFilter: function (data) {
          responseData = jQuery.parseJSON(data);
          notificationData = responseData.data;
          return JSON.stringify(responseData);
        }
      },
      "columns": [{
        "data": "id"
      },
      {
        "data": "offerTitle"
      },
      {
        "data": "offerBrief"
      },
      {
        "data": "offerCategory"
      },
      {
        "data": "offerTo"
      },
      {
        "data": "offerBy"
      },
      {
        sortable: false,
        "render": function (data, type, full) {
          let buttonID = "edit_" + full.id;
          return '<a id=' + buttonID + ' class="icofont icofont-edit edit"></a>';
        }
      },
        {
          sortable: false,
          "render": function (data, type, full) {
            let buttonID = "delete_" + full.id;
            return '<a id=' + buttonID + ' class=" icofont icofont-trash trash"></a>';
          }
        },


      ],
    });
$('#notificationTable').on('click', 'a.trash', function (row) {

      let rowId = row.target.id;

      selectedId = rowId.split('_')[1]

      Swal.fire({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'
      }).then((result) => {
        if (result.isConfirmed) {
          axios.delete(`/system/admin/notifications/${selectedId}`).then (function (response){

            $("#addCashback").text("Add").prop('disabled',false)
            if (true){
              //if (response.data.success){
              $("#addNewNotification").modal("hide")
              notificationTable.draw(true)
              notify("Success","Data Saved successfully","success")
            }
            else{
              notify("Error",response.data.errors,"danger")
            }

          })
          Swal.fire(
                  'Deleted!',
                  'Your file has been deleted.',
                  'success'



          ).then(function() {
            notificationTable.draw(true);
          });

        }

      })

    });

【讨论】:

  • 感谢您的回答。由于它只包含代码,请您在上面解释一下吗?
  • 要刷新一个表,你必须添加 notificationTable.draw(true); .它只会刷新表格而不是整个页面
猜你喜欢
  • 1970-01-01
  • 2013-08-31
  • 2013-01-27
  • 1970-01-01
  • 2014-11-24
  • 2012-09-16
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多