jquery - Copying a ColdFusion cfparam array into a JavaScript array -
i've have cf page who's inventory search form, frm_inv
post's itself. frm_inv
's main table of records, tbl_inv
, uses tablesorter
. hidden input (sort_list
) used in conjunction cfparam keep track of tbl_inv
's sortlist
:
main.cfm
<cfparam name="form.sort_list" type="string" default="1,0"> <form id="frm_inv" action="main.cfm" method="post"> <input name="sort_list" type="hidden" value="#form.sort_list#"/> <table id="tbl_inv" class="tablesorter"> ... </table> </form>
when frm_inv
submitted, cf uses sort_list
in $(document).ready()
restore tbl_inv
's sort order:
$(document).ready(function(){ var sort_list_str = <cfoutput>"#form.sort_list#"</cfoutput>; var sort_list = sort_list_str.split(","); $("#tbl_inv").tablesorter({ textextraction: ['complex'], sortlist:[[sort_list[0],sort_list[1]]] }).bind("sortend", function(sorter) { var sl = sorter.target.config.sortlist; $("input[name='sort_list']").val(sl.tostring()); }); });
i rather use arrays convert comma separated string array i'm doing
<cfparam name="form.sort_list" type="string" default="1,0"> <cfparam name="form.sort_list" type="array" default="arraynew(2)">
however need know proper javascript , coldfusion syntax in order pose that's relevant in arrays exclusively.
copying coldfusion ... array javascript array
why? part, http transmits strings, there no translation between client , server complex types. unless doing more passing sort value , forth, converting between client , server side arrays unnecessary complication. simpler leave value string , splitting or parsing on client side.
you did not explain problem trying solve, .. there nothing inherently wrong current approach. however, simplified bit. there no need cfoutput variable again here:
(a)
var sort_list_str = <cfoutput>"#form.sort_list#"</cfoutput>;
since stored current form.sort_list
value in hidden form field, above redundant. instead, read field's value javascript ie
(b) var sort_list_str = $("input[name='sort_list']").val();
having said that, if prefer work arrays, store json string representation of arrays instead. use parse() , stringify() convert arrays , forth. same net effect current method, bit simpler in terms of code.
form:
<cfparam name="form.sort_list" default="[[1,0]]"> ... <input id="sort_list" name="sort_list" type="hidden" value="#encodeforhtml(form.sort_list)#" /> ...
jquery:
$(document).ready(function(){ $("#tbl_inv").tablesorter({ textextraction: ['complex'], sortlist: json.parse($("#sort_list").val()) }).bind("sortend", function(sorter) { var sort_arr = sorter.target.config.sortlist; $("#sort_list").val(json.stringify(sort_arr)); }); });
Comments
Post a Comment