Kendo Grid:列标题复选框'全部检查'检查网格所有页面

时间:2015-05-29 20:07:31

标签: checkbox kendo-ui pagination grid

在javascript和/或jquery中是否有一个示例,其中某人有一个“全部检查”复选框,用于检查网格所有页面中的所有复选框?

我现在已经尝试了3天,但我找不到干净的答案。

我现在拥有的是丑陋的,并没有真正起作用......

        function onRequestEnd(e) {
            var masterCbChecked = $("#masterCheckBox").is(':checked');
            var grid = $("#grid").data("kendoGrid");

            for (var i = 0; i < grid.dataSource.total(); i ++) {
                var dataRow = $("#grid").data("kendoGrid").dataSource.data()[i];
                var elementRow = grid.table.find(".cbAdvisor")[i];
                if (elementRow != null) {
                    var checked = elementRow.checked,
                        row = $(elementRow).closest("tr"),
                        dataItem = grid.dataItem(grid.tbody.find("tr").eq(i));
                    checkedIds[dataItem.DimAgentId] = masterCbChecked;
                    if (masterCbChecked) {
                        //-select the row
                        elementRow.checked = true;
                        row.addClass("k-state-selected");
                        dataRow.IsSelected = true;
                    } else {
                        //-remove selection
                        elementRow.checked = false;
                        row.removeClass("k-state-selected");
                        dataRow.IsSelected = false;
                    }
                }
            }

        }

        function checkAll(ele) {
            var state = $(ele).is(':checked');
            var grid = $("#grid").data("kendoGrid");
            //grid.dataSource.pageSize(grid.dataSource.total());
            //grid.dataSource.read();
            //grid.refresh();

            var currentPage = $("#grid").data("kendoGrid").dataSource.page();

            checkedIds = {};

            //for (var a = 0; a < modelJson.length; a ++) {
            //    var m = modelJson[a];
            //    m.IsSelected = true;
            //}
            for (var a = 1; a < 2; a ++) {
                var pager = grid.pager;
                pager.bind('change', a);
                grid.one("dataBound", function () {
                    this.dataSource.page(a);
                });
                grid.dataSource.fetch();

                for (var i = 0; i < grid.dataSource.total(); i ++) {
                    var dataRow = $("#grid").data("kendoGrid").dataSource.data()[i];
                    var elementRow = grid.table.find(".cbAdvisor")[i];
                    if (elementRow != null) {
                        var checked = elementRow.checked,
                            row = $(elementRow).closest("tr"),
                            dataItem = grid.dataItem(grid.tbody.find("tr").eq(i));
                        checkedIds[dataItem.DimAgentId] = state;
                        if (state) {
                            //-select the row
                            elementRow.checked = true;
                            row.addClass("k-state-selected");
                            dataRow.IsSelected = true;
                        } else {
                            //-remove selection
                            elementRow.checked = false;
                            row.removeClass("k-state-selected");
                            dataRow.IsSelected = false;
                        }
                    }
                }

                pager.bind('change', currentPage);
                grid.one("dataBound", function () {
                    this.dataSource.page(currentPage);
                });
                grid.dataSource.fetch();

                //mark for paging
                if (dataRow != null) {
                    if (state) {
                        dataRow.IsSelected = true;
                    } else {
                        dataRow.IsSelected = false;
                    }
                }

            };

            if (!state) {
                checkedIds = {};
            }

            //grid.dataSource.pageSize(50);
            //grid.refresh();
        }

3 个答案:

答案 0 :(得分:3)

我通过一些修改得到了这个代码:

columns.Template(@<text><input name="chkStatus" type="checkbox" class="chkFormols" /></text>)
.HeaderTemplate("<input type='checkbox' id='chkSelectAll' onclick='checkAll(this)' />").Width(50);



function checkAll(ele) {
            debugger;
            var state = $(ele).is(':checked');
            var grid = $('#grid').data('kendoGrid');
            if (state == true) {
                $('.chkFormols').prop('checked', true);
            }
            else {

                $('.chkFormols').prop('checked', false);
            }
        };

答案 1 :(得分:1)

我解决了这个问题。

    <div id="panelGridContainer" class="containerGrid">
        @(Html.Kendo().Grid<ReportGridModel>()
              .Name("grid")
              .Columns(columns =>
              {
                  var m = Model.Section.DrilldownColumns[0];
                  columns.BoundAll(p => p.DimAgentId, m);
                  columns.Template(@<text></text>).ClientTemplate("<input type='checkbox' #= IsSelected ? checked='checked':'' # class='cbAdvisor' />")
                      .HeaderTemplate("<input type='checkbox' id='masterCheckBox' onclick='checkAll(this)'/>").Width(30);
                  columns.BoundAll(p => p.AdvisorName, m).ClientTemplate("" + "#: AdvisorName #" + "<div class='reportDetailLink ui-button ui-widget ui-button-text-only' onclick='getAdvisorDetailView(this)'>Detail</div>").Width(300);
                  columns.BoundAll(p => p.GoalAmount, m).ClientTemplate("" + "$#: GoalAmount.formatMoney(0, '.', ',') #" + "<div id='editIndividualGoalLink' class='reportEditLink ui-button ui-widget ui-button-text-only' onclick='openFytaIndividualGoalOverlay(this)'><i class='icon icon-pencil'></i> Edit</div><div class='editIndGoalPanel' style='display:none;'></div>").Width(200);
                  columns.BoundAll(p => p.RevenueAmount, m).Width(100);
                  columns.BoundAll(p => p.GoalProgressToDate, m).Width(75);
                  columns.BoundAll(p => p.RevenueChangeYoY, m).Width(75);
                  columns.BoundAll(p => p.BranchComposer, m).Width(200);
              })
              .Sortable()
              .Pageable(pager => pager.PageSizes(new[] { 50, 100, 500, 1000 }))
        //.Scrollable()
              .DataSource(dataSource => dataSource
                  .Ajax()
                  .PageSize(50)
                  .Read(read => read.Action("GetReportGridData", "FytByAdvisor"))
                  .Events(events => events.RequestStart("onDataSourceRequestStart"))
              )
              .Events(events => events.DataBound("onGridDataFound").DataBound("onDataBound")))
    </div>





        var checkedIds = {};

        //on click of the checkbox:
        $('#grid').on('click', '.cbAdvisor', function() {
            var checked = this.checked,
                row = $(this).closest("tr"),
                grid = $("#grid").data("kendoGrid"),
                dataItem = grid.dataItem(row);

            if (!checked) {
                $('#masterCheckBox').attr('checked', false); // Unchecks it
            }

            checkedIds[dataItem.DimAgentId] = checked;
            if (checked) {
                //-select the row
                row.addClass("k-state-selected");
            } else {
                //-remove selection
                row.removeClass("k-state-selected");
            }
        });

        //on dataBound event restore previous selected rows:
        function onDataBound(e) {
            var view = this.dataSource.view();
            for(var i = 0; i < view.length;i++){
                if(checkedIds[view[i].DimAgentId]){
                    this.tbody.find("tr[data-uid='" + view[i].uid + "']")
                        .addClass("k-state-selected")
                        .find(".cbAdvisor")
                        .attr("checked","checked");
                }
            }

            var masterCbChecked = $("#masterCheckBox").is(':checked');
            var grid = $("#grid").data("kendoGrid");

            //if(!masterCbChecked)
            //{
            //    checkedIds = {};
            //}
            if(masterCbChecked)
            {
                for (var i = 0; i < grid.dataSource.total(); i ++) {
                    var dataRow = $("#grid").data("kendoGrid").dataSource.data()[i];
                    var elementRow = grid.table.find(".cbAdvisor")[i];
                    if (elementRow != null) {
                        var checked = elementRow.checked,
                            row = $(elementRow).closest("tr"),
                            dataItem = grid.dataItem(grid.tbody.find("tr").eq(i));
                        if (masterCbChecked) {
                            checkedIds[dataItem.DimAgentId] = masterCbChecked;
                            //-select the row
                            elementRow.checked = true;
                            row.addClass("k-state-selected");
                            dataRow.IsSelected = true;
                        } else {
                            //-remove selection
                            elementRow.checked = false;
                            row.removeClass("k-state-selected");
                            dataRow.IsSelected = false;
                        }
                    }
                }
            }
        }


        function checkAll(ele) {
            var state = $(ele).is(':checked');
            var grid = $("#grid").data("kendoGrid");
            grid.dataSource.pageSize(grid.dataSource.total());
            grid.dataSource.read();
            grid.refresh();

            checkedIds = {};


            for (var i = 0; i < grid.dataSource.total(); i ++) {
                var dataRow = $("#grid").data("kendoGrid").dataSource.data()[i];
                var elementRow = grid.table.find(".cbAdvisor")[i];
                if (elementRow != null) {
                    var checked = elementRow.checked,
                        row = $(elementRow).closest("tr"),
                        dataItem = grid.dataItem(grid.tbody.find("tr").eq(i));

                    if (state) {
                        checkedIds[dataItem.DimAgentId] = state;
                        //-select the row
                        elementRow.checked = true;
                        row.addClass("k-state-selected");
                        dataRow.IsSelected = true;
                    } else {
                        //-remove selection
                        elementRow.checked = false;
                        row.removeClass("k-state-selected");
                        dataRow.IsSelected = false;
                    }
                }

                //mark for paging
                if (dataRow != null) {
                    if (state) {
                        dataRow.IsSelected = true;
                    } else {
                        dataRow.IsSelected = false;
                    }
                }

            };

            // set back to 50 page size
            grid.dataSource.pageSize(50);
        }

答案 2 :(得分:1)

试试这段代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
      "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
      "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
      <definition name="common" template="/WEB-INF/layout/classic.jsp">
            <put-attribute name="footer" value="/WEB-INF/layout/footer.jsp" />
      </definition>

      <definition name="ndex" extends="common">
            <put-attribute name="title" value="Tuk sam!" />
            <put-attribute name="body" value="/WEB-INF/jsp/index.jsp" />
      </definition>

</tiles-definitions>