【问题标题】:How to fetch data from MSSQL Query for DataTables using Ajax如何使用 Ajax 从 MSSQL 查询中为 DataTables 获取数据
【发布时间】:2015-02-17 22:59:57
【问题描述】:

这是我从DataTables Child Rows得到的代码

$(document).ready(function() {
    var table = $('#example').DataTable( {
        "ajax": "../ajax/data/objects.txt", //here
        "columns": [
            {
                "className":      'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            }, //and here to fetch the data below
            { "data": "name" }, 
            { "data": "position" }, 
            { "data": "office" },
            { "data": "salary" }
        ],
        "order": [[1, 'asc']]
    } );

我想使用 ajax 从 SQL 查询中获取数据。这是我的 SQL 查询:

$tsql = 
"SELECT *
FROM [dbo].[ITEM_MASTER] A
INNER JOIN
[dbo].[STOCK] B
ON
B.ItemId = A.ItemId
";
$result = sqlsrv_query($conn, $tsql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
if (!$result) {
 die("Query to show fields from table failed");
}

while($row=sqlsrv_fetch_array($result))
{
    $ItmId = $row['ItemId'];
    $ItmName = $row['ItemName'];
    $ItmType = $row['ItemType'];
    $ItmGroup = $row['ItemGroup'];
    $ItmClass = $row['ItemClass'];
    $ItmSerialNum = $row['ItemSerialNum'];
    $ItmUOM = $row['ItemUOM'];
    $StkQty = $row['StockQuantity'];
    $StkId = $row['StockId'];
 }

在 ajax 部分,我只需调用变量的名称,如 $ItmId 或我在 while 循环中所述的名称。 可能吗?如果是这样,怎么做?因为我对AJAX一无所知


更新

数据被推送到第二个参数并且没有显示ItmId?而且无论我是否更改$data : ItmName,它都只会根据数组显示并显示其他内容?

$(document).ready(function() {
    var table = $('#table').DataTable( {
        "ajax": {
            "url": "table_data.php",
            "type": "POST"
            },
        "columns": [
            {
                "class":          'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            },
            { "$data": "ItmId"  },
            { "$data": "ItmName" },
            { "$data": "ItmGroup"},
            { "$data": "ItmClass"}
    ],
        "order": [[1, 'asc']]
    } );

【问题讨论】:

  • 您需要创建一个 json 对象并将数据添加到该 json 响应中的 data 数组中。
  • @Darren 你能举个例子吗?

标签: php jquery sql-server ajax datatable


【解决方案1】:

首先,你需要改变这一行:

"ajax": "../ajax/data/objects.txt", //here

指向将运行 sql 查询以获取数据的实际文件:即像这样:

"ajax": {"url": "path/to/phpfile.php", "type": "POST"}

你需要像这样让你的while循环:

$data = array();
while($row=sqlsrv_fetch_array($result))
{
    $ItmId = $row['ItemId'];
    $ItmName = $row['ItemName'];
    $ItmType = $row['ItemType'];
    $ItmGroup = $row['ItemGroup'];
    $ItmClass = $row['ItemClass'];
    $ItmSerialNum = $row['ItemSerialNum'];
    $ItmUOM = $row['ItemUOM'];
    $StkQty = $row['StockQuantity'];
    $StkId = $row['StockId'];

    $data['data'][] = array($ItmId, $ItmName, $ItmType,....etc);
 }
 echo json_encode($data);

您应该注意,您需要实际表 (html) 中的确切列数。 此外,您的 json 应如下所示:

data:
    array(
        ItmId,
        ItmName,
        ..etc
    ),
    array(
        ItmId,
        ItmName,
        ..etc
    ),

基本上有一个行数组。

【讨论】:

  • 所以这部分"ajax": "../ajax/data/objects.txt"{ "data": "name" },那我应该如何获取数据呢?
  • data : array( ItmId, ItmName,......), 应该替换 { "data": "name" }
  • 不,你应该从你的dataTables JS 代码中删除它。
  • 对不起,我必须摆脱这一切?你能告诉我吗:(
  • 达伦,我已经解决了。感谢您的指导!但我必须更改我的 while 循环中的代码
【解决方案2】:

根据达伦的回答,我是这样解决的:

table_data.php

    $data = array();

    while($row=sqlsrv_fetch_array($result))
    {
    $data['data'][] = array(
                        'ItmId'          => $row['ItemId'],
                        'ItmName'        => $row['ItemName'],
                        'ItmType'        => $row['ItemType'],
                        'ItmGroup'       => $row['ItemGroup'],
                        'ItmClass'       => $row['ItemClass'],
                        'ItmSerialNum'   => $row['ItemSerialNum'],
                        'ItmUOM'         => $row['ItemUOM'],
                        'StkQty'         => $row['StockQuantity'],
                        'StkId'          => $row['StockId']
                        );

    }

   echo json_encode($data);

table.php(我在其中显示我的 html)

$(document).ready(function() {
    var table = $('#table').DataTable( {
        "ajax": {
            "url": "table_data.php",
            "type": "POST"
            },
        "columns": [
            {
                "class":          'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            },
            { "data": "ItmId"},
            { "data": "ItmName"},
            { "data": "ItmClass"},
            { "data": "ItmUOM"}
    ],
        "order": [[1, 'asc']]
    } );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    相关资源
    最近更新 更多