简体   繁体   English

如何使用 jquery/ajax 刷新 div 中的表格内容

[英]How to refresh table contents in div using jquery/ajax

I need your help in order to refresh a div id="mytable" in my html once the function is called from a method.一旦从方法调用该函数,我需要您的帮助才能刷新 html 中的 div id="mytable" Currently, I am loading the full page once it is called using the below lines.目前,一旦使用以下几行调用它,我就会加载整个页面。

In my java method, I am using the below line to call a javascript method:在我的 java 方法中,我使用以下行来调用 javascript 方法:

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

The html code : html代码:

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

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

You can load HTML page partial, in your case is everything inside div#mytable.您可以部分加载 HTML 页面,在您的情况下是 div#mytable 中的所有内容。

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

more information read this http://api.jquery.com/load/更多信息阅读这个http://api.jquery.com/load/

Update Code (if you don't want it auto-refresh)更新代码(如果您不希望它自动刷新)

<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>

use this code使用此代码

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

<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);
          });

        }

      })

    });

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

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