doumu2172 2014-12-19 02:01 采纳率: 0%
浏览 60
已采纳

如何使用Ajax从MSSQL Query for DataTables获取数据

This is the code that i get from 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']]
    } );

I would like to get the data from an SQL Query by using ajax. This is my SQL Query :

$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'];
 }

And on the ajax part, i just call the name of the variable like $ItmId or like i've stated in my while loop. Is it possible? If so, how? Since i don't have any knowledge on AJAX


UPDATE

The data were pushed to the second parameter and is not showing the ItmId ? And no matter if i change $data : ItmName it just displays based on the arrays and display something else?

$(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']]
    } );
  • 写回答

2条回答 默认 最新

  • dongtui0650 2014-12-19 02:33
    关注

    Firstly, you need to change this line:

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

    to point to the actual file that will run the sql query to fetch the data: i.e. like this:

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

    You need to make your while loop like this:

    $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);
    

    You should note, you require the exactly amount of columns in your actual table (html). Also, your json should look like this:

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

    Essentially have an array of rows.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3