HTML表单:发布一个对象数组 [英] HTML Form: POST an array of objects
问题描述
问题:我们如何获得数组数组中的所有学生?
students [0] =>数组(
[first] =>名字为0,
[last] =>姓氏为0,
[age] = >年龄为0
),
students [1] =>数组(
[first] =>first name for 1,
[last] =>姓氏为1,
[age] = >age for 1
),
...
<
< input type =text详情
对于一名学生: NAME = 第一 >
< input type =textname =last>
< input type =textname =age>
我们可以像这样在不同的数组中返回多个学生:
< input type =textname =students [first] []>
< input type =textname =students [last] []>
< input type =textname =students [age] []>
返回第一个数字,持续数字和年龄
students [first] = [名字数组]
students [last] = [姓氏数组]
students [年龄] = [数组年龄]
理论上我们可以通过访问获取学生的所有信息相同的索引(对每个数组说3)。
我们不想以编程方式在表单中添加索引。
不想要:
< input type =textname =students [hardcoded_index] [first]>
< input type =textname =students [hardcoded_index] [last]>
< input type =textname =students [hardcoded_index] [age]>
如果由于任何原因它很重要,我们使用Rails作为视图,但可以使用表单助手或HTML。
tl; dr:添加空括号( []
) students
添加到输入名称中。
摆弄 Rack :: Utils.parse_nested_query 看来你可以像这样获得你想要的载荷:
<! - first student - >
< input type =textname =students [] [first]>
< input type =textname =students [] [last]>
< input type =textname =students [] [age]>
<! - 第二个学生 - >
< input type =textname =students [] [first]>
< input type =textname =students [] [last]>
< input type =textname =students [] [age]>
请注意空括号( [] $ c $在
。这告诉Rack你希望 students
之后 students
param是一个数组。后续遇到的(使用相同名称的)参数将启动一个新元素。
POST / myroute?students [] [first] = foo& amp; amp; amp; amp; amp; students [] [最后] =栏&安培;学生[] [年龄] = 21&安培;学生[] [第一] =巴兹&安培;学生[] [最后] = qux&安培;学生[] [年龄] = 19
获得如下解析:
{students=> ; [
{
first=> foo,
last=> bar,
age=> 21
},
{
first=> baz,
last=> qux,
age=> 19
}
]}
进一步阅读: http://codefol.io/posts/How-Does -Rack-Parse-Query-Params-With-parse-nested-query
Submitting a class roster. Adding 3 students at once. Each student has first, last, age.
Question: How can we get all of the students in an array of arrays?
students[0] => Array (
["first"] => "first name for 0",
["last"] => "last name for 0",
["age"] => "age for 0"
),
students[1] => Array (
["first"] => "first name for 1",
["last"] => "last name for 1",
["age"] => "age for 1"
),
...
Details
For one student:
<input type="text" name="first">
<input type="text" name="last">
<input type="text" name="age">
We can return multiple students in separate arrays like this:
<input type="text" name="students[first][]">
<input type="text" name="students[last][]">
<input type="text" name="students[age][]">
which returns an array of firsts, lasts and ages
students["first"] = [array of first names]
students["last"] = [array of last names]
students["age"] = [array of ages]
Theoretically we can get all the info for a student by accessing the same index (say "3" for each array).
We do not want to programatically add an index in the form.
Do not want:
<input type="text" name="students[hardcoded_index][first]">
<input type="text" name="students[hardcoded_index][last]">
<input type="text" name="students[hardcoded_index][age]">
If for any reason it matters, we are using Rails for views but can use form helpers or HTML.
tl;dr: Add empty brackets ([]
) after students
to the input names.
Fiddling with Rack::Utils.parse_nested_query it seems you can get the payload you want like this:
<!-- first student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">
<!-- second student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">
Note the empty brackets ([]
) after students
. This tells Rack you want the students
param to be an array. Subsequent params encountered (with the same name) will start a new element.
POST /myroute?students[][first]=foo&students[][last]=bar&students[][age]=21&students[][first]=baz&students[][last]=qux&students[][age]=19
Gets parsed like this:
{"students" => [
{
"first" => "foo",
"last" => "bar",
"age" => "21"
},
{
"first" => "baz",
"last" => "qux",
"age" => "19"
}
]}
Further reading: http://codefol.io/posts/How-Does-Rack-Parse-Query-Params-With-parse-nested-query
这篇关于HTML表单:发布一个对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!