使用 data-id="" 以 ':' 分隔传递数据数组 | PHP、HTML、SQL

Pass data array using data-id="" separated by ':' | PHP, HTML, SQL

我目前在 SQL 中有一个数据表,我可以传递的数据只有 $row['deptcode'],有没有办法将超过 1 个数据 ID 传递到模态并分发它来分隔输入文本?如果是这样,我将如何通过它?有没有办法将数据也传递到 PHP?

这样可以吗? data-id='".$row['deptcode'].":".$row['empno'].":".$row['departmentname'].":".$row['jobposition']."'

 <div class="box-body">
              <table id="example2" class="table table-bordered">
                <thead>
                  <th>Job ID</th>
                  <th>Latest Data</th>
                  <th>Department</th>
                  <th>Job Position</th>
                  <th>Payment Mode</th>
                  <th>Tools</th>
                </thead>
                <tbody>
                  <?php
$sql = "
SELECT DISTINCT 
LEFT (departmentcode,4) as deptcode,
MAX(RIGHT(employeeidno, 7)) as empno,
section,  departmentname, paymentmode,
hrdjobpositionentry.jobposition as jobposition
FROM departmentmasterfile, hrdjobpositionentry,employeemasterfile 
WHERE hrdjobpositionentry.department=departmentmasterfile.departmentname AND
LEFT (employeemasterfile.employeeidno,4) = LEFT (departmentmasterfile.departmentcode,4)
GROUP BY LEFT (departmentcode,4), section, departmentname, paymentmode, hrdjobpositionentry.jobposition
ORDER BY LEFT (departmentcode,4)";
                    $query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
                    while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
                      echo "
                        <tr>
                          <td>".$row['deptcode']."</td>
                          <td>".$row['empno']."</td>
                          <td>".$row['departmentname']."</td>
                          <td>".$row['jobposition']."</td>
                          <td>".$row['paymentmode']."</td>
                          <td><button class='btn btn-success btn-sm select btn-flat' data-id='".$row['deptcode']."'><i class='fa fa-select'></i> Select</button></td>
                        </tr>
                      ";
                    }
                  ?>
                </tbody>
              </table>

目前我只能将此数据传递给单个模态。每当我在table中的select行时,都可以通过这里

<script>
$(function(){

    $("body").on('click', '.select', function (e){
    e.preventDefault();
    $('#select').modal('show');
    var id = $(this).data('id');
    getRow(id);
  });
});

步骤 1- 单击按钮时,使用 closest() 方法获取所选行的所有数据。 https://api.jquery.com/closest/.

步骤 2- 使用 map,将所有需要的数据存储在一个数组中。

步骤 3 - 使用此数组作为 getRow 函数中的参数。

第4步-在getRow函数中使用循环获取数组的数据并显示在模型中

jQuery代码.

<script>

function getRow(data){
    $.each(data, function(index, value) {
              // console.log(index+':'+value);
    });

    //Get value by individual key
    var Job_ID = data['Job ID'];
    var Latest_Data = data['Latest Data'];
    var Department = data['Department'];
    var Job_Position = data['Job Position'];
    var Payment_Mode = data['Payment Mode'];

    console.log(Job_ID+':'+Latest_Data+':'+Department+':'+Job_Position+':'+Payment_Mode);
}

$(document).ready(function() {
$("body").on('click', '.select', function (e){
row = {};
e.preventDefault();
    var data = $(this).closest('tr').find('td').not(':last').map(function() {
        var index = $(this).index();
        var key = $('thead th').eq(index).text();
        value = $(this).text().trim();
        row[key] = value;
        });

    //you will get all the row data in array with key.
    console.log(row);

    getRow(row);
  });
});
</script>