I'm building my own wordpress template and basically what I want is the abbility to switch (back and forth) between two view modes on a category page without a refresh.
PHP code is executed once a page is loaded, you're not going to be able to include php loops within javascript.
What you could do is execute both loops within the page. So the one list will be ontop of the other list. You could then wrap both lists in a div, for example: HTML:
<a href="#" id="switch">Click here to switch between list and grid</a> <div id="grid"> // grid wordpress loop here </div> <div id="list"> // list wordpress loop here </div>
By clicking on the anchor link, you will be hiding the grid and showing the list. When you click again, you will show the grid and hide the list - Which seems to be what you were looking for.
jama_za is spot on. That is the smartest way to do it.
Alternatively you could store it in two separate files, and load the content into your div on click on your anchor link. However, that would be less desireable.
The site you linked to refreshes the whole page when you change between the two views. At least here :-)
jQuery explanation: If you click an anchor link with the class of toggle, take it's ID, remove the first 7 characters (the first 7 are 'toggle-'). Toggle an ID of this new found value.
ok that's not entirely what I ment... sure now I can toggle using two links but now I can also see them both, none or one of the two. So if I toggle the list div, I want the grid div to automatically hide (or vice versa).
If I do something like this, both links have the same functionality, that just doesn't make sense ;-)
Not sure why one would build the two loops upfront, its unnecessary bandwidth, the visitor might never switch between the two. The code below will only build the alt loop if required.
grid.php:
<?php echo "grid loop"; ?>
list.php:
<?php echo "list loop"; ?>
switcher.js:
$(document).ready(function(){ $('#toggle-grid').click(function(){ // hide both loops $(".view").hide(); // check if loop aleady has been generated if ($("#grid").html() != "") { $("#grid").show(); } else { // otherwise fetch loop $.get("grid.php", function(data) { $("#grid").html(data).show(); }); } }); $('#toggle-list').click(function(){ // hide both loops $(".view").hide(); // check if loop aleady has been generated if ($("#list").html() != "") { $("#list").show(); } else { // otherwise fetch loop $.get("list.php", function(data) { $("#list").html(data).show(); }); } }); });
Yip it would work, the php code would execute on the server side before returning a result back to the js method. The js method will wait until something gets returned.
Been using this for quite sometime now with aspx generic handlers that return full html.
Very helpful post. Would you possible know some relative module or plugin I could install to my site in order to enjoy this functionality? Thank you for your effort.
@alphakurt You're saying that you are doing the same thing, but your HTML/PHP doesn't seem like it. Explain to me exactly what you are trying to do. If you're trying to refresh a product list, there must be a .php file somewhere which contains that list.
Does it make a differenece whether it's a list or not? Basically all elements are the same thing with different names. Your site crashed my browser twice lol (After opening web-inspector)
Your Row nests start with "ListingRow" and list nests start with "Listinggrid". So you can do the same thing as in example #2.
I was wondering even though this site is done in flash I am looking for another way preferably with css, java or jquery to create this sort of a menu.. what do you think guys can this be done with css or does it need to be done in flash only? here is the subject site. http://aerostudios.com/
I'm building my own wordpress template and basically what I want is the abbility to switch (back and forth) between two view modes on a category page without a refresh.
My own mockup:
I hope it makes sense and someone can/ is willing to help me out..
Thierry
PHP code is executed once a page is loaded, you're not going to be able to include php loops within javascript.
What you could do is execute both loops within the page. So the one list will be ontop of the other list. You could then wrap both lists in a div, for example:
HTML:
CSS:
#list{display: none;}jQuery:
By clicking on the anchor link, you will be hiding the grid and showing the list. When you click again, you will show the grid and hide the list - Which seems to be what you were looking for.
i found a website that contains the same function I'm looking for at www.aisleone.net
Looking at the source they have used a different technique
Alternatively you could store it in two separate files, and load the content into your div on click on your anchor link. However, that would be less desireable.
The site you linked to refreshes the whole page when you change between the two views. At least here :-)
One last question, can I also use two links to toggle between the two? a "grid" link and a "list" link?
jQuery:
jQuery explanation:
If you click an anchor link with the class of toggle, take it's ID, remove the first 7 characters (the first 7 are 'toggle-'). Toggle an ID of this new found value.
It could be more simple if you'd prefer:
If I do something like this, both links have the same functionality, that just doesn't make sense ;-)
grid.php:
list.php:
switcher.js:
Been using this for quite sometime now with aspx generic handlers that return full html.
Would you possible know some relative module or plugin I could install to my site in order to enjoy this functionality? Thank you for your effort.
im trying to implement the above and I managed to do everything as described (allthough im not at all technical)
im also trying to implement a product list and grid display switch on my site :
http://beta.prijsjagers.be/babyfoon~1044.htm#
the problem i have is that the code is not executing..
here is my current code
currently it is displayed in a grid, I would like to have link / button to let a user switch between e grid and a list display..
I have the css for the grid and the css for the list.. but im unable to switch between one and the other
You have a div:
Add an id to that, like:
Then make the list styling dependent on the id #list
Then all we need to do is toggle that ID on the div when the button is clicked. Am I making sense?
I tried to change the code ..but nothing happens.
I have changed the following ( I didnt use pl box as this is to define the big square in which products are displayed )
and
and then changed the JS to:
You can use the same concept.
:(
to summarize
I have a div called <div class="PLBox">
in this class I have several nested div's for the grid display :
the same for the list display :
I want to be able to switch these two blocks of nested divs
I'm really not understanding what the problem is. Aren't you able to edit the HTML?
Your Row nests start with "ListingRow" and list nests start with "Listinggrid". So you can do the same thing as in example #2.
I guess I need a line by line explanation... :(
thanks already for the time you spend on this
(one of the listings is currently hidden via CSS though)
So very very basically, that's what your page looks like, right?
All I have done to that (which is different from the simplified version above) is added "parent-list" on each of those lists.