php - 如何检测我使用 Bootstrap 数据表编辑过的单元格?

标签 php mysql ajax twitter-bootstrap datatables

我正在使用可编辑的 Bootstrap 数据表。我想要做的是在编辑单元格时更新 mySQL 数据库:

index.php:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css" />
</head>
<body>
    <table id="myTable" class="stripe">
        <thead>
            <tr>
                <th>id</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <th>1</th>
                <td>Elliott</td>
                <td>Beaty</td>
                <td>elliott@example.com</td>
            </tr>
            <tr>
                <th>2</th>
                <td>Joe</td>
                <td>Dirt</td>
                <td>Joe@example.com</td>
            </tr>
            <tr>
                <th>3</th>
                <td>John</td>
                <td>Doe</td>
                <td>John@example.com</td>
            </tr>
            <tr>
                <th>4</th>
                <td>Jane</td>
                <td>Doe</td>
                <td>Jane@example.com</td>
            </tr>
            <tr>
                <th>5</th>
                <td>Billy</td>
                <td>Bob</td>
                <td>Billy@example.com</td>
            </tr>
            <tr>
                <th>6</th>
                <td>Bobby</td>
                <td>Axlerod</td>
                <td>Bobby@axecapital.com</td>
            </tr>
        </tbody>
    </table>
</body>
</html>
<script src="https://code.jquery.com/jquery-1.12.2.min.js"></script>
<script src="http://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<script src="https://raw.githubusercontent.com/ejbeaty/CellEdit/master/js/dataTables.cellEdit.js"></script>
<script src="basic.js"></script>

基本.js:

$(document).ready(function () {
    var table = $('#myTable').DataTable();

    table.MakeCellsEditable({
        "onUpdate": myCallbackFunction
    });
});

function myCallbackFunction(updatedCell, updatedRow, oldValue,row) {
    console.log("The new value for the cell is: " + updatedCell.data());
    console.log("The old value for that cell was: " + oldValue);
    console.log("The values for each cell in that row are: " + updatedRow.data());
    jQuery.ajax({
       type: "POST",
       url: "update.php",
       data: 'new='+ updatedCell.data(),
       cache: false
     });
}

更新.php:

require('database.php'); 
$new = @$_POST['new'];
$pdo = $db->prepare("UPDATE data SET new=? WHERE id=?");  
$pdo->execute(array($update)); 

我很难找出我到底编辑了哪个单元格。因此,当我将“Joe”更改为“Alan”时,我需要它是 id:1row:First name 的信息。我想知道 dataTables 是否已经具有此信息的变量,例如 updatedRow.data()

<小时/>

添加一个:

控制台给出了我的输出:

[Log] The new value for the cell is: Alan
[Log] The old value for that cell was: Joe 
[Log] The values for each cell in that row are: 2,Alan,Dirt,Joe@example.com 

所以我现在的想法是解析updatedRow.data()以获取已编辑单元格的ID

最佳答案

更改 data: {new:updatedCell.data()}, 而不是 data: 'new='+ UpdatedCell.data(),

如下所示:

$.ajax({
    url: "update.php",
    type: "POST", 
    data: {new:updatedCell.data()},
    contentType: false,
}).done(function (ex) {
   //alert(updatedRow.data());  
   var final_data = updatedRow.data(); 
   var split_data = final_data.split(","); 

   alert("Id : "+ split_data[0] + "Email : " + split_data[3]);      
}).fail(function (ex) {
   console.log('failed');
});

关于php - 如何检测我使用 Bootstrap 数据表编辑过的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41358757/

相关文章:

java - 如何防止 primefaces ajax 调用禁用我的脚本

php - 计算列,其中 column1 LIKE %something%

php - 执行此 UPDATE 查询的最佳方法

php - 通过php/json从数据库获取数据

javascript - Jasmine 模拟 Angular $http - 错误 : describe does not expect a done parameter

javascript - 在 cordova 应用程序上使用 AJAX 功能的最佳方法是什么?

php - 当用户选择所有内容时询问高级搜索?

php - 如何对此上下文建模,以便有可能创建自定义问题并收集答案?

php - 要存储到数据库中的音频文件

mysql - MySQL Cluster 7.3 如何实现 99,999% 的可用性? CAP定理的对立面