postgresql 未与HTML表的元素绑定的数据列

zed5wv10  于 2023-02-15  发布在  PostgreSQL
关注(0)|答案(1)|浏览(132)

我尝试使用Ajax和PHP将PostgreSQL中的数据显示到HTML表中,但似乎我得到的数据没有绑定到HTML表的columns元素中,我只得到了一个Json字符串


如何将idemployee_nameemployee_salaryemployee_age列与HTML表的"td"元素绑定
这是我的代码:

    • 超文本标记语言**
<div class="col-sm-6" style="padding-top:50px;">
        <table id="employee_grid" class="table" width="100%" cellspacing="0">
            <thead>
                <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Salary</th>
                <th>Age</th>
                </tr>
            </thead>
            <tbody id="emp_body">
            </tbody>
        </table>
    </div>
    • Ajax**(将列与HTML表格的元素绑定):
$(document).ready(function(){
    /* Handling get employee */
    function get_employee() {
      $.ajax({        
        type : 'GET',
        url  : 'response.php?action=list',
        success : function(response){
          response = JSON.parse(response);
            var tr;
                $('#emp_body').html('');
                $.each(response, function( index, emp ) {
                tr = $('');
                    tr.append("" + emp.id + "");
                    tr.append("" + emp.employee_name + "");
                    tr.append("" + emp.employee_salary + "");
                    tr.append("" + emp.employee_age + "");
    
                    var action = 
                    "<div class='btn-group' data-toggle='buttons'>";
                    action += "<a href='#' target='_blank' class='btn btn-warning btn-xs' data-toggle='modal' data-target='#edit_model'>Edit</a>";
                    action += "<a href='#' target='_blank' class='btn btn-danger btn-xs'>Delete</a></div>";
                    tr.append(action);
                    $('#emp_body').append(tr);
            });
        }
      });
    }
    
    //initialize method on load
    function init() {
        get_employee();
    }
    init();
});
    • PHP**(从PostgreSQL查询数据)
<?php
include("connection.php");
 
$params = $_REQUEST;
$action = isset($params['action']);
$params['action'] !='' ? $params['action'] : 'list';
$empCls = new Employee();
 
switch($action) {
    case 'list':
        $empCls->getEmployees();
    break;
    default:
    return;
}

class Employee {
    protected $conn;
    protected $data = array();
    function __construct() {

    $db = new dbObj();
    $connString =  $db->getConnstring();
        $this->conn = $connString;
    }
    
    function getEmployees() {
        $sql = "SELECT * FROM employee";
        $queryRecords = pg_query($this->conn, $sql) or die("error to fetch employees data");
        $data = pg_fetch_all($queryRecords);
        echo json_encode($data);
    }
}
?>
watbbzwu

watbbzwu1#

在将响应追加到表中时,还需要添加<tr><td>标记。
可以在当前代码中添加的更改:

var tr = $('<tr></tr>'); //tr tag
  //appending td >>
  tr.append("<td>" + emp.id + "</td>");
  tr.append("<td>" + emp.employee_name + "</td>");
  tr.append("<td>" + emp.employee_salary + "</td>");
  tr.append("<td>" + emp.employee_age + "</td>");

演示代码

x一个一个一个一个x一个一个二个x

相关问题