ajax动态更新dynatable 表格里面的数据

易飞文
2023-12-01
html代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script src="vendor/jquery-1.7.2.min.js"></script>
<script src="jquery.dynatable.js"></script>
<link  rel="stylesheet"  href="jquery.dynatable.css" >
</head>
<body>
<script>
$(document).ready(function() {
	var mytable= $("#dynatable");
    setInterval(function() {	
  $.ajax({
      url: "do.php",
      success: function(data) {
          myRecords = $.parseJSON(data);
          mytable.dynatable({
              dataset: {				  
                  records:myRecords
              }
          });
		    mytable.data('dynatable').settings.dataset.records=myRecords;
		 mytable.data('dynatable').dom.update();		
		   console.log(data);
      }
  });
    }, 1000);
});

</script>
<table id="dynatable">
  <thead>
  <th>test1</th>
    <th>test2</th>
      </thead>
  <tbody>
  </tbody>
</table>
</body>
</html>

服务端php代码


<?php 
$response = array();
 $response [] = array(
        "test1" => "AAAA".time(),
        "test2" => "BBBB".time()
		   );

echo json_encode($response);
?>



其实这样更新也是可以的


$(document).ready(function() {
    setInterval(function() {
        $.ajax({
            url: "myStuff.php",
            success: function(data) {
                console.log(data);

                var myRecords = $.parseJSON(data);

                var dynatable = $('#dynatable').dynatable({
                    dataset: {
                        records: myRecords
                    }
                }).data('dynatable');

                dynatable.settings.dataset.originalRecords = myRecords;
                dynatable.process();
            }
        });
    }, 3000);
});



 类似资料: