0

我正在将可编辑网格与 mysql 一起使用。它将数据绑定并显示到我的网格。但是,当我尝试编辑或更新网格时,它没有这样做。

下面是我的加载数据的一些代码,

$grid->addColumn('CertificateNo', 'CertificateNo', 'integer', NULL, false); 
$grid->addColumn('ID', 'ID', 'integer');
$grid->addColumn('QuizNo', 'Test No', 'integer');  
$grid->addColumn('Received', 'Received', 'boolean');  
$grid->addColumn('DateReceived', 'Date Received', 'datetime'); 

以下是更新脚本的代码:

    // Get all parameters provided by the javascript
$colname = $mysqli->real_escape_string(strip_tags($_POST['colname']));
$id = $mysqli->real_escape_string(strip_tags($_POST['id']));
$coltype = $mysqli->real_escape_string(strip_tags($_POST['coltype']));
$value = $mysqli->real_escape_string(strip_tags($_POST['newvalue']));
$tablename = $mysqli->real_escape_string(strip_tags($_POST['tablename']));
    / This very generic. So this script can be used to update several tables.
$return=false;
if ( $stmt = $mysqli->prepare("UPDATE ".$tablename." SET ".$colname." = ? WHERE id = ?")) {
    $stmt->bind_param("si",$value, $id);
    $return = $stmt->execute();
    $stmt->close();

下面是将值传递给我的 update.php 脚本的 javascript 部分。

    function updateCellValue(editableGrid, rowIndex, columnIndex, oldValue, newValue, row, onResponse)
{      
    $.ajax({
        url: 'update.php',
        type: 'POST',
        dataType: "html",
        data: {
            tablename : editableGrid.name,
            id: editableGrid.getRowId(rowIndex), 
            newvalue: editableGrid.getColumnType(columnIndex) == "boolean" ? (newValue ? 1 : 0) : newValue, 
            colname: editableGrid.getColumnName(columnIndex),
            coltype: editableGrid.getColumnType(columnIndex)            
        },
        success: function (response) 
        { 
            // reset old value if failed then highlight row
            var success = onResponse ? onResponse(response) : (response == "ok" || !isNaN(parseInt(response))); // by default, a sucessfull reponse can be "ok" or a database id 
            if (!success) editableGrid.setValueAt(rowIndex, columnIndex, oldValue);
            highlight(row.id, success ? "ok" : "error"); 
        },
        error: function(XMLHttpRequest, textStatus, exception) { alert("Ajax failure\n" + errortext); },
        async: true
    });

这个模板带有一个 javascript editablegrid-2.0.1。我注意到问题与主键有关。在可以从 www.editablegrid.net 找到的演示中,该表具有主键 ID,而我的具有 CertificateNo,但我的表中的 ID 不是主键。

所以我改变了

$grid->addColumn('ID', 'ID', 'integer', NULL, false);

$grid->addColumn('CertificateNo', 'CertificateNo', 'integer', NULL, false); 

现在我无法更新网格。

4

3 回答 3

1

我有同样的问题,你可以在 loaddata.php 中看到这段代码

$grid->addColumn('action', 'Action', 'html', NULL, false, 'id'); 

用“CertificateNo”替换最后一列的“id”,它会是这样的

$grid->addColumn('action', 'Action', 'html', NULL, false, 'CertificateNo');

然后重新加载您的页面以查看更改。

于 2014-11-08T12:42:17.340 回答
0

update.php 使用 $_POST['id'] 也许您应该将其更改为 $_POST['CertificateNo'] 并将 id 作为另一列处理,因为它不再是主键。

于 2013-10-09T00:27:54.323 回答
0

I had the same problem. You have to change in js/demo.js in

function DatabaseGrid() 
{ 
    this.editableGrid = new EditableGrid("HERE!!! Chage to name of your sql table", {...

and also rename your CertificateNo to "id" or make new before it, because the grid works with it on many places, so it's hard to change everywhere.

于 2014-03-17T14:28:34.000 回答