简体   繁体   English

JSON最佳实践:是否在客户端过滤节点?

[英]JSON best practices: filtering nodes client side or not?

In my database I store a number of "topics" and "examples". 在我的数据库中,我存储了许多“主题”和“示例”。 Each example belongs to one topic. 每个示例都属于一个主题。

I'd like show all the topics by name ASC in a tree component except for one topic (Topic C) which I always want on top. 我想在树组件中按名称ASC显示所有主题, 我始终希望在其中一个主题(主题C)除外 This may though come to change later and I'd rather not change the basic JSON output of topics and examples from the DB. 虽然这可能以后会更改,但我宁愿不更改数据库中主题和示例的基本JSON输出。

1) Does it seem reasonable to generate the "unordered list" structure client side (or should I, already in the json_encode() phase make sure the order is appropriate?) and 1)生成客户端的“无序列表”结构似乎合理(或者我应该已经在json_encode()阶段中确保该顺序合适吗?)并且

2) how would I, in that case, filter one specific topic node by name and put it first in the sequence? 2)在这种情况下,我将如何按名称过滤一个特定的主题节点并将其放在序列的第一位?

  • Topic C 主题C
    • Example
    • Example
  • Topic A 话题A
    • Example
    • Example
  • Topic B 话题B
    • Example
    • Example

Your questions are very difficult to answer without knowing exaclty the reasons you're doing this! 如果不确切知道这样做的原因,很难回答您的问题!

Anyway, in attempting to answer, 1) : 无论如何,在尝试回答时,1):

  • If you are generating a list in HTML on the server anyway, and the order of the list doesn't change, why do it with JSON & JavaScript? 如果您仍在服务器上用HTML生成列表,并且列表的顺序不变,那么为什么要使用JSON和JavaScript? Use your server side tech to do this. 使用服务器端技术来执行此操作。

  • If the list will change and you want something in the UI on the client to manage the order of the list and you have accessibility considerations: create your list and your JSON using your server side tech, order both server side and then render your HTML list and pass your JSON to the client. 如果列表将更改,并且您希望客户端的UI中的某些内容可以管理列表的顺序,并且您具有可访问性注意事项:请使用服务器端技术创建列表和JSON,同时对服务器端进行排序,然后呈现HTML列表并将您的JSON传递给客户端。 This will make the list available to client (user) without JavaScript enabled and the 'sortable list' available to JavaScript enabled clients. 这将使列表对未启用JavaScript的客户端(用户)可用,而“可排序列表”对启用JavaScript的客户端可用。

  • If you don't have any concern for users without JavaScript AND the list needs to be reorderable then just pass your JSON data to your client and use JavaScript to render it and handle the reordering. 如果您对没有JavaScript的用户不感兴趣,并且列表需要重新排序,则只需将JSON数据传递给客户端,然后使用JavaScript呈现它并处理重新排序即可。

2) I'm assuming that each of your objects that you want to sort is in an array? 2)我假设您要排序的每个对象都在一个数组中? If they are then I would use the Array.sort method : www.w3schools.com/jsref/jsref_sort.asp 如果是这样,我将使用Array.sort方法: www.w3schools.com/jsref/jsref_sort.asp

Something like the following should give you an idea of how to go about doing this: 类似于以下内容的内容应使您了解如何执行此操作:

       var data = [{
    "Topic" : "C",
    "Examples":[
     {"Example" : "Example A one"},
     {"Example" : "Example A two"},
    ] 
   },{
    "Topic" : "B",
    "Examples":[
     {"Example" : "Example B one"},
     {"Example" : "Example  B two"},
    ] 
   },{
    "Topic" : "A",
    "Examples":[
     {"Example" : "Example C one"},
     {"Example" : "Example C two"},
    ] 
   }];

   function sortTopics(topics, firstItem){
    var sortedArray = []; 

    for (var i = 0; i < topics.length; i++) {
     if (topics[i].Topic == firstItem) {
      //add the specified first item
      sortedArray.push(topics[i]);
      //remove element from original
      topics.splice(i,1);
break;
     }

    }

    //sort the remainder of the array
    topics.sort(function(a,b){
     if(a.Topic < b.Topic){return -1;}
     if(a.Topic > b.Topic){return 1;}
     return 0;
    });

    //loop though and add each item to the new array          
    for (var i = 0; i < topics.length; i++) {
     sortedArray.push(topics[i]);
    }

    return sortedArray;
   }

   sortTopics(data, "C");

I'm not sure how cross browser it would be and obviously you'll need to generate the list from the JSON but it should give you somewhere to start, you could make it all more generic and efficient of course! 我不确定这将是多么的跨浏览器,显然您需要从JSON生成列表,但是它应该给您一个开始的地方,您当然可以使其变得更加通用和高效!

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM