使用javascript进行JSON列表排序 [英] JSON list sorting with javascript

查看:78
本文介绍了使用javascript进行JSON列表排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我正在努力解决这个问题并且可以提供一些建议。我做了一些搜索,并没有找到我正在寻找的所以我认为这是下一站。



删除不重要的部分后我的代码看起来像这样。 Ajax转到我的服务器端GetProducts函数,它返回一个序列化的JSON字符串。这是一个从SQL数据库中填充Select语句的项目列表。

 $。ajax({
type:' POST'
url:' Answer.aspx / GetProducts'
data: JSON .stringify({id:ID}),
contentType:' application / json; charset = utf-8'
dataType: ' json'
成功: function (msg){

document .getElementById( hfProducts)。value = msg.d;

document .getElementById( <跨度class =code-string> ulList)。innerHTML = ;
var data = eval ' (' + document .getElementById( hiddenfieldOfProducts)。value + ' )'< /跨度>);

$ .each(data, function (i,item){
var message = < li> +
< div style = + width:100%; + > +
item.ProductName + £ + item.ProductPrice +
< / div> +
< / li>;

document .getElementById( ulList)。innerHTML + = message;
var newcontent = document .createElement(' li');
});
}
});





在我开始显示列表中的项目之前的某个时刻很长的无组织列表。现在我想根据产品的类别拆分项目并对数据进行排序,这样输出就像这样,粗体文本分类:



食物:

鸡肉馅饼£4.00

烤肉晚餐£7.00

辣椒和米饭£5.00 < br $> b $ b

沙漠

苹果派£2.00

冰淇淋£3.00


KitchenWare

Wok£15.00

Pan£10.00



你得到了这个想法,目前他们按照他们根据唯一ID添加到数据库的顺序来完成,我想根据item.Category将它们拆分并像上面一样显示它们在该部分上面的类别标题。



我可以就我在这里做什么得到一些建议吗?我今天花了好几个小时试图解决这个问题但没有取得多大成功。由于C#不允许你在return语句中同时返回多个列表而且我不想诉诸大量重复代码,我需要有更多经验的人的建议。#



希望我很清楚,先谢谢。

解决方案

.ajax({
type:' POST'
url:' Answer.aspx / GetProducts'
data: JSON .stringify({id:ID }),
contentType:' application / json; charset = utf-8'
dataType:' json'
成功:功能(msg){

document .getElementById ( hfProducts)。value = msg.d;

document .getElementById( ulList)。innerHTML = ;
var data = eval ' (' + document .getElementById( hiddenfieldOfProducts)。value + ' )');


.each(data, function (i,item){
var message = < li> +
< div style = + width:100%; + > ; +
item.ProductName + £ + item.ProductPrice +
< / div> +
< / li> ;

document .getElementById( ulList)。innerHTML + = message;
var newcontent = document .createElement(' li');
});
}
});





在开始显示List项目之前的某个时刻unorganized list.Now我想分割项目并根据产品的类别对数据进行排序,所以它会像这样输出,粗体文本类别:



食物:

鸡肉馅饼£4.00

烤肉晚餐£7.00

辣椒和米饭£5.00



沙漠

苹果派£2.00

冰淇淋£3.00



KitchenWare

Wok£15.00

Pan£10.00



你明白了,目前他们按照他们被添加到数据库的顺序来完成关闭唯一ID,我想根据item.Category将它们拆分并像上面那样用上面的类别标题显示它们。



我能得到一些吗?关于我在这做什么的建议?我今天花了好几个小时试图解决这个问题但没有取得多大成功。由于C#不允许你在return语句中同时返回多个列表而且我不想诉诸大量重复代码,我需要有更多经验的人的建议。#



希望我很清楚,先谢谢。


下面的元素有什么价值,因为我很困惑

- hfProducts

- hiddenfieldOfProducts


Hey guys, Im struggling with this problem and could do with some advice. Ive done some searching and havent found what im looking for so I figured this was the next stop.

After removing the unimportant parts my code looks something like this. the Ajax goes to my serverside GetProducts function which returns a serialized string of JSON. Which is a List of items which a Select statement filled from a SQL database.

$.ajax({
        type: 'POST',
        url: 'Answer.aspx/GetProducts',
        data: JSON.stringify({ id: ID }),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {

        document.getElementById("hfProducts").value = msg.d;

        document.getElementById("ulList").innerHTML = "";
        var data = eval('(' + document.getElementById("hiddenfieldOfProducts").value + ')');

        $.each(data, function (i, item) {
        var message = "<li>" + 
                           "<div  style=" + " width:100%;" + ">" +
                           item.ProductName+"  £" + item.ProductPrice+
                            " </div> " + 
                      "</li>  ";

         document.getElementById("ulList").innerHTML += message;
         var newcontent = document.createElement('li');
          });
        }
 });



At some point in the process before I start displaying the List items in a long unorganised list.Now I want to split up the items and sort the data based off the Category of the product so It would output like this, with the bold text the category:

Food:
Chicken pie £4.00
Roast Dinner £7.00
Chilli and rice £5.00

Desert
Apple pie £2.00
Ice cream £3.00

KitchenWare
Wok £15.00
Pan £10.00

You get the idea, currently they come through in the order they were added to the database based off the unique ID, I want to split them up based off the item.Category and display them like above with the Category title above that section.

Can I get some advice on what im doing here? I''ve spent hours today trying to work on this problem without much success. Since C# doesn''t allow you to return multiple lists at the same time in a return statement and I don''t want to resort to a lot of repeating code I need advice from someone with more experience.#

Hopefully I was clear enough, Thanks in advance.

解决方案

.ajax({ type: 'POST', url: 'Answer.aspx/GetProducts', data: JSON.stringify({ id: ID }), contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (msg) { document.getElementById("hfProducts").value = msg.d; document.getElementById("ulList").innerHTML = ""; var data = eval('(' + document.getElementById("hiddenfieldOfProducts").value + ')');


.each(data, function (i, item) { var message = "<li>" + "<div style=" + " width:100%;" + ">" + item.ProductName+" £" + item.ProductPrice+ " </div> " + "</li> "; document.getElementById("ulList").innerHTML += message; var newcontent = document.createElement('li'); }); } });



At some point in the process before I start displaying the List items in a long unorganised list.Now I want to split up the items and sort the data based off the Category of the product so It would output like this, with the bold text the category:

Food:
Chicken pie £4.00
Roast Dinner £7.00
Chilli and rice £5.00

Desert
Apple pie £2.00
Ice cream £3.00

KitchenWare
Wok £15.00
Pan £10.00

You get the idea, currently they come through in the order they were added to the database based off the unique ID, I want to split them up based off the item.Category and display them like above with the Category title above that section.

Can I get some advice on what im doing here? I''ve spent hours today trying to work on this problem without much success. Since C# doesn''t allow you to return multiple lists at the same time in a return statement and I don''t want to resort to a lot of repeating code I need advice from someone with more experience.#

Hopefully I was clear enough, Thanks in advance.


What are the values in follwoing elements, because I''m confused
- hfProducts
- hiddenfieldOfProducts


这篇关于使用javascript进行JSON列表排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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