1

I am looking for an explaination, why my example works only whit the document ready handler inside the for-loop:

Here Iam trying to append/remove some rows:

    ko.bindingHandlers.customControl = {
    init: function (element, valueAccessor, allBindings, viewModel, bindingContext)
    {
        var value = valueAccessor();
    },
    update: function (element, valueAccessor, allBindings, viewModel, bindingContext)
    {
        var value = valueAccessor();
        var unmapped = value();
        var $element = $(element);


        $(function ()
        {
            $element.find("bla").remove();

            for (i = 0; i < 10; i++)
            {
                $element.find("table tbody").append('<tr class="bla">some text</tr>');
            }
        });

    }

};

Our c# helper method looks kind of:

    @helper  CustomTableControl(string dynamicBindArray)
    {
//...defining some lists
    <div  data-bind="customControl : @dynamicBindArray">
        <table class="table customTable">
            <thead>
                <tr>
                    @foreach (string item in Listitem)
                        {
                        <th>@item</th>
                        }
                </tr>
            </thead>
            <tbody data-bind="foreach: @dynamicBindArray">
                <tr data-bind="visible: ...">
                    @for (int i = 0; i < properties.Count; i++)
                        {
                        <td>
                            <span data-bind="text: @properties[i]"></span>

                        </td>
                        }
                </tr>

            </tbody>

        </table>
    </div>
    }

And in the .csthml-file I call it like this:

@helper.CustomTableControl("SomeKoObservableArray") 

Actually it does not make sense to call the document ready handler every time in the for-loop... right ?!

Its something like this post. Thanks.

Community
  • 1
  • 1
ASfdsa33
  • 97
  • 1
  • 9
  • 1
    Why dont you try to do this with knockout.js binding. So if you want to remove some tr and append some new bind this whole functionality to a dynamic array. Is that what you want to accomplish anyway? – Panagiotis Vrs Oct 10 '16 at 06:55
  • No, it doesn't make sense (that was the question, right? :D) – Icepickle Oct 10 '16 at 06:57
  • @Icepickle Ok, I see, but why does it work with the `document.ready` and without not:) ? – ASfdsa33 Oct 10 '16 at 07:00
  • Why not follow @PanagiotisVrs his advice, ifi you are binding your element, just make sure that the data is available in the viewmodel – Icepickle Oct 10 '16 at 07:02
  • 1
    @Panagiotis Vrs I added some additional code, so actually we receive an knockout binding through the helper like this `@helper.CustomTableControl("SomeKoObservableArray")` inside the `.csthml` – ASfdsa33 Oct 10 '16 at 07:09
  • How can I solve it better ? – ASfdsa33 Oct 10 '16 at 07:53

0 Answers0