views:

323

answers:

2

I need to be able to have the displayed table change based on the drop down menu item selected. I have no idea what to write in javascript. The code I have so far is:

JS:

//change table viewed

$("#view").change( function (event) {
$.getJSON('view.php?view=' + $(this).val(), function (json) { 


});
});

HTML:

                <div>
             View: <select id="view"><option value="failed">Failed</option><option value="pending">Pending</option><option value="both">Both</option></select>
    <table id="jobs-failed" border="1">
    <tr><th>ID</th><th>Media</th><th>Playlist</th><th>Input</th><th>Output</th><th>Bit Rate</th><th>Status</th><th>Encoder</th><th>Progress</th></tr>
    <tr><td>2851</td><td>1849474</td><td>367</td><td>txt</td><td>html</td><td>best</td><td>failed</td><td>encvm2</td><td>NULL</td></tr>
    </table>
    <table id="jobs-pending" border="1">
    <tr><th>ID</th><th>Media</th><th>Playlist</th><th>Input</th><th>Output</th><th>Bit Rate</th><th>Status</th><th>Encoder</th><th>Progress</th></tr>
    <tr><td>2344</td><td>1843278</td><td>455</td><td>mp3</td><td>wav</td><td>best</td><td>pending</td><td>encvm1</td><td>NULL</td></tr>
    </table>
    <table id="jobs-both" border="1">
    <tr><th>ID</th><th>Media</th><th>Playlist</th><th>Input</th><th>Output</th><th>Bit Rate</th><th>Status</th><th>Encoder</th><th>Progress</th></tr>
    <tr><td>2851</td><td>1849474</td><td>367</td><td>txt</td><td>html</td><td>best</td><td>failed</td><td>encvm2</td><td>NULL</td></tr>
    <tr><td>2344</td><td>1843278</td><td>455</td><td>mp3</td><td>wav</td><td>best</td><td>pending</td><td>encvm1</td><td>NULL</td></tr>
    </table>
    </div>

Thanks!

A: 

Are you rendering the table on the client from JSON? Of not, then you need to change the event to call this:

$('#id_of_some_table_wrapper').load('view.php?view=' + $(this).val());
Josh Pearce
I am rendering the tables in html. Also, I should have added that I have NEVER worked with JQuery/Json in the past.
If you are rendering the table dynamically each time the user changes the select list, then you need to create a new page on your server that just displays the table, no additional html, and call that page with the proper query, to replace the current table. Otherwise, if all the tables are already in your page, then you just need to show and hide the proper ones using jquery. Go check out the jquery does for things like simple AJAX or DOM manipulation.
Josh Pearce
+1  A: 

This feels like a "teach me how to program" question (as opposed to a "how do I solve this one specific programming problem" question), so I'm going to be a little terse, but ...

$("#view").change( function (event) {

You've got that part right.

$.getJSON('view.php?view=' + $(this).val(), function (json) {

As Josh pointed out, this is wrong unless you are using AJAX. Based on your comment in Josh's answer I'm going to assume that's not the case; ie. you have some content ready to be swapped in already there on the page. In this case, doing the swapping is as simple as:

function doSwap(id) {
  // This is just one way of doing the swap; see jQuery Manipulation docs for others
  var content = $("#" + id).html();
  $("table#jobs-failed").html(content);
  // repeat for your other tables
}
$("#view").change( function (event) {
  var $target = $(event.target);
  var whatTheySelected = $target.val();
  if (whatTheySelected == case1) {
    doSwap("content1");
  } else if (whatTheySelected == case2) ...

If you don't have the content (you just have JS variables with the data or something) you'll need to make the elements. Instead of doing:

  var content = $("#" + id).html();

do:

  var content = $("<tr><td>" + yourVar + "</td><td>" + yourVar2 +"</td></tr>");

Hope that helps.

machineghost
Thanks for the help! And yes it is a bit more of a teach me how to code web based applications as I am used to working in c#, c++, java, and python.
Glad I could help :-)
machineghost