0

我有一个由 4 列组成的数据库(id-symbol-name-contractnumber)。所有 4 列及其数据都使用 JSON 显示在用户界面上。

有一个函数负责向数据库添加新列,例如(国家代码)。

该库已成功添加到数据库中,但无法在用户界面中显示新添加的库。

下面是我显示列的代码。

你能帮助我吗?

表.php

        <!DOCTYPE html>
        <html lang="en">
        <head>

<link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
<script type="text/javascript" src="../../scripts/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxmenu.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxgrid.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxgrid.selection.js"></script> 
<script type="text/javascript" src="../../jqwidgets/jqxgrid.filter.js"></script>        
<script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script>   
<script type="text/javascript" src="../../jqwidgets/jqxlistbox.js"></script>    
<script type="text/javascript" src="../../jqwidgets/jqxdropdownlist.js"></script>   
<script type="text/javascript" src="../../scripts/gettheme.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    // prepare the data
    var theme = getDemoTheme();

    var source =
    {
        datatype: "json",
    datafields: [
                 { name: 'id' },
                 { name: 'symbol' },
                 { name: 'name' },

                 { name: 'contractnumber' }
            ],
        url: 'data.php',
        filter: function()
        {
            // update the grid and send a request to the server.
            $("#jqxgrid").jqxGrid('updatebounddata', 'filter');
        },
        cache: false
    };      
    var dataAdapter = new $.jqx.dataAdapter(source);

    // initialize jqxGrid
    $("#jqxgrid").jqxGrid(
    {       
        source: dataAdapter,
        width: 670,
        theme: theme,
        showfilterrow: true,
        filterable: true,
        columns: [
            { text: 'id', datafield: 'id', width: 200 },
            { text: 'symbol', datafield: 'symbol', width: 200 },
            { text: 'name', datafield: 'name', width: 100 },
            { text: 'contractnumber', filtertype: 'list', datafield: 'contractnumber' }
        ]
    });
    });
</script>
       </head>
           <body class='default'>
        <div id="jqxgrid"></div>
        </div>
            </body>
       </html>

数据.php

      <?php
#Include the db.php file
include('db.php');
#Connect to the database
//connection String
$connect = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die('Could not connect: ' . mysql_error());
//Select The database
$bool = mysql_select_db($mysql_database, $connect);
if ($bool === False){
   print "can't find $database";
}

$query = "SELECT * FROM pricelist";

$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
$orders = array();
// get data and store in a json array
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $orders[] = array(
        'id' => $row['id'],
        'symbol' => $row['symbol'],
        'name' => $row['name'],

        'contractnumber' => $row['contractnumber']

      );
}

echo json_encode($orders);
  ?>
4

3 回答 3

0

当您向数据库添加新列时,您也必须以某种方式更新 UI。为此,您必须向源对象的 datafields 数组添加一个新数据字段,并通过设置 columns 属性来更新 Grid 的列。

$("#jqxgrid").jqxGrid({columns: newColumnsArray});

于 2013-06-25T09:41:03.803 回答
0

您说“contrycode”列已正确附加到pricelist表中?$order因此,获取列值并将它们显示为 JSON看起来并不难。无需过多查看您的代码,我将从...开始

# in your PHP
$orders[] = array(
    'countrycode' = $row['countrycode'],
    'id' => $row['id'],
  ...

和:

# in your JS
    columns: [
        { text: 'countrycode', datafield: 'countrycode', width: 2 },
        { text: 'id', datafield: 'id', width: 200 },
  ...
于 2013-06-25T09:41:11.820 回答
0

对于 php 方面,如果您在 while 中使用循环,则应该很容易为动态字段创建数组。

$orders = array(); $i  = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $orders[$i] = array();
    foreach($row as $col => $value) {
        $orders[$i][$col] = $value; 
    }
    $i++;
}

但是对于 jqgrid,您必须自己找到解决方案才能显示动态列。

于 2013-06-25T09:42:13.120 回答