簡體   English   中英

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

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

一旦從方法調用該函數,我需要您的幫助才能刷新 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>

您可以部分加載 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>

使用此代碼

$(".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