Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to render a matrix correctly in Blazor? #49945

Closed
1 task done
sam-wheat opened this issue Aug 9, 2023 · 7 comments
Closed
1 task done

How to render a matrix correctly in Blazor? #49945

sam-wheat opened this issue Aug 9, 2023 · 7 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@sam-wheat
Copy link

sam-wheat commented Aug 9, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I need to render some objects in a matrix - basically a table with a variable number of columns.
In the case of the example below I need four columns.

The problem code is below. As indicated in the comment rowIndex variable is stuck at 4. I apparently have some
kind of closure problem (?) but IDK what it is. What am I doing wrong here?
Runnable project is attached showing expected output.
This is my first Blazor project for a large client. I hope to impress them favorably with Blazor. Thank you for your assistance with this.

	<table style="border:solid 2px black;">
		@{
			int rowIndex = 0;
			int rowCount = Convert.ToInt32(Math.Ceiling(people.Count / Convert.ToDecimal(ColumnCount)));
		}
		@while (rowIndex <= rowCount)
		{
			<Tr>
				@{
					int colStart = (rowIndex * ColumnCount);  //  rowIndex is always 4.
					int colStop = (rowIndex * ColumnCount + ColumnCount);
				}
				@for (int colIndex = colStart; colIndex <= colStop; colIndex++)
				{
					if (colIndex >= people.Count)
					{
						break;
					}

					<td>
						<PersonInput Name="@people[colIndex].Name"  />
					</td>
				}
			</Tr>
			rowIndex++;
		}
	</table>

Expected Behavior

	<table>
		<Tr>
			<td><PersonInput Name="This is"/></td>
			<td><PersonInput Name="The" /></td>
		</Tr>
		<Tr>
			<td><PersonInput Name="Expected" /></td>
			<td><PersonInput Name="output format" /></td>
		</Tr>
	</table>

Steps To Reproduce

See Index.razor in attached project

Exceptions (if any)

No response

.NET Version

7.0.306

Anything else?

MatrixTest.zip

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Aug 9, 2023
@bub-bl
Copy link

bub-bl commented Aug 9, 2023

Have you tried to use a for instead of a while ?

Like:
for(var rowIndex = 0; rowIndex <= rowCount; rowIndex++)

I think the main problem is you attempt to increment a value in a while, but basicaly, a while not used in a thread block the mainthread.

@sam-wheat
Copy link
Author

@bub-bl Not sure I understand what you mean. I am incrementing rowIndex inside the loop.

@Markz878
Copy link

Markz878 commented Aug 9, 2023

When you pass a loop variable to a child component in a loop, you need to capture that variable in a new parameter:

<table>
    @for (int i = 0; i < 4; i++)
    {
        int x = i;
        <Tr @key=x>
            @for (int j = 0; j < 4; j++)
            {
                <td>
                    <p>Test @x @j</p>
                </td>
            }
        </Tr>
    }
</table>

You should also use a @key attribute, especially if you're going to insert/delete rows.

@sam-wheat
Copy link
Author

@Markz878 I got it. Thanks!

@mkArtakMSFT
Copy link
Member

Thanks for assisting with this, @Markz878!

@sam-wheat I assume that resolved your problem, didn't it?

@sam-wheat
Copy link
Author

@mkArtakMSFT Yes many thanks to all.

@ghost ghost locked as resolved and limited conversation to collaborators Sep 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

5 participants