Mewriick / Blazor.FlexGrid

GridView component for Blazor
MIT License
198 stars 36 forks source link

How to get the current value of a property in a RenderFragment in order to create an select input ? #87

Closed Methraen closed 4 years ago

Methraen commented 4 years ago

@Mewriick Hello again, it's been a while! Im currently trying to make an select input based on what you told me, and im having a little issue \: (I followed the example you made here \: Blazor.FlexGrid/src/Blazor.FlexGrid/Components/Renderers/EditInputs/SelectInputRenderer.cs) i know i need IActualItemContext<object> to get the current value but RenderFragment cant get a second parameter so how do i get it ?

Func<EditColumnContext, RenderFragment<TestDto>> editRenderSexe =
      context =>
      {
        RenderFragment<TestDto> ageEdit = (TestDto test) => delegate (RenderTreeBuilder rendererTreeBuilder)
        {
          var internalBuilder = new BlazorRendererTreeBuilder(rendererTreeBuilder);
          internalBuilder
            .OpenElement(HtmlTagNames.Div, "edit-field-wrapper")
            .OpenElement(HtmlTagNames.Select, "edit-text-field")
            .AddAttribute(HtmlJSEvents.OnChange, EventCallback.Factory.Create(this,
              (ChangeEventArgs e) =>
              {
                context.NotifyValueHasChanged(BindConverterExtensions.ConvertTo(e.Value, String.Empty));
              })
            );

          List<String> enumTypeValue = new List<string>() { "Homme", "Femme", "Autre" };

          foreach (var enumValue in Enum.GetValues(enumTypeValue.GetType()))
          {
            var enumStringValue = enumValue.ToString();

            internalBuilder.OpenElement(HtmlTagNames.Option);
            if (enumStringValue == actualStringValue)//At This Point
            {
              internalBuilder.AddAttribute(HtmlAttributes.Selected, true);
            }

            internalBuilder
              .AddAttribute(HtmlAttributes.Value, enumStringValue)
              .AddContent(enumStringValue)
              .CloseElement();
          }

          internalBuilder
            .CloseElement()
            .CloseElement();
        };

        return ageEdit;
      };
Mewriick commented 4 years ago

@Methraen

Current value for you is test parametr of your lamba. You have whole row object in this value and you can use it

Methraen commented 4 years ago

@Mewriick I dont know how i didnt saw this, i think my brain overheated, thank you.

Methraen commented 4 years ago

@Mewriick I changed (enumStringValue == actualStringValue) to (enumStringValue == test.Sexe) but then, when i click on the edit row button the site freeze and dont respond anymore.. Any idea ?

Mewriick commented 4 years ago

@Methraen

It could be badly builded renderer tree. Some closing tag can be missed or maybe much more closing tags than opening

Methraen commented 4 years ago

@Mewriick I did not find missed closing tag or too much opening tag.. the problem is maybe in the loop...

Func<EditColumnContext, RenderFragment<TestDto>> editRenderAge =
      context =>
      {
        RenderFragment<TestDto> ageEdit = (TestDto test) => delegate (RenderTreeBuilder rendererTreeBuilder)
        {
          var internalBuilder = new BlazorRendererTreeBuilder(rendererTreeBuilder);
          internalBuilder
            .OpenElement(HtmlTagNames.Div, "edit-field-wrapper")//Open 1
            .OpenElement(HtmlTagNames.Select, "edit-text-field")//Open 2
            .AddAttribute(HtmlJSEvents.OnChange, EventCallback.Factory.Create(this,
              (ChangeEventArgs e) =>
              {
                context.NotifyValueHasChanged($"{BindConverterExtensions.ConvertTo(e.Value, String.Empty)}_CustomEdit");
              })
            );

          List<String> enumTypeValue = new List<string>() { "18-35", "35-50", "50+" };

          foreach (var enumValue in Enum.GetValues(enumTypeValue.GetType()))
          {
            var enumStringValue = enumValue.ToString();

            internalBuilder.OpenElement(HtmlTagNames.Option);//Open 3
            if (enumStringValue == test.TrancheAge)
            {
              internalBuilder.AddAttribute(HtmlAttributes.Selected, true);
            }

            internalBuilder
              .AddAttribute(HtmlAttributes.Value, enumStringValue)
              .AddContent(enumStringValue)
              .CloseElement();//Close 3
          }

          internalBuilder
            .CloseElement()//Close 2
            .CloseElement();//Close 1
        };

        return ageEdit;
      };
Methraen commented 4 years ago

It worked without changing anything in particular... ty anyway.