Cakephp, how to create grid and list view using pagination? - php

What i intend to do is change view(grid or list) with cakephp.
So what i've done was this:
$( document ).ready(function() {
$(".lista_options #gridview").click(function(){
$( "#content" ).addClass( 'cubos' );
});
$(".lista_options #listview").click(function(){
$( "#content" ).removeClass( 'cubos' );
});
});
The problem here is that i have a paginator, and for that reason when i change page to get more results the view that i choose doesn't stays. My question how can i change my view mode and be constant during all pagination?
Any suggestion is good. Thanks in advance.

Share your changes using cookies and (or) sessions. When you click to switch between list and grid, save changes to the cookie, on page load read the cookie and uses its value.
EDIT: update code
use this jquery plugin
in your javascript add:
$( document ).ready(function() {
$(".lista_options #gridview").click(function(){
$( "#content" ).addClass( 'cubos' );
$.cookie.raw = true;
$.cookie('CakeCookie[switch]', 'grid', { expires: 7, path: '/' });
});
$(".lista_options #listview").click(function(){
$( "#content" ).removeClass( 'cubos' );
$.cookie.raw = true;
$.cookie('CakeCookie[switch]', 'list', { expires: 7, path: '/' });
});
});
AppController::beforeFilter();
if($this->Cookie->check('switch')){
$this->set('style', $this->Cookie->read('switch'));
}
index.ctp
<div class="<?php if($style == 'grid'){ ?> cubos <?php } ?>" > ...

Related

trying to load php only once with jquery

I have Jquery set up so when I click on a button it loads in the PHP from another page. Currently it loads this every time I click the button. I would like to have it so it only loads the content once.
Here is my code:
//initially hides all containers
$( document ).ready(function() {
$('.aboutcontainer,.contactcontainer,.homecontainer,.portfoliocontainer,.musiccontainer').hide();
});
//home page must show as first page on arrival
$( document ).ready(function () {
$(".homecontainer").show();
});
//load about page
//button to start animation
$( document ).ready(function() {
$('#about').click(function () {
//fades unwanted pages
$('.homecontainer,.musiccontainer, .portfoliocontainer, .contactcontainer').fadeOut({queue: false, duration: 700});
//animates up unwanted pages
$('.homecontainer, .musiccontainer, .portfoliocontainer, .contactcontainer').animate({
'margin-Top' : "-1000px",
},400, 'easeInBack');
//brings in selected page with a 0.7 second pause
setTimeout(function() {
$('.aboutcontainer ').fadeIn({
queue: false,
duration: 100
});
$(".aboutcontainer").animate({
marginTop: "115px",
},1200,'easeOutQuint');
}, 700 );
//loads in selected page content
setTimeout(function() {
$( ".aboutcontainer" ).load( "about.php" );
}, 1200 );
});
});
Also I am experiencing difficulty with the page not loading the change page functionality when I open it in incognito mode.
Here is a link to the site: http://benliger.webatu.com
Why would this be?
To allow an event handler to run only once, use .one() rather than .click() (which is a shortcut to .on("click"...).
Documentation: http://api.jquery.com/one

jQuery Autosuggest with mySQL and PHP

I'm setting up an autosuggest where a person can enter a state and get referred to a page on my website. I am linking this to a database because once I get this simple example working I will have 50,000 places in a database to look up. Right now I just have states.
Everything works good except that when a user selects a state from the list, the name filled out in the box is the url link that I am sending them too rather than the correct label. (But I am still going to send them to a url.) Its a small point but it just doesn't look right. Any ideas?
jQuery(document).ready(function($){
$('#searchbox').autocomplete({
source:'suggest.php',
minLength:1,
select: function(event, ui) {
$( "#searchbox" ).val( ui.item.label );
var url = ui.item.value;
if(url != '#') {
window.location.href = url;
}
},
html: true,
open: function(event, ui) {
$(".ui-autocomplete").css("z-index", 1000);
}
});
});
The php returns both the web link and the label being searched:
while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
{
$data[] = array(
'label' => $row['Label'],
'value' => $row['Link']
);
}
Here is the example running 1
It looks like the select method's default behaviour is to set the textbox to use value instead of label. So using $( "#searchbox" ).val( ui.item.label ); won't work, regardless of the string you put in there. In that case, we should stop the default behaviour.
Putting event.preventDefault() in the select function seems to fix the problem.
select: function(event, ui) {
event.preventDefault();
$( "#searchbox" ).val( ui.item.label );
var url = ui.item.value;
if(url != '#') {
window.location.href = url;
}
}

Pull Down To Refresh (Div)

The Question
How could I make it where when you pull <div id="content"> down, it does a function and shows a div at the top of the screen that says "Pull Down To Refresh"?
I know iScroll does this, but it won't work with what I'm trying to do. It only works with UL's.
Here is my HTML
<div id="pullDown">
<span class="pullDownIcon"></span><span class="pullDownLabel">Pull down to refresh...
</span>
</div>
<div id="content">
(There is a bunch of items that are loaded in via jquery and php, when you pull down to refresh, I want it to basically react that function.)
</div>
<script>
$(function() {
var refreshCallback = function(loader) {
setTimeout(function(){
//loader.finish();
}, 1000);
};
var cancelRefreshing = function() {
};
$("#draggable").pulltorefresh({
async: true,
// event triggered when refreshing start
refresh: function(event, finishCallback) {
element = $(this)
setTimeout(function(){
alert("refresh");
// you must call this function if refreshing process runs asynchronusly
finishCallback();
}, 1000);
},
abort: function() {
alert("abort");
}
});
});
</script>
for full Demo Click Here
You can provide with iScroll plugin.
iScroll 4: http://cubiq.org/iscroll-4
Github Page: https://github.com/cubiq/iscroll
Demo: http://cubiq.org/dropbox/iscroll4/examples/pull-to-refresh/
try this
$( "#pullDown" ).on( "dragstart", function( event, ui ) {} );
inside the function u can have the ajax call to load the content
$("#content").load("<something.php>");
that is on the whole
$( "#pullDown" ).on( "dragstart", function() {
$("#content").load("<something.php>");
} );

codeigniter - how to display a popup form in a div tag using jquery

I'd like have a button on my page that display a form (in a div) which includes text box and two buttons.
I'm not too familiar yet with jquery but I did do some reading and have tried to base my code on an example found here at: http://jqueryui.com/demos/dialog/#modal-form
I have a button that looks like:
<input type='button' class='btn btn-info' value='Change VLAN' id='changevlan'/>
and a div that contains the details of the pop form that I want:
<div id="dialog-form" title="Change Vlan">
<p>You must supply a new vlan number</p>
<form>
<fieldset>
<label for="vlan">Vlan Number:</label>
<input type="text" name="vlan" id="vlan" class="text ui-widget-content ui-corner-all" />
</fieldset>
</form>
</div>
And then at the bottom of my page, i have included the jquery and jquery ui libraries (I loaded at bottom for faster page loading) and I have the following javascript: (I based it off the example i mentioned at the beginning of this post.)
<script type="text/javascript">
$(function() {
// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
$( "#dialog:ui-dialog" ).dialog( "destroy" );
var vlan = $( "#vlan" ),
tips = $( ".validateTips" );
function updateTips( t ) {
tips
.text( t )
.addClass( "ui-state-highlight" );
setTimeout(function() {
tips.removeClass( "ui-state-highlight", 1500 );
}, 500 );
}
function checkLength( o, n, min, max ) {
if ( o.val().length > max || o.val().length < min ) {
o.addClass( "ui-state-error" );
updateTips( "Length of " + n + " must be between " +
min + " and " + max + "." );
return false;
} else {
return true;
}
}
function checkRegexp( o, regexp, n ) {
if ( !( regexp.test( o.val() ) ) ) {
o.addClass( "ui-state-error" );
updateTips( n );
return false;
} else {
return true;
}
}
$( "#dialog-form" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Change Vlan": function() {
var bValid = true;
allFields.removeClass( "ui-state-error" );
bValid = bValid && checkLength( vlan, "vlan", 1, 1 );
bValid = bValid && checkRegexp( vlan, /^([0-9])+$/i, "vlan must be numeric." );
if ( bValid ) {
//this is where I need to redirect to another URL passing the vlan number.
$( this ).dialog( "close" );
}
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
}
});
$( "#changevlan" )
.button()
.click(function() {
alert('in here');
$( "#dialog-form" ).dialog( "open" );
});
});
</script>
As you can see I inserted an alert statement to ensure that i was getting into the javascript. But I don't get the alert message. And the contents of the div form just show up on the main page when it's initially rendered.
What I've checked so far:
I've tried to double check that the name of my button that should trigger the div form display matches the name of the variable i'm looking for in the javascript function:
$( "#changevlan" )
.button()
.click(function() {
alert('in here');
$( "#dialog-form" ).dialog( "open" );
});
I've also made sure that my div form is called #dialog-form
But I'm not too sure what else to try. If you ahve any suggestions, I'd appreciate it. I also need to know how to access the contents of the text box and then redirect the user to a new controller and method, passing the vlan number as a parameter.
Maybe I can just do a window.location type of thing...
Thanks.
u dont need more libraries, just make a simple jQueryUI .dialog, inside the "div" element for the dialog place some inline php from CI, something like <div id="dlgPopUpForm"><?= $this->load->view('dialogView.php'); ?></div>, then build your form in the dialogView.php of course.
At run time, the php wil be read into the div and the jquery options u use to create the dialog will handle the rest. something like $("#dlgPopUpForm").dialog({ autoOpen: false, modal: true }); $("#someButton").click(function(e){ $("#dlgPopUpForm").dialog("open"); });
dont forget to include your javascript for the form on the form view page (dialogView.php in my example)
maybe when i'm not so lazy, i'll make ya an example later today, though, its pretty straight forward

Loading the result of a PHP script into a div from a link with JQuery

I have been trying to load the content of a link to a div element without reloading the page. I found this solution 'http://wecodesign.com/demos/stackoverflow-7071545.htm' and have tried it but I couldn't get it to work.
What I am doing is exactly the same, but for some reason it doesn't work.
This is the Jquery part:
<script type="text/javascript">
function updateContainer( url ){
dynamicCon = '#mainContent';
ObjTag = $( dynamicCon );
ObjTag.load( url );
}
$( document ).ready( function() {
$( 'a[rel="dynamicLoad"]' ).bind( "click", function( event ) {
url = $( this ).attr( 'href' );
updateContainer( url );
event.preventDefault();
event.stopPropagation();
});
});
</script>
And this is the PHP bit I use to output the HTML:
echo '<li><a href = "/getPost.php?q='. $row['id'] .' " rel="dynamicLoad" >'. $row['title'] .'</a> </li> <br>';
But this sends me to the link instead of loading it into the div. The div I want the content to load into is called "mainContent". Now, I can't say I know JQuery but everything looks OK to me but obviously I am missing something.
UPDATE: I tried linking to a later library, restart the server, cleared the browser cache and changed the Javascript to:
function updateContainer( url ){
dynamicCon = '#mainContent';
ObjTag = $( dynamicCon );
$("#mainContent").load(url)
}
$( document ).ready( function() {
$( 'a[rel="dynamicLoad"]' ).bind( "click", function( event ) {
url = $( this ).attr( 'href' );
updateContainer( url );
event.preventDefault();
event.stopPropagation();
});
});
Now, when I try to load a link the mainContent div becomes empty (certainly an improvement considering the situation)
UPDATE 2: I uploaded the thing on the net if you want to poke around. electricbrownie.net
Well the answer is simple. In your generated urls remove trailing spaces and it will work :)
echo '<li><a href = "/getPost.php?q='. $row['id'] .'" rel="dynamicLoad" >'. $row['title'] .'</a> </li> <br>';
it was <a href = "/getPost.php?q='. $row['id'] .' " should be <a href = "/getPost.php?q='. $row['id'] .'"
I am pretty sure it is because the default functionality of the <a> element is to send you to the href attribute. Just try this to see if it works:
<script type="text/javascript">
$( document ).ready( function() {
$( 'a[rel="dynamicLoad"]' ).each(function(index){
$(this).click(function(){
url = '/getPost.php?q='+$(this).attr('id');
$('#mainContent').load(url);
});
});
});
</script>
<?php
...
echo '<li><a id="'.$row['id'].'" href = "#" rel="dynamicLoad" >'. $row['title'] .'</a> </li> <br>';
?>
$( 'a[rel="dynamicLoad"]' ).bind( "click", function( event ) {
var url = $( this ).attr( 'href' );
$.get(url, function(data) {
$('#mainContent').html( data);
}):
event.preventDefault();
});

Categories