I have a mysql database and I am using jquery to do a live search which is working like a charm. But I have tried alot of ways to have a pagenation for my search results.
Below is my jquery code
$(document).ready(function(){
/*
Set the inner html of the table, tell the user to enter a search term to get started.
We could place this anywhere in the document. I chose to place it
in the table.
*/
$.get('data/search-data.php', function(returnData) {
$('#results').html(returnData);
});
/* When the user enters a value such as "j" in the search box
* we want to run the .get() function. */
$('#searchData').keyup(function() {
/* Get the value of the search input each time the keyup() method fires so we
* can pass the value to our .get() method to retrieve the data from the database */
var searchVal = $(this).val();
/* If the searchVal var is NOT empty then check the database for possible results
* else display message to user */
if(searchVal !== '') {
/* Fire the .get() method for and pass the searchVal data to the
* search-data.php file for retrieval */
$.get('data/search-data.php?searchData='+searchVal, function(returnData) {
/* If the returnData is empty then display message to user
* else our returned data results in the table. */
if (!returnData) {
$('#results').html('<p style="padding:5px;">Search term entered does not return any data.</p>');
} else {
$('#results').html(returnData);
}
});
} else {
$.get('data/search-data.php?searchData='+searchVal, function(returnData) {
$('#results').html(returnData);
});
}
});
});
A simple solution, without delving into javascript object and/or templating is returning the pages from the server like this:
<div data-page="1" class="results">
<ul>
<li>Result 1</li>
<li>Result 2</li>
</ul>
</div>
<div data-page="2" class="hidden results">
<ul>
<li>Result 3</li>
<li>Result 4</li>
</ul>
</div>
<div data-page="3" class="hidden results">
<ul>
<li>Result 4</li>
<li>Result 5</li>
</ul>
</div>
<div id="pager"></div>
then create the pager links with jquery on the ajax callback:
var links = [];
$('.results').each(function(i) {
var link = $('<a />', {
text: i+1,
href: '#'
});
links.push(link);
});
$('#pager').html(links);
then a click handler will toggle the display of the pages:
$('#pager').on('click', 'a', function() {
var i = $(this).index();
$('.results').eq(i).removeClass('hidden').siblings('.results').addClass('hidden');
return false;
});
DEMO: http://jsfiddle.net/Ry7NJ/1/
Related
Using JQuery I want to check that each list item has the css class Complete. If so, i want to display a button, if not I want to keep the button hidden.
The classes are currently inserted dynamically using PHP when the page loads.
My code so far is;
<button id="steps-complete" hidden>Download Now</button>
<ul class="wb-steps">
<li class="<?php echo $class ?>">Step 1</li>
<li class="<?php echo $class ?>">Step 2</li>
<li class="<?php echo $class ?>">Step 3</li>
</ul>
<script>
jQuery( document ).ready(function() {
var steps = [];
jQuery( ".wb-steps li" ).each(function( index ) {
// successfully displays complete for each list item
console.log( index + ": " + jQuery( this ).attr('class') );
// if all stepa have 'complete' show button
$("#steps-complete").show();
});
});
</script>
You have the $class variable, and its value is set as the class of all list items.
Since you are using PHP to render the page, you can use the same variable to test if the button should be shown or not. So you wouldn't need jQuery in this case, you can just remove that part.
Here's what it would look like:
<?php
if (strpos($class, 'Completed') !== false) {
?>
<button id="steps-complete" hidden>Download Now</button>
<?php
}
?>
This works in your case because you set the same class to all items.
Hope this helps.
i've made a function to check of the children of the wb-steps have a class named john, if not then we show the steps complete
jQuery( document ).ready(function() {
if($(".wb-steps").children().not('.john').length = 0){
console.log("there was a div found with a class named john");
}else{
$("#steps-complete").show();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="steps-complete" hidden>Download Now</button>
<ul class="wb-steps">
<li class="<?php echo $class ?>">Step 1</li>
<li class="<?php echo $class ?>">Step 2</li>
<li class="<?php echo $class ?>">Step 3</li>
</ul>
You can simply count the elements to see if all of them have the complete class:
jQuery(document).ready(function() {
var $steps = $(".wb-steps li");
var show = $steps.length === $steps.filter('.complete').length;
$("#steps-complete")
.toggle(show)
.get(0)
.toggleAttribute('hidden', show);
});
hi i am using jquery ui sortable to sort my divs . i can get the div oder to save in the db , this is my code
<div style="margin:0 auto !important;">
<ul id="sortable1" class="connectedSortable">
<li id="one_1" class="ui-state-default"><div>Item 1</div></li>
<li id="one_2" class="ui-state-default"><div>Item 2</div></li>
<li id="one_3" class="ui-state-default"><div>Item 3</div></li>
<li id="one_4" class="ui-state-default"><div>Item 4</div></li>
<li id="one_5" class="ui-state-default"><div>Item 5</div></li>
</ul>
<ul id="sortable2" class="connectedSortable">
<li id="two_1" class="ui-state-highlight"><div>Item 1</div></li>
<li id="two_2" class="ui-state-highlight"><div>Item 2</div></li>
<li id="two_3" class="ui-state-highlight"><div>Item 3</div></li>
<li id="two_4" class="ui-state-highlight"><div>Item 4</div></li>
<li id="two_5" class="ui-state-highlight"><div>Item 5</div></li>
</ul>
</div>
<script type="text/javascript">
jQuery(function() {
var sortable1 = 'one_1,one_2,one_3,one_4,one_5';
var sortable2 = 'two_1,two_2,two_3,two_4,two_5';
jQuery( "#sortable1, #sortable2" ).sortable({
update: function(event, ui) {
var newOrder = jQuery(this).sortable('toArray').toString();
if(jQuery(this).attr('id')=="sortable1"){
sortable1 = newOrder;
}
else {
sortable2 = newOrder;
}
console.log(sortable1);
console.log(sortable2);
}
}).disableSelection();
});
</script>
i can save the sorted div order to DB correctly , but i have no idea how to populate the div's to correct order again . please help me . thanks in advance .
Instead of saving the sort order to the DB as the comma separated value 1,2,3,4,5, why not just save the 'sort column' e.g. #sortable1 such that when you are loading the divs you just pass this value to your js logic.
Example:
if you save the sort column name say #sortable1 or #sortableN in the DB then you end up with
var newOrder = jQuery(this).sortable('toArray').toString();
if(jQuery(this).attr('id')== "<% $your_php_variable %>"){
sortable1 = newOrder;
}
else {
sortable2 = newOrder;
}
Am not sure how you are passing the php variable to the UI that's what i've put as <% $your_php_variable %>. At some point in your php code you will have set $your_php_variable = '#sortableX';
I have two lists
<ul class="sortable" id="list-A">
<li id="1">Value 1 </li>
<li id="2">Value 2 </li>
<li id="3">Value 3 </li>
</ul>
<ul class="sortable" id="list-B">
<li id="4">Value 1 </li>
<li id="5">Value 2 </li>
<li id="6">Value 3 </li>
</ul>
connected like this
$( ".sortable" ).sortable({
connectWith: '.sortable',
placeholder: "widget-highlight"
});
i knew how to save a one ordered list to database by saving the order
but how to save if the user moves an item from list a to list b ?
i want to save the item position but how
Using .sortable()'s .receive() callback, get the dropped node's .index() via ui.item.index(). How you process it in PHP will depend on how your ajax handler is setup.
$( ".sortable" ).sortable({
connectWith: '.sortable',
placeholder: "widget-highlight",
// Receive callback
receive: function(event, ui) {
// The position where the new item was dropped
var newIndex = ui.item.index();
// Do some ajax action...
$.post('someurl.php', {newPosition: newIndex}, function(returnVal) {
// Stuff to do on AJAX post success
});
},
// Likewise, use the .remove event to *delete* the item from its origin list
remove: function(event, ui) {
var oldIndex = ui.item.index();
$.post('someurl.php', {deletedPosition: oldIndex}, function(returnVal) {
// Stuff to do on AJAX post success
});
}
});
The above example will send the dropped node's new list position in $_POST['newPosition']
The events are fully described in the .sortable() API documentation
I am trying to update a value within the jquery ui tab via a ui dialog.
The expected process as such:
From tab display, user clicks on the edit link.
Prompts a ui dialog box with form for user to input value.
User input value and save changes.
After saving, user is brought back to the tab with the updated value.
I have issues on point 4, and here are the codes so far.
HTML:
<div id="acc">
<input type="text" id="edit_acc" value="">
</div>
<div id="tabs">
<ul>
<li>Tab One</li>
<li>Tab Account</li>
<li>Tab Three</li>
</ul>
</div>
<div id="one">
<p>Tab One Listing</p>
</div>
<div id="account">
<p>Tab Account Listing</p>
<table width="100%">
<?php
while ($rows = mysql_fetch_array($query))
{
echo '<tr>';
echo '<td id="editacc_'.$rows['id'].'">'.$rows['name'].'</td>';
echo '<td><a id="acc_'.$rows['id'].'" class="link_acc" href="#">Edit</a></td>';
echo '</tr>';
}
?>
</table>
</div>
<div id="three">
<p>Tab Three Listing</p>
</div>
Javascript:
$('#tabs').tabs({
ajaxOptions: {
error: function(xhr, index, status, anchor)
{
// if page does not exist, this will load
$(anchor.hash).text('Could not load page');
}
}
});
$('.link_acc').click(function() {
acc = $(this).attr('id');
acc = acc.replace('acc_', '');
$.post('get.php', { item: acc}).success(function(data) {
$('#edit_acc').val(data);
});
// prompt dialog box with the value of the stated id
$('#acc').dialog({
title: 'Edit Account',
modal: true,
buttons: {
"Save": function() {
var data = $('#edit_acc').val();
$.post('set.php', { item: acc, val: data}).success(function() {
$('#tabs').tabs('load', 2);
$('#acc').dialog( "close" );
});
},
Cancel: function() {
$(this).dialog( "close" );
}
}
});
});
get.php - retrieve value from the database
if (isset($item))
{
// check for the cat id
$query = mysql_query('SELECT name FROM acc WHERE id = '.$item);
$rows = mysql_num_rows($query);
if ($num_rows == 1)
{
while ($result = mysql_fetch_array($query))
{
echo $result['name'];
}
}
}
set.php - update the database
if (isset($item))
{
mysql_query('UPDATE acc SET name ="'.$val.'" WHERE id = '.$item);
}
I have 2 questions:
how to get display refreshed and displayed the updated value within the tab Account listing?
is there a better/neater way to in the passing of the referencing the id instead of using the id attr and replacing it?
Thank you in advance.
You don't need an ajax request to get the data. You can read it from the table cell.
Replace
$.post('get.php', { item: acc}).success(function(data) {
$('#edit_acc').val(data);
});
with
var valueHolder = $(this).parent().prev();
$('#edit_acc').val(valueHolder.text());
And if you want to update the data after the save-request in the table cell, you can use the variable again:
valueHolder.text(value);
P.s.: in your code you have the id 'account' twice, this is not allowed, ids have to be unique; in the javascript code you're using the id 'acc', so you should use it in the html too.
=== UPDATE ===
Also see this example.
P.s.: you should remove the $('#tabs').tabs('load', 2); and move the divs with the ids one, account and three into the div with the id tabs.
Im trying to replicate the functionality of this page (http://www.kissfm.ro/fresh-top-40/) for a friend who has a small web radio. The site is setup in wordpress fyi.
So my question is, after searching stackoverflow, how do i save/get the revised charts based on the users input? i found how to save single submittions but how do i get the final charts based on the user choice?
Thanks in advance!
(code or link to tutorial welcome!)
make your HTML sortable, add javascript, and save to php on update.
<ul id="sortable">
<li id="1">elem 1</li>
<li id="2">elem 2</li>
<li id="3">elem 3</li>
<li id="4">elem 4</li>
</ul>
$(document).ready(function(){
$('#sortable').sortable({
update: function(event, ui) {
var newOrder = $(this).sortable('toArray').toString();
$.get('saveSortable.php', {order:newOrder});
}
});
});
I know this is old, but what I do is just add a hidden input element in every LI. They would all have a the same name with [] at the end. This way, when you post the form containing the UL, you will get an array in your post values in the order you just put your list.
According to the Sortable docs we have to prefix the LI's id with some string.
Then if we serialize the sortable in the update method we'll get a nice array in php e.g. new_order[]=1&new_order[]=2 etc.
var data = $(this).sortable('serialize');
<ul id="sortable">
<li id="new_order_1">elem 1</li>
<li id="new_order_2">elem 2</li>
<li id="new_order_3">elem 3</li>
<li id="new_order_4">elem 4</li>
</ul>
You may POST with input to DB and save it
Here we go:
<ul id="sortable">
<li id="1"><input type ="text" value="elem 1"/></li>
<li id="2"><input type="text" value="elem 2"/></li>
.
.
</ul>
<style>
#sortable{
border: hidden;
}
</style>
$(document).ready(function(){
$('#sortable').sortable({
update: function(event, ui) {
var newOrder = $(this).sortable('toArray').toString();
$.get('saveSortable.php', {order:newOrder});
}
});
});
Hope it helps ;)