MySQL INSERT with node.js 后 jTable 不显示新行内容

jTable not showing new row content after MySQL INSERT with node.js

我有一个小 jTable,它显示了使用 node.js 和 express-generator 和 mysql 从我的本地数据库中提取的记录列表。我可以显示、创建、编辑和删除记录 in/from 我的 table 就好了。

我的问题是,当我使用我的 jTable 添加新记录时,它会在我的 jTable 中创建一个空行,并且不会用新添加的数据填充它,除非我刷新页面。这很奇怪,因为当我也使用 jTable 编辑和删除它时,它成功地更新了 jTable 中的行。

谁能看出这是为什么?
(我排除了编辑和删除代码,因为它们工作正常)

tbl_bar结构

rowID = INT, PK, AI
rowName = VARCHAR, UQ
rowOrder = INT (for future row organization)

index.js

var express = require('express');
var router = express.Router();

var mysql = require('mysql');
var connection = mysql.createConnection({
    host:     'localhost',
    user:     'root',
    password: 'password'
});

// Select which database to use
connection.query('USE db_foo;');

/*********************/
/***** SHOW ROWS *****/
/*********************/
// Retrieves all rows and sends them to the jTable for display
router.showRows = function (request, response) {
    // Query tbl_bar and return the ordered result/s
    connection.query('SELECT * FROM tbl_bar ORDER BY rowOrder;', function (error, result) {
        if (error) response.send({ Result: "ERROR", Message: "Error getting rows" });
        else response.send({ Result: "OK", Records: result });
    });
}

/**********************/
/***** CREATE ROW *****/
/**********************/
// Function for inserting new row into tbl_bar
function insertRow(data, response) {
    connection.query('INSERT INTO tbl_bar SET ?', data, function(error, result) {
        if (error) response.send({ Result: "ERROR", Message: "Error adding row" });
        else response.send({ Result: "OK", Records: result });
    });
}
// Adds a new row to tbl_bar
router.createRow = function (request, response) {
    var query = JSON.parse(JSON.stringify(request.body));

    // Check if tbl_bar contains any rows
    connection.query('SELECT COUNT(*) AS rowCount FROM tbl_bar', function (error, result) {
        if (error) response.send({ Result: "ERROR", Message: "Error getting rows" });
        else {
            var data = {
                rowName: query.rowName
            };

            // If tbl_bar contains no rows, make rowOrder 1
            if (result[0].rowCount === 0) {
                data['rowOrder'] = 1;
                insertRow(data, response);
            } else {
                // Get the highest rowOrder to determine order of newly added row
                connection.query('SELECT rowOrder FROM tbl_bar ORDER BY rowOrder DESC LIMIT 1', function (error, result) {
                    if (error) response.send({ Result: "ERROR", Message: "Error getting rows" });
                    else {
                        data['rowOrder'] = result[0].rowOrder + 1;
                        insertRow(data, response);
                    }
                });
            }
        }
    });
}

/*************************/
/***** GET HOME PAGE *****/
/*************************/
router.get('/', function (request, response, next) {
    response.render('index', { title: 'My jTable' });
});

// Event listeners
router.post('/showRows', router.showRows);
router.post('/createRow', router.createRow);

module.exports = router;

table.js

function populateTable() {
    $('#container').jtable({
        title: 'My jTable',
        paging: false,
        messages: {
            addNewRecord: 'Add a New Row'
        },
        deleteConfirmation: function(data) {
            data.deleteConfirmMessage = 'Delete Row: ' + data.record.rowName + '?';
        },
        actions: {
            listAction:   '/showRows',
            createAction: '/createRow'
        },
        fields: {
            rowID: {
                key:    true,
                list:   false,
                create: false,
                edit:   false
            },
            rowName: {
                title:  'Name',
                list:   true,
                create: true,
                edit:   true
            },
            rowOrder: {
                list:   false,
                create: false,
                edit:   true
            }
        }
    });
}

$(document).ready(function () {    
    populateTable();
    $('#container').jtable('load');
});

index.jade

doctype html
html
  head
    meta(charset='utf-8')
    |     
    title My jTable
    |     
    link(rel='stylesheet', type='text/css', href='javascripts/jquery-ui-1.11.4/jquery-ui.min.css')
    |     
    link(rel='stylesheet', type='text/css', href='javascripts/jtable.2.4.0/themes/metro/blue/jtable.min.css')
    |     
    script(type='text/javascript', src='javascripts/jquery-1.11.2.min.js')
    |     
    script(type='text/javascript', src='javascripts/jquery-ui-1.11.4/jquery-ui.min.js')
    |     
    script(type='text/javascript', src='javascripts/jtable.2.4.0/jquery.jtable.min.js')
    |     
    script(type='text/javascript', src='javascripts/table.js')
  |   
  body
    #container

在处理 child table 时,我遇到了同样的问题,只是这次我找到了解决方案。为了让新行显示其数据,我需要使用 recordAdded 事件重新加载 jTable。
遗憾的是我不能保持酷行 fade-in 效果。

recordAdded: function (event, data) {
    $('#container').jtable('reload');
}

我的 child table 我用过

recordAdded: function (event, data) {
    $('.jtable-child-table-container').jtable('reload');
}

阅读您的代码也许您可以尝试更改 insertRow 函数中的这一行:

else response.send({ Result: "OK", Records: result });

else response.send({ Result: "OK", Record: result });


我有同样的问题(jtable 没有在新添加的行中显示数据)但是 PHP。

问题是我的 PHP 页面的 JSON 响应的格式。

此格式(1 个元素数组)未按预期工作:

{"Result":"OK","Record":[{"id_documento":"96","id_prodotto":"27","qta":"5"}]}

此格式确实按预期工作,具有淡入效果:

{"Result":"OK","Record":{"id_documento":"96","id_prodotto":"27","qta":"5"}}

在PHP中,区别是这样的(错误的方式):

while($row = $result->fetch_assoc()){
    $rows[]=$row;
}
$jTableResult['Result'] = "OK";
$jTableResult['Record'] = $rows;

至(正确方式):

$jTableResult['Result'] = "OK";
$jTableResult['Record'] = $result->fetch_assoc();

希望这对您有所帮助。