?
Solved

Save sort order to hidden field with sortable jQuery plugin

Posted on 2014-03-28
7
Medium Priority
?
2,018 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
  • 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

588 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