jeudi 26 juillet 2018

Check a checkbox in partial view from Main view

A lot of questions on checkboxes, but none helped in this matter. I want to check my checkboxes in my PartialView FROM my MainView.

Please look at my Ajax function on my MainView

$.ajax({
                url: '@Url.Action("Show", "Moderator")',
                data: { search: v, productName: p }

            }).done(function(data) {
                CloseWaitDialogue();
                $('#gridContent').html(data);
                $res = $(data).filter('#qFilterGrid'); //Finding this ID in my Partial View

                if ($res) {

                    $("#qFilterGrid th").each(function() {

                        if ($.trim($(this).text().toString().toLowerCase()) === "{checkall}") {
                            $(this).text('');
                            $("<input/>",
                                {
                                    type: "checkbox",
                                    id: "cbSelectAll1",
                                    value: "",
                                    class: "selectall",
                                    onclick: "SelectCheckBox(this);"
                                }).appendTo($(this));
                            $(this).append("<span>EnableALL</span>");
                        }

                    });
                }

            }).fail(function() {
                alert("FAIL");
            });

This is my partial view:

grid.Columns(
                grid.Column("Sl.No", format: @<text>
                                                 <div>
                                                     <span class="sl-holder">@( item.WebGrid.Rows.IndexOf(item) + 1)</span> <input type="hidden" value="@item.Id" class="p-id"/>
                                                 </div> </text>, canSort: false),
                grid.Column(
                    format: @<text>
                                @if (@item.EnableQueue)
                                {
                                    <text> <input id=@( item.WebGrid.Rows.IndexOf(item) + 1) type="checkbox" value="@item.EnableQueue" checked disabled="disabled" name="ids" class="disabled-true"/></text>
                                }
                                else
                                {
                                    <text><input id=@( item.WebGrid.Rows.IndexOf(item) + 1) type="checkbox" value="@item.EnableQueue" name="ids" class="dis"/></text>
                                }
                             </text>,
                    header: "{checkall}", style: "width"
                    ),
                grid.Column("Project", format: @<text> <span class="display-mode">@item.Project </span> </text>),
                grid.Column("Version", format: @<text> <span class="display-mode">@item.Version</span></text>),
                grid.Column("Mode", format: @<text><span class="display-mode lblMode">@item.Mode</span><label class="edit-mode lblMode"></label></text>, canSort: false),
                grid.Column("Priority", "Priority", format: @<text>
                                                                <span class="display-mode">
                                                                    <label class="lblPriority">@item.Priority</label>
                                                                </span> </text>, canSort: false))

As you can see, I want the check boxes in my partial view to be checked from my main view.

Some of my attempts that didn't work.

Attempt 1

$("#cbSelectAll1").on("click", function () {

        debugger;
        var ischecked = this.checked;
        $('#qFilterGrid tr').each(function () {

            if ($(this)[0].style.cssText === "display: table-row;" || $(this)[0].style.cssText == "") {
                $(this).find("input:checkbox").each(function () {
                    if (this.disabled != true) {
                        this.checked = ischecked;
                    }


                });
            }

        });


    });

Attempt 2

function SelectCheckBox(obj) {

        var c = new Array();
        c = document.getElementsByTagName('input');
        for (var i = 0; i < c.length; i++) {
            if (c[i].type == 'checkbox') {
                c[i].checked = obj.checked;
            }
        }
    }

I tried a couple of more but none helped.

What should I write in SelectCheckBox(); function? Thank you.




Aucun commentaire:

Enregistrer un commentaire