-2
$\begingroup$

I worked this way, but it does not work as expected: The rows of the table:

nrows = Slider[Dynamic@n, {0, 5, 1}];

The contents of the table:

info = Dynamic@Table[<|"a" -> "", "b" -> ""|>, {i, n}]

The input form of the table:

dt = Dynamic @ Table[With[{i = i},
  <|"a" -> InputField[info[[1]][[i]][["a"]], String],
    "b" -> InputField[info[[1]][[i]][["b"]], String]
 |>
], {i, n}]

enter image description here

Why haven't the editing been updated to the table's content?

Also, if I predefine the table's rows, then the table will not be dynamic, but if I do not predefine it, the data will lose when shorten the table's rows. The first way is easy, I've already implemented it that way. So what I am really confused of is the second way. How can I implement the input form if I want the data to be forgotten if I shorten n, and at the same time, what I input into the form, the data I input will be dynamic updated in the table? Or, is there a way to define a table with dynamic rows without losing data when shorten the rows by a slider?

$\endgroup$

closed as off-topic by Kuba Feb 28 '17 at 11:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Kuba
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ How changing n should affect already existing data? $\endgroup$ – Kuba Feb 21 '17 at 13:35
  • $\begingroup$ Sorry for late reply. Your question is just another problem I've been encountered with. I found I could only define a 5 row table and then use a dynamic slider to control the input form generated, but I could not change the row number of the table and generate a same length input form at the same time. So could you help me with that also. $\endgroup$ – cmal Feb 28 '17 at 11:38
  • $\begingroup$ I just want to use a slider to control a table's length and a dynamic form's length where the form presents the table's every element, and the edited value been updated to the table immediately. $\endgroup$ – cmal Mar 1 '17 at 3:41
  • $\begingroup$ Sure, but you still didn't answered my first comment, what should happen with entries when you shorten n, should they be forgotten? Maybe changing n should always reset the table? $\endgroup$ – Kuba Mar 1 '17 at 7:27
  • $\begingroup$ Oh, that is a good question. If I predefine the table's rows, then the table will not be dynamic, but if I do not predefine it, the data will lose when shorten the table's rows. The first way is easy, I've already implemented it that way. So what I am really confused of is the second way. How can I implement the input form if I want the data to be forgotten if I shorten n, and at the same time, what I input into the form, the data I input will be dynamic updated in the table? Thanks! $\endgroup$ – cmal Mar 1 '17 at 10:44
4
$\begingroup$

I think you can solve the problem as follows:

Row[{Slider[Dynamic[n], {0, 5, 1}], " n = ", Dynamic[n]}]

Dynamic[
    TableForm[
        Table[
            {
                InputField[Dynamic[Evaluate[Symbol["x" <> ToString[i]]]]], 
                InputField[Dynamic[Evaluate[Symbol["y" <> ToString[i]]]]]
            }, 
            {i, 1, n}
        ]
    ]
]


Dynamic[
    TableForm[
        Table[
            <|
                "x" <> ToString[i] -> Symbol["x" <> ToString[i]], 
                "y" <> ToString[i] -> Symbol["y" <> ToString[i]]
            |>, 
            {i, 1, n}
        ]
    ]
]

I received the following:

$\endgroup$
  • $\begingroup$ But I am afraid I cannot take this way to solve this problem. We use a lot of fields in associations so I think it is not a good way to rename every key to a symbol to assoc a value and then assoc to the association. $\endgroup$ – cmal Feb 20 '17 at 12:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.