Monday 15 July 2013

javascript - JSON best practices: filtering nodes client side or not? -


I store many "topics" and "examples" in my database. Each example is related to a subject.

I want to show all topics in the form of a tree named ACC, except one topic (subject matter) , which I always want at the top. Although it may change later and I could not change the basic JSON output of subjects and examples from DB.

1) Does this "Anarod List" structure seem appropriate to generate a client side (or can I already ensure that the order is appropriate in json_encode () phase?) And

2) How do I filter a specific topic node by name in that case and index it first? Example

  • Example
  • Topic A
    • Example < Example
    • Example
  • Example
  • Your questions are very difficult to answer without knowing the exaclty, too!

    Still, in an attempt to answer, 1):

    • If you are in any way preparing a list in HTML on the server, and The list does not change, it is JSON & amp; Javascript? Use your server-side technology to do this.

    • If the list changes and you want to manage the sequence in the list in the few UI on the client and you have the idea of ​​accessibility: using your server side technology, your list and your Make Jason, order both sides, and then render your HTML list and pass your JSON to the customer. This will make the list available to the client without JavaScript and will provide a 'sort list' available for Javascript enabled clients.

    • If you do not have any worries for users, without javascript and need to rearrange the list, just pass your JSON data to your customer and render it Use Javascript to handle and re-handle.

    2) I'm assuming that you want to sort everything that is in an array? If they occur then I will use the Array.sort method:

    Some of the following should give you an idea of ​​how to do this:

      var Data: {"subject": "c", "example": [{"example": "example one one"}, {"example": "example one two"},]}, {"subject": "b "Example": "Example": "Example B.", {"Example": "Example B Two"},]}, {"Topic": "A", "Example": [ : "Example C A"}, {"Example": "Example C Two"},]}] Function SortType (Subject, First Image) {var sortedArray = {]; (Var i = 0; i & lt; topics.length; i ++) {if (topics [i] .opic == first itam) {// The first item specified is sortedArrepees (subject [ I]); Remove the elements from the original elements // (i, 1); break up;}} // Sort the remainder of the array topics. Sort (function (a, b) {if (atopic & lt; b.Topic) { Return -1;} If (ATPC & gt; BTOPC) {return1;} return 0;}); // While loop and each item (var i = 0; i & lt; topics.length; i ++) Add to new array for {sortedArray.push (topics [i]);} Return sorted Ray;} Sorttepiks (data, "C");  

    I'm not sure how the cross browser will be, and obviously you will need to prepare a list from JSON but it should start somewhere, you can make it even more common and efficient !

    Good luck.


    No comments:

    Post a Comment