Is there a way to populate the Row Label in a table field using a database look up?
Question
Question
Answer
The tricky part is getting the code to run after the dropdown is populated with the results. Once you do that, you can use something like this to get it working. The following code assumes you have a drop-down (given the filledField class) being populated with values from a lookup, and a table with an Add button (id #q4).
You'll probably need to fiddle with this a bit more, but here's a start.
$(document).ready(function () { function tableAwesome() { var resultNumber = $('.filledField option').size(); for (var i = resultNumber - 1; i > 0; i--) { $('#q4').trigger('click'); } for (var i = 1; i <= resultNumber; i++) { $('td:contains("Row ' + i + '")').text($('.filledField option:nth-child(' + i + ')').text()); } } $('.filledField').change(tableAwesome); });
Update: The code used to answer this question also applies to this question.
Replies
So, based on the number of results in the lookup, would you expect the number of table rows to adjust to match the number of results? I think it should be possible, but might be a little work.
I think it'd work like this:
- Have the lookup populate a drop-down list
- If the list is greater than the number of table rows, add more table rows to balance things out
- Iterate through the number of results/rows. Change row one's label to match the first result's value and so on.
That is correct. We would like the rows to adjust dynamically to the number of results in the lookup.
The tricky part is getting the code to run after the dropdown is populated with the results. Once you do that, you can use something like this to get it working. The following code assumes you have a drop-down (given the filledField class) being populated with values from a lookup, and a table with an Add button (id #q4).
You'll probably need to fiddle with this a bit more, but here's a start.
$(document).ready(function () { function tableAwesome() { var resultNumber = $('.filledField option').size(); for (var i = resultNumber - 1; i > 0; i--) { $('#q4').trigger('click'); } for (var i = 1; i <= resultNumber; i++) { $('td:contains("Row ' + i + '")').text($('.filledField option:nth-child(' + i + ')').text()); } } $('.filledField').change(tableAwesome); });
Update: The code used to answer this question also applies to this question.