With a play view template I know I can convert a Scala List into a javascript Array e.g. for example.scala.html:
@(list: List[Any])
@import play.api.libs.json.Json
<script type="text/javascript">
$(document).ready(function () {
var jsArr = @Json.toJson(list);
console.log(jsArr);
});
</script>
but how does one go back the other way, converting a javascript array into a scala list to pass on to a controller?
I have template code (test.scala.html) like this:
@(selected: List[String])
@main("Test Scripts Page") {
<div class="page-header page-heading">
<h1 class="pull-left">Test Scripts Page</h1>
@helper.form(action = helper.CSRF(routes.DoSomething.create(selected))) {
<input type="submit" class="btn btn-primary pull-right" value="Done >"> }
<div class="clearfix"></div>
<p class="lead text-left">Choose Buttons</p>
</div>
@for(index <- 0 to 3) {
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary"><input type="checkbox" name="options" id="option-@index" aria-pressed="false" autocomplete="off" checked>option @index</label>
</div> <!-- buttons --> }
<input type="text" id="buttonvalue"/>
<script>
$(window).unload(function() {
var jsArray = []
$('.btn-group checkbox:selected').each(function() {
jsArray.push($(this).attr("id"));
});
selected = Json.fromJson(jsArray, String);
$("#buttonvalue").val(selected.text());
});
</script>
}
Which doesn't seem to be working... only returns an empty list [] to my DoSomething(List) controller... basically I'm trying to let the user set the state of some widgets (in my case a bootstrap button-grp, with the ids of the buttons being object ids of static reference data in my database) and then read that selection back into my DoSomething controller as a List of ids when they hit a "done" button. Can anyone offer some advice on what I've missed here or a better way to handle this situation?