save jquery ui-sortable positions to DB - php

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 ;)

Related

Hide/show element after append

$("ol").append("<div id='save' style='display:none'>Save<div>");
$("li").mouseup(function() {
$('#save').show();
});
$("ol").sortable();
$("body").on('click','#save',function(){
$(this).hide();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js'></script>
<ol id='some123'>Items
<li draggable='true'>Item 1</li>
<li draggable='true'>Item 2</li>
<li draggable='true'>Item 3</li>
<li draggable='true'>Item 4</li>
</ol>
snippet above working correctly with js append.
How to show an element after append by php using mouseup?
So, I have draggable list. And, I need to show element every time the user changes the list order. Every time the element is dropped, I call a function as shown:
$("li").mouseup(function() {
$('#save').show();
});
$("ol").sortable();
When the user clicks save button, I hide it using:
$("body").on('click','#save',function(){
$(this).hide();
})
But how do I make it visible again? $('#save').show(); isn't working!
funniest thing:
my php generate 2 buttons, like that:
<div class="buttons"><button id="saveTit" style="display: none" >Save content</button></div>
<div class="buttons"><button class="butClick" data-con='<?php echo $content?>'>Add new row</button></div>
To call 2nd button i use:
$(".butClick").on('click',function(){
alert($(this).attr('data-tit'));
})
but to call 1st one i have to use body selector... any idea why?!
I change id for class inside div and to call a event...
And it works...
Hi try to change the div to hidden instead and use the prop function instead of show and hide :
$("ol").append("<div id='save' hidden>Save<div>");
$("li").mouseup(function() {
$('#save').prop("hidden",false);
});
$("ol").sortable();
$("body").on('click','#save',function(){
$(this).prop("hidden",true);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js'></script>
<ol id='some123'>Items
<li draggable='true'>Item 1</li>
<li draggable='true'>Item 2</li>
<li draggable='true'>Item 3</li>
<li draggable='true'>Item 4</li>
</ol>
Hope it helps

Problems in handling pagination in jquery

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/

Display Hex Code on Page load

The correct, working code can be seen on the replies. End result: http://www.creativewebgroup.co.uk/library/colorshareV2/palette/Android
I'm attempting to make a colour palette script.
I have this jQuery script:
<script>
//document ready
$(document).ready(function () {
$('.palette-detail li').each(function () {
$(this).html('<input type="text" style="background: #' + $(this).attr('swatch') + '" />' );
});
$('.palette-detail').click(function (e) {
var elem = e.target;
if ($(elem).is('input')) {
$(elem).val($(elem).parent().attr('swatch'));
}
});
});
Here's a basic idea of the HTML used (in the script however it's PHP driven).
<ul class="palette">
<li swatch="#FFFFFF">
<li swatch="#FFFFFF">
<li swatch="#FFFFFF">
<li swatch="#FFFFFF">
<li swatch="#FFFFFF">
<span>Title</span>
</ul>
At the moment the script requires the user to click on a li block for the hex code to display. I want it to instead show straight away.
Is this possible? If so how?
Thanks a bunch guys!
HTML
<ul class="palette">
<li swatch="#4362ff">
<li swatch="#ee3d5f">
<li swatch="#FFFFFF">
<li swatch="#FFFFFF">
<li swatch="#FFFFFF">
</ul><span>Title</span>
jQuery
//document ready
$(document).ready(function () {
$('.palette li').each(function () {
$(this).html('<input value='+$(this).attr("swatch")+' type="text" style="background: ' + $(this).attr('swatch') + '" />');
});
});
fiddle
There were lots of mistakes in the code. You didn't select the correct class for the UL.
Also UL elements can not contain span elements.
Also using inspect element would have showed the code does what it told it to and put ## on front of the color.

save sort oder and populate sorted records jquery UI Sortable

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';

jquery ui selectable checkbox

I have two questions. I am using jquery selectable ui for a checkbox.
http://jsfiddle.net/skeR4/4/
1.) How can i change the script, so on click of a different <li> it also highlights that element, instead of deselecting what was previously highlighted. and to deselect an element the user reclicks the element. this way the user can select multiple elements by clicking each one individually without having to ctrl+click to select multiple.
<div class="demo">
<ol id="selectable">
<li class="ui-state-default">1<input type="checkbox" value="something"></li>
<li class="ui-state-default">2</li>
<li class="ui-state-default">3</li>
<li class="ui-state-default">4</li>
</ol>
</div>​
<script>
$(function() {
$( "#selectable" ).selectable();
});
</script>​
$("#selectable").bind("mousedown", function(e) {
e.metaKey = true;
}).selectable();
DEMO

Categories