C
C#15mo ago
N0mlss

❔ Blazor: Get value of nested component

I have two component: CompA and CompB and a page index. CompB is a simple input CompA has a RenderFragment I want to display the input text of CompB inside CompA PS: I am doing stuff step by step to understand the process
No description
77 Replies
N0mlss
N0mlssOP15mo ago
No description
N0mlss
N0mlssOP15mo ago
No description
N0mlss
N0mlssOP15mo ago
The goal of all of that is to eventually build a component with nested components that interact with eachother...
friedice
friedice15mo ago
What's your question?
N0mlss
N0mlssOP15mo ago
How can i display input of CompB inside of CompA
friedice
friedice15mo ago
if you want to only display the input, you dont need to set up two way binding like the way you're doing
//compA
<div>This is Comp A</div>
@ChildContent

@code {
[Parameter]
public RenderFragment ChildContent { get; set; }
}


//compB
<div>This is comp B, underneath it is the input</div>
<input type="text" @bind-value="_inputVal" />
@code {
private string _inputVal = string.Empty;
}


//page
@page "/"

<PageTitle>Index</PageTitle>

<CompA>
<CompB/>
</CompA>
//compA
<div>This is Comp A</div>
@ChildContent

@code {
[Parameter]
public RenderFragment ChildContent { get; set; }
}


//compB
<div>This is comp B, underneath it is the input</div>
<input type="text" @bind-value="_inputVal" />
@code {
private string _inputVal = string.Empty;
}


//page
@page "/"

<PageTitle>Index</PageTitle>

<CompA>
<CompB/>
</CompA>
N0mlss
N0mlssOP15mo ago
I simplified my problem as much as possible because we don't care about the details. The real issue is that I want to retrieve the value in CompB and show it in CompA, but I think it is not possible to do it according to that https://stackoverflow.com/questions/60588712/retrieving-value-within-renderfragment
Stack Overflow
Retrieving value within RenderFragment
I would like to retrieve the value of a RenderFragment for use in my code: Parent Component <SkillIndividual> <Skill>Fireball</Skill> <AttributeType>Fire</Attribute...
friedice
friedice15mo ago
so you want the Value from the child component and use it in the parent component?
N0mlss
N0mlssOP15mo ago
Yeah
friedice
friedice15mo ago
easiest thing to do would be 2 way binding then create the value in the parent, pass it to the child
N0mlss
N0mlssOP15mo ago
What if i want to put two CompB in the RenderFragment?
friedice
friedice15mo ago
why do you want specifically render fragment?
N0mlss
N0mlssOP15mo ago
The real thing im trying to do is build a generic table where you can add lines to it. The footer either accept input text or select. Before runtime, the code doesnt know how much column there will be in the table
friedice
friedice15mo ago
why not in CompA
<div>This is compA</div>
<CompB @Value="_value1"/>
<CompB @Value="_value2"/>
<div>This is compA</div>
<CompB @Value="_value1"/>
<CompB @Value="_value2"/>
N0mlss
N0mlssOP15mo ago
Cause before runtime i dont know how much compB i will need
N0mlss
N0mlssOP15mo ago
No description
N0mlss
N0mlssOP15mo ago
Im passing a class that generate the columns here, so the component is generic. In some case, there might be 3 columns, other cases, only one. So I can't simply put the CompB in CompA because the number of inputs are situational Does that make sense?
blinkbat
blinkbat15mo ago
well the ui is just a reflection of the data so you'd add data to the list and the two way binding would correspond to a parent data structure's key just pass a handler to the child like you know how Prop/OnPropChanged params work?
N0mlss
N0mlssOP15mo ago
But how can i define if the table uses input or select? I understand that the UI is the reflection of the data, but i am trying to build a reusable "infinite table" where i can define the combinaison of element i want in the footer wheter it is ddl.txtbox,txtbox ddl or whatever else combinaison. I dont know if that make sense
blinkbat
blinkbat15mo ago
it doesnt explain with concrete example or diagram
N0mlss
N0mlssOP15mo ago
I will explain what i am trying to do then i will send screnshot of where i am rn with it. Is that good?
blinkbat
blinkbat15mo ago
ok
N0mlss
N0mlssOP15mo ago
So I am trying to create a generic table where i can pass a List<class> and the table will display the data. Each properties of "class" has a column
blinkbat
blinkbat15mo ago
ok
N0mlss
N0mlssOP15mo ago
In the footer of that table, you can add datas to that List<class>. But since the table is generic, i want to reuse it. Sometime the datas can only be added from combobox, ddl, select, whatever you want to call it Some other times, the datas can be added from input text
blinkbat
blinkbat15mo ago
add data = add a list row?
N0mlss
N0mlssOP15mo ago
Exactly
blinkbat
blinkbat15mo ago
ok and depending on the property type it could be an enum or free text etc right?
N0mlss
N0mlssOP15mo ago
exact! or data coming from database as choices
blinkbat
blinkbat15mo ago
per column ok
N0mlss
N0mlssOP15mo ago
yup
blinkbat
blinkbat15mo ago
any issue so far? i see none aside from costly reflection to determine the properties
N0mlss
N0mlssOP15mo ago
Well since the table doesnt know how much columns and what the elements will be in the footer before runtime, I was thinking of having nested component to determine the elements in the footer
blinkbat
blinkbat15mo ago
you just use reflection
N0mlss
N0mlssOP15mo ago
what do you mean?
blinkbat
blinkbat15mo ago
the user picks type T
N0mlss
N0mlssOP15mo ago
No description
N0mlss
N0mlssOP15mo ago
This is what i have right now
blinkbat
blinkbat15mo ago
you use reflection to get properties and types of each from T
N0mlss
N0mlssOP15mo ago
No description
blinkbat
blinkbat15mo ago
depending on the property's type you determine the correct input to show and predefined options if any
N0mlss
N0mlssOP15mo ago
Ohhh so depending on what i give in the class, it will decide if a textbox or a combobox is needed?
blinkbat
blinkbat15mo ago
of course how else?
N0mlss
N0mlssOP15mo ago
...
blinkbat
blinkbat15mo ago
like if type is T and T.A is string with no constraints show an input
N0mlss
N0mlssOP15mo ago
I was thinking of passing RenderFragment in the component to decide it
No description
blinkbat
blinkbat15mo ago
if T.B is an enum, show a select idk what render fragment has to do with it
N0mlss
N0mlssOP15mo ago
Not sure either, I wanted to add a render fragment per columns to decide what will be in each column of the footer
blinkbat
blinkbat15mo ago
basically you have a Table<T> let's say T is MyObj
N0mlss
N0mlssOP15mo ago
yeah
blinkbat
blinkbat15mo ago
public enum SomeOpts { Opt1, Opt2 }
class MyObj {

public string FreeText { get; set; }
public SomeOpts Opt { get; set; }
}
public enum SomeOpts { Opt1, Opt2 }
class MyObj {

public string FreeText { get; set; }
public SomeOpts Opt { get; set; }
}
using reflection, iterate over properties of MyObj foreach, if type of property == string, render an <input> if type == enum, render a <select><option value="1">... etc right?
N0mlss
N0mlssOP15mo ago
Got it! But what if the propertie is a string, but it has to be a select for some reason?
blinkbat
blinkbat15mo ago
why would it? that woud be an enum with DescriptionString
N0mlss
N0mlssOP15mo ago
for some occasion I want the client to choose from a list of option but normally its a free text. Is that a problem with the logic?
blinkbat
blinkbat15mo ago
if its a predefined list, thats an enum
N0mlss
N0mlssOP15mo ago
what if it is a index from the data base refering to another table, how would i retrieve the data to show it in the select inside the table component code?
blinkbat
blinkbat15mo ago
so you're composing classes at runtime? or what
N0mlss
N0mlssOP15mo ago
Lets say the data coming from the db is the following User Name: John Doe City: 4 Then when i pass those datas to the table comp, i would retrieve the city name so it will be a string. But in reality, its not a free choice, since the selection need to come from the database table city. So the select options need to come from the database. In that case it won't be an enum cause we can add or remove available cities as the "application" runs from an admin console. So it cant be an enum
blinkbat
blinkbat15mo ago
do you know the class of User ahead of time?? if not a typed lang gives you nothing
N0mlss
N0mlssOP15mo ago
Yeah
blinkbat
blinkbat15mo ago
ok good then you know that City is an enum right?
N0mlss
N0mlssOP15mo ago
Wait, i know cityId is a foreign key, but i never considered it as an enum 😅 I guess it is?
blinkbat
blinkbat15mo ago
if there are limited options for it then of course
N0mlss
N0mlssOP15mo ago
but the class user is the following User string name int cityId int age So how do i diffence cityId that is a reference to another table thus an enum from age wich is simply an int
blinkbat
blinkbat15mo ago
public enum Cities {
New York,
Los Angeles
}

public Cities City { get; set; }
public enum Cities {
New York,
Los Angeles
}

public Cities City { get; set; }
you take the value as an int and cast it to enum
N0mlss
N0mlssOP15mo ago
but cities isnt defined in the code since the options comes from database
blinkbat
blinkbat15mo ago
int CityInt, (Cities)CityInt you should define it in code
N0mlss
N0mlssOP15mo ago
But what if it can change overtime from an admin console because some cities arent taken in charge anymore and some are?
blinkbat
blinkbat15mo ago
well code-first would be best instead of db first
N0mlss
N0mlssOP15mo ago
the each time a new city is added the code needs to be modified, it doesnt make sense ahhhh
blinkbat
blinkbat15mo ago
but if you must do db first you need to infer from your models what the enums will be in your view models
N0mlss
N0mlssOP15mo ago
Not sure what you mean by that but the concept make sense now
blinkbat
blinkbat15mo ago
you cant really compose classes dynamically defeats the purpose of a class so it sounds like you want some sort of db first codegen
N0mlss
N0mlssOP15mo ago
yeah, i learnt with database first and just recently started using code first So i didnt think about casting CityInt into enum
blinkbat
blinkbat15mo ago
i think it would be the only way to tell if it shoud be a select or input
N0mlss
N0mlssOP15mo ago
Yeah, that would make sense!!
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server