dimanche 28 juin 2015

How to organize objects by their attributes in an AJAX function

Please excuse me if this seems elementary ive been stuck on this for weeks. If you are using an AJAX success function to display data dynamically on your html page, and the data you are getting back from the server is in stdclass objects like so

    array( 
       [7] => stdClass Object
        (
       [id] => 3
       [title] => Electrition
       [img] => 
       [description] => 
       [school] => 
       [location] => 1
       [url] => 
       [tablename] => 3
       [votes] => 0
       [name] => John Doe
       [NumJobsdone] => 4
           )

         [8] => stdClass Object
       (
        [id] => 2
        [title] => Electrition
        [img] => 
        [description] => 
        [school] => 
        [location] => 1
        [url] => 
        [tablename] => 2
        [votes] => 0
        [name] => Tico Marinez
        [NumJobsdone] => 6
        )

        [9] => stdClass Object
        (
        [id] => 2
        [title] => Engineer
        [img] => 
        [description] => 
        [school] => 
        [location] => 1
        [tablename] => 2
        [votes] => 0
        [name] => Jerry Smity
        [NumJobsdone] => 6
       )

     [10] => stdClass Object
       (
       [id] => 2
       [title] => Engineer
       [img] => 
       [description] => 
       [school] => 
       [location] => 1
       [url] => 
       [tablename] => 2
       [votes] => 0
        [name] => Laura Bastian
       [NumJobsdone] => 6
         )
        ) 

how do you write a jquery function that can iterate through the data and create paragraph tags to label the output and an unordered list within a div of the corresponding objects?

ex. you are trying to create two groupings, electritions and engineers, and list the names of the individuals.

it seems simple but when i used a $.each method in my success function, i created a paragraph tag for every object that had the attribute i was organizing the objects by, and when i tried to use the sort method, i could only compare two or three objects at a time and not iterate through a long list of several objects.

say the desired output looked something like this

     <div id="accordion">
        <p> Electrition</p>
        <div>
        <ul>
          <li>jon</li>
          <li>jill</li>         
       </ul>
       </div>

        <p>Engineer</p>
       <div>
        <ul>
          <li>jerry</li>
          <li>laura</li>
       </ul>
      </div>
      </div>

Aucun commentaire:

Enregistrer un commentaire