?
Solved

Save sort order to hidden field with sortable jQuery plugin

Posted on 2014-03-28
7
Medium Priority
?
1,917 Views
Last Modified: 2014-03-29
I am using this plugin (http://farhadi.ir/projects/html5sortable/) to create a feature for a website where a user can add form fields on the fly.

The idea is that they will give the form a name (title), and then add a series for form fields. These form fields need to be drag and drop sortable (thus the plugin) so the user can rearrange the order.

Once they are satisfied with the order, they will click the "Create Form" button, and the data to create the form will be submitted.

The data itself will be stored in three tables. Table 1: Forms, Table 2: Fields, and Table 3: Forms_Has_fields. The Forms_Has_fields table will includes a sort_order column, which tells the system in what order to display the fields.

That's where this question is important: when a field is re-sorted using the drag and drop feature, I need a way to save the sort order in an <input type="hidden"> or some other form control so I can parse it with PHP and create the fields properly.

If this was in PHP, I would just keep everything in an associative array: ($fieldname => $sortorder), but that doesn't seem like it's an option in javascript. Ideally, I could keep this in an array in Javascript, and then when sumit was clicked, dump that array as a JSON to an <input> field, and POST it to PHP. But if that's possible, I don't know how to do it.

If there is another (more elegant) way to handle this, I am open to suggestions.
0
Comment
Question by:DrDamnit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 39962078
Since any object in Javascript can be defined as a string, why not use something like serializeArray to convert your array into a string that can be put into the hidden field. It will be posted back to the server, but you won't be doing anything with it--just dumping it back out to the hidden field in the response. On page load, you can call parseJson to get the data back into an array.
0
 
LVL 32

Author Comment

by:DrDamnit
ID: 39962090
But here's my hangup: when I change the sort order, it will obviously fire an event... is the array still available after the page loads and the $(document).ready() has finished? Keep in mind, I am adding a DOM / form element (I'm using <li data-name="foo">foo</li>).
0
 
LVL 32

Author Comment

by:DrDamnit
ID: 39962100
Additionally... I want to re-generate the <input> data every time. I can sift, sort, push and pop the array, but dumping to JSON every time will make it clean.

Should this array be a global variable? (I hate globals, but is this the best way?)
0
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39962105
If you keep it in a global variable (i.e. not declared locally to any particular function [or event]), then it should be. Obviously you'll have to update it as the sort order is modified.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39962109
Feel free to wait for one of the web-dev gurus to pop in. There may be a more elegant way to handle this; this is just what popped in my head upon reading your question.
0
 
LVL 32

Author Comment

by:DrDamnit
ID: 39964085
Turns out, I was making it more complicated than it needed to be. I am giving you the points because you answered my question; however I did find a more elegant way of doing it. The <ul> is, in fact, the order and array already. There is no reason to create a separate array to manage what is being saved in what order because the <ul> does it already. All you have to do is itterate through it.

And, best of all, I didn't have to use global variables.
<script>
function buildOrder() {
	var buffer = [];
	//Rebuild the sort order array.
	$("#theFields li").each(function(index){
		buffer.push($(this).text());
		console.log(buffer);
	});
}
$(document).ready(function(event){
	$("#addField").click(function(event){
		event.preventDefault();
		var fieldName = $("#fieldName").val();
		$("#theFields").append("<li><div class=\"form-group\"><label>"+fieldName+"</label><input type=\"text\" class=\"form-control\"></div></li>");
			buildOrder();
		$(".sortable").sortable().bind('sortupdate',function() {
			buildOrder();
		})
	});
	$("#formName").keyup(function(event){
		$("#formTitle").html($("#formName").val());
	});
});
</script>

Open in new window

0
 
LVL 32

Author Closing Comment

by:DrDamnit
ID: 39964089
Oh, and I am using serializeArray. It was the other bit that was more elegant.
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question