Jquery ajax返回已排序的数据 [英] Jquery ajax returns data sorted

查看:197
本文介绍了Jquery ajax返回已排序的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本从数据库中提取数据,结果集按特定顺序返回,例如

I have a php script that fetches data from a DB, the result set is returned in a particular order, e.g.

姓名:John,年龄:21岁,地址:1234 Fifth Ave。

我在调试时验证了订单。然后将结果集推入一个数组,然后将其编码为json对象,并通过AJAX请求传递给我的jquery脚本。

I verified the order while debugging. The result set is then pushed into an array, which is then encoded as a json object and passed to my jquery script through an AJAX request.

var resultSet = [];
    $.ajax({
        async: false,
        url: 'scripts/getData.php,
        dataType: "json",
        success: function(data) {
        /* Store the pieces of the array */
        resultSet = data["php_resultSet"]; //<--Breakpoint here shows the resultSet sorted, if order is preserved here, my problem will be solved.
    }
    });

但是,当我收回数据并分析数组时,它会被排序,例如

However, when I get my data back and analyze the array, it is sorted, e.g.

地址:1234 Fifth Ave.,年龄:21,姓名:John年龄:21

我想保留原始结果集的顺序,是否有必须设置的选项才能这样做?感谢帮助。

I want to preserve the order of the original result set, is there an option I have to set in order to do that? Help is appreciated.

更新:

以下是构建数组和编码的PHP代码片段它

Here is a snippet of the PHP code that builds the array and encodes it

    $data = array ( "otherData" => array(), "php_resultSet" => array() );

    while ( $row = sqlsrv_fetch_array ( $stmt, SQLSRV_FETCH_ASSOC ) ) {
        //Push the entire row to the result set array
        array_push ( $data["php_resultSet"], $row );
    }

    asort($someData);

    foreach ( $someData as $key) {
        array_push ( $data["otherData"], $key );
    }

return json_encode($data);//<--At this point, even though the I sorted $someData before pushing it to the $data, the php_resultSet maintains its sorted order.


推荐答案

将哈希(关联数组)编码为JSON时,由于在源语言中表示散列的方式不同,因此无法保证键/值对的顺序。例如,Perl中的哈希以这样的方式存储,以便以字段顺序为代价来优化存储要求。

When encoding a hash (associative array) into JSON, the order of key/value pairs is not guaranteed to be preserved due to differences in how a hash is represented in the source language. For example, hashes in Perl are stored in such a manner as to optimize storage requirements at the expense of field order.

话虽如此,有几种方法可以采取让JavaScript重新排序AJAX结果,一个是返回结果以及预期的演示顺序,如:

Having said that, there are several approaches you can take to have JavaScript reorder the AJAX result, one being to return the result along with the expected order of presentation, as in:

{
    "presentation_order": [
        "Name",
        "Address",
        etc.
     ],
     "records": [
         {"Name":"Juan", etc.},
         etc.
     ]
}

然后使用它在填充预期出现的表格之前或之时重新排序记录。

And then use it to either reorder the records before or while you populate the table on which they are expected to appear.

另一种方法可能是为每个记录中的每个字段名称附加一个值,可以在检索后对它们进行排序,然后在显示之前从字段名称中删除排序值。

Another approach might be to append a value to each field name in each record which can be used to order them after retrieval and then, before display, removing the ordering value from the field name.

因此在将结果转换为JSON后在服务器上,为第一个字段名称添加aa_前缀,将每个后续字段名称的前缀增加到zz_ 。因此,如果前两个字段名称是Name和Address,则变为aa_Name和ab_Address。

So on the server after converting the result into JSON, add a prefix from "aa_" for the first fieldname, incrementing the prefix for each succeeding field name up to "zz_". So if the first two field names are "Name" and "Address" then become "aa_Name" and "ab_Address".

然后就在将记录添加到表中之前客户端,按字段名称对字段进行排序,然后使用 fieldName = fieldName.substring(3)删除前缀。

Then just before adding the record to the table at the client, sort the fields by field name and then remove the prefix using fieldName=fieldName.substring(3).

这篇关于Jquery ajax返回已排序的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆