dtkvlj5386 2016-12-28 09:10
浏览 70

如何使用bootstrap dataTables检测我编辑过的单元格?

I am working with editable bootstrap datatables. What I want to do is update the mySQL database when edited a cell:

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>

basic.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
     });
}

update.php:

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

I have difficulties to find out what cell exactly I have edited. So when I change for example "Joe" to "Alan" I need the information that it is id:1 and row:First name. I was wondering if dataTables already have variables for this information like for example updatedRow.data().


One Addition:

the Console gives my the output:

[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 

So my idea is now to parse updatedRow.data() to get the id of the edited cell

  • 写回答

1条回答 默认 最新

  • duanhan9479 2016-12-28 09:15
    关注

    Change data: {new:updatedCell.data()}, instead of data: 'new='+ updatedCell.data(),

    like bellow :

    $.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');
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀