1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Angular JS Help

Discussion in 'Programming' started by cris02, Sep 25, 2013.

  1. #1
    Hi,

    I have a json data that i want to display in html page.
    var location_tech_ranking = [
            ["table_opening":"
                <table>
                <thead>
                  <tr>
                    <th></th>
                    <th>Account ID</th>
                    <th>Name</th>
                    <th>Company Name</th>
                    <th>Position</th>
                  </tr>
                </thead>"],
           
            ["content", [
                { "account_id":"1", "name":"John Doe", "comp_name":"Company 1","position":"Technician" },
                    { "account_id":"2", "name":"Alex Fleri", "comp_name":"Company 2","position":"Technician" },
                    { "account_id":"3", "name":"Anthony Neri", "comp_name":"Company 3","position":"Technician" },
                    { "account_id":"4", "name":"Jason Campbel", "comp_name":"None","position":"Individual Technician" }, ],
           
            ["table_closing":"
                <tfoot>
                  <tr>
                    <td colspan=\"5\">
                      <div class=\"pagination pagination-centered\">
                        <span style=\"display:block;\"><strong>Total Technician: 4</strong></span>
                        <ul>
                            <li><a href=\"#\">«</a></li>
                            <li><a href=\"#\">1</a></li>
                            <li><a href=\"#\">2</a></li>
                            <li><a href=\"#\">»</a></li>
                        </ul>
    
                      </div>
                    </td>
                  </tr>
                </tfoot>
              </table>"]
          ];
    Code (markup):
    What i want to this json data is to parse using angular js. Basically I want to render the html table with dynamic content inside a modal or a page. Is my json format correct? Anyone have an idea with this.

    Thanks
     
    Solved! View solution.
    cris02, Sep 25, 2013 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Javascript doesn't support carriage returns in strings unless you escape them with \n, javascript arrays do NOT support indexed names so almost all your [] need to be {}... and your markup is no winner either since TFOOT actually has to go BEFORE TBODY, not after (counterintuitive I realize), and really if it's going to colspan the full width why is it even in a table element in the first place?

    Of course it also feels like you're using outdated/broken methodology since it would appear you are building your elements with innerHTML instead of the DOM?
     
    deathshadow, Sep 26, 2013 IP
  3. #3
    Here, I just rewrote that as valid JSON... and it shows why even trying to pass markup in JSON is a train wreck usually not worth doing. Pass data, not markup. You want that, build it on the DOM.

    var location_tech_ranking = {
    	table_opening : '<table><thead><tr><th></th><th>Account ID</th><th>Name</th><th>Company Name</th><th>Position</th></tr></thead>',
    	content : [
    			{ account_id : '1', name : 'John Doe', comp_name : 'Company 1', position : 'Technician' },
    			{ account_id : '2', name : 'Alex Fleri', comp_name : 'Company 2', position : 'Technician' },
    			{ account_id : '3', name : 'Anthony Neri', comp_name : 'Company 3', position : 'Technician' },
    			{ account_id : '4', name : 'Jason Campbel', comp_name : 'None', position : 'Individual Technician' }
    	],
    	table_closing : '<tfoot><tr><td colspan="5"><div class="pagination pagination-centered"><span style="display:block;"><strong>Total Technician: 4</strong></span><ul><li><a href="#">«</a></li><li><a href="#">1</a></li><li><a href="#">2</a></li><li><a href="#">»</a></li></ul></div></td></tr></tfoot></table>'
    };
    Code (markup):
    again, [] is arrays, which cannot have named indexes in javascript. Only objects using {} can have names. I also pulled a bunch of unnecessary quotes, and swapped to single quotes for the string declarations so you don't have to escape the doubles.
     
    deathshadow, Sep 26, 2013 IP
    Basti likes this.
  4. cris02

    cris02 Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Thank you, i'll take note of that.
     
    cris02, Sep 29, 2013 IP
  5. diyakapoor

    diyakapoor Active Member

    Messages:
    619
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    Just looking to share something on AngularJS. http://javascript.pixelcrayons.com/kb/understanding-the-concept-of-angularjs/

    Hope this will be helpful for new learners.
     
    diyakapoor, Oct 10, 2013 IP