SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Object List: display and sort- How to?

    Hello, i am a javascript newbie,

    and i need some help on some web app admin interface i have the task to developp:

    i have a long list of object: books with properties like title, author, publication date, subject, similar books list with their title and date of publication.

    how do i generate the list books object after getting that list of books after retrieving it through php from a mysql database and then display that list of books in an HTML table?

    Then i would like to be able for the user of that admin system to be able to sort the list by title, author, date or even subject?

    why all of this should be done in JS instead of php, it's because i have been ordered to do dev this in JS by my boss (i ma an intern)

    i would like to have the layout of the functions i have to write to do this three things!
    1-generate javascript objects Books (list?) out of php/mysql!
    2-display the list of book in html table (one cell with title, author, publication and subject) and the second cell (lits of similar books with title and date of publication)
    3-sorting books by title, author, date or subject

    Thanks in advance

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This would be much easier if you're generating the table using PHP. Then the sorting of it is done with javascript by generating the objects using the DOM. It's easier if each cell is given a class, e.g. the cells containing the titles have the class "booktitle", the cells containing the subject have the class "booksubject", etc. Are you really sure you're supposed to generate the javascript objects using PHP? You can't create the table with PHP - it has to be javascript?

  3. #3
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Most of the code of this app is written in php and the books list etc... are stored in a mysql database and the specifications i have been given were very sharp about the part that said that everything will be done in javascript except adding a new book to the list or removing one... The rest is entirely done with javascript displaying the list of a given number of books (10), then sort those 10 by names, titles etc... all of this should be in JS... i am really new with JS as an OO language so i am quite lost with it... that's why i am trying to get help on functions generating the js list objects books, then display the list and sorting the list...

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    OK, first you need to know how object literals in JS work:
    Code:
    var obj = {
      'title': 'Reproductive Habits of Beetles',
      'subject': 'Beetles',
      'author': 'Beetleman'
    }
    It's like associative arrays in PHP. You iterate through them using for...in, similar to foreach in PHP.

    In order to create the table, you need to create a TABLE element. And then create a TBODY element inside, and then TR and TD elements inside that. For this, you need createElement. So, you need to create the TABLE and TBODY and then iterate through your objects creating TR and TD elements as you go along. Give it a go and then based on that we'll see how it goes.

  5. #5
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank u

    i'll try and do that it would be helpfull since i am not really good with JS that u give me some coding hints...
    list of object generation
    displaying the list in html table
    and then sorting them by one of their property
    Last edited by kreelin; Jul 26, 2007 at 21:32.

  6. #6
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Paris
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    OK, first you need to know how object literals in JS work:
    Code:
    var obj = {
      'title': 'Reproductive Habits of Beetles',
      'subject': 'Beetles',
      'author': 'Beetleman'
    }
    It's like associative arrays in PHP. You iterate through them using for...in, similar to foreach in PHP.

    In order to create the table, you need to create a TABLE element. And then create a TBODY element inside, and then TR and TD elements inside that. For this, you need createElement. So, you need to create the TABLE and TBODY and then iterate through your objects creating TR and TD elements as you go along. Give it a go and then based on that we'll see how it goes.
    Object literal notation representation is it what is often called JSON?
    On a PHP/Java/XML/Javascript/MySQL internship right now!

  7. #7
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's pretty much what JSON is, but also mixed with arrays. So each value in the object could also be an array:
    Code:
    var obj = {
      'title': 'Reproductive Habits of Beetles',
      'subject': ['beetles', 'reproduction', 'dung', 'javascript'],
      'author': 'Beetleman'
    }
    kreelin, I'm not going to do everything for you. The best thing is if you try for yourself and then I can help you with anything you have problems with. Those links are a good starting point and if you are stuck, have a look at the rest of the Javascript guide those links are part of. Also, I'm assuming you know what the structure of an HTML table is like. If not, just google for it and you'll find tons of help.

  8. #8
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Paris
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do you get to display with alert method one element of an associative array?
    On a PHP/Java/XML/Javascript/MySQL internship right now!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •