可以在没有用户数据服务器请求的情况下填充 jqGrid 摘要页脚吗?

发布于 2024-10-31 13:38:01 字数 11541 浏览 0 评论 0原文

我已经详细介绍了该网站上提出的与填充 jqGrid 摘要页脚相关的每个问题,而没有将用户数据作为服务器请求。而 jqGrid wiki 资源却找不到答案。这可以吗?

我以多种典型方式使用 jqGrid 作为我的管理门户的一部分,但有一种特殊用途,我想要外观和效果。 jqGrid 的感觉是作为一个空的 UI 容器开始,用于某些用户交互,通过表单添加行,该表单不会在“提交”上发布(感谢下面的 Oleg 出色的脚本),而只是添加行并使用以下内容更新摘要页脚新添加的行中的值。我在 colModel 中有 'summarytype:"sum"',"grouping:true", footerrow: true, userDataOnFooter: true, altRows: true,在网格选项中,但除了本地数据字符串中 userdata 提供的初始值,摘要页脚值永远不会改变。似乎没有人愿意触及这个话题。是因为 jqGrid 的主要性质是它的数据库驱动功能吗?我在数据库驱动的立场中使用了大约 15 个 jqGrid 实例(其中许多现在处于生产服务中),但为了一致性起见需要使用它(我的所有 UI 都在 jqTabs 内),最初作为客户端 UI,没有服务器请求(所有内容稍后都会保存到数据库中)但我正在竭尽全力尝试仅在客户端上以编程方式操作摘要页脚。任何人都可以建议如何在添加新行时更新摘要页脚的值,该行的值将与任何现有行相加,并且不涉及到服务器的任何发布,仅在网格内更新?

提供的代码有点长,主要基于用户 Oleg 的解决方案,用于从模式表单添加行而不发布到服务器。我已将本地数组数据更改为 JSON 字符串,只是为了更好地理解我习惯的 xml 表示法。 jsonstring 使用一个默认行来初始化网格以供用户编辑。我省略了 jsonReader 因为网格不会用它来渲染。

简而言之,我想要做的就是在将新行添加到网格(此时没有发布到服务器)或编辑或删除时更新摘要页脚。当达到一组特定值时,将通过显示的按钮提示用户将行数据保存到数据库。

var lastSel, mydata = { "total": 1, "page": 1, "records": 1, "rows": [{ "id": acmid, "cell": ["0.00", "0.00", "0.00", "0.00"]}], "userdata":{ "mf": 0.00, "af":0.00,"pf":0.00,"cf":0.00 }}

grid = $("#ta_form_d"),
    onclickSubmitLocal = function (options, postdata) {

        var grid_p = grid[0].p,
            idname = grid_p.prmNames.id,
            grid_id = grid[0].id,
            id_in_postdata = grid_id + "_id",
            rowid = postdata[id_in_postdata],
            addMode = rowid === "_empty",
            oldValueOfSortColumn;

        // postdata has row id property with another name. we fix it:
        if (addMode) {
            // generate new id
            var new_id = grid_p.records + 1;
            while ($("#" + new_id).length !== 0) {
                new_id++;
            }
            postdata[idname] = String(new_id);
            //alert(postdata[idname]);
        } else if (typeof (postdata[idname]) === "undefined") {
            // set id property only if the property not exist
            postdata[idname] = rowid;
        }

        delete postdata[id_in_postdata];

        // prepare postdata for tree grid
        if (grid_p.treeGrid === true) {
            if (addMode) {
                var tr_par_id = grid_p.treeGridModel === 'adjacency' ? grid_p.treeReader.parent_id_field : 'parent_id';
                postdata[tr_par_id] = grid_p.selrow;
            }

            $.each(grid_p.treeReader, function (i) {
                if (postdata.hasOwnProperty(this)) {
                    delete postdata[this];
                }
            });
        }

        // decode data if there encoded with autoencode
        if (grid_p.autoencode) {
            $.each(postdata, function (n, v) {
                postdata[n] = $.jgrid.htmlDecode(v); // TODO: some columns could be skipped
            });
        }

        // save old value from the sorted column
        oldValueOfSortColumn = grid_p.sortname === "" ? undefined : grid.jqGrid('getCell', rowid, grid_p.sortname);
        //alert(oldValueOfSortColumn);
        // save the data in the grid
        if (grid_p.treeGrid === true) {
            if (addMode) {
                grid.jqGrid("addChildNode", rowid, grid_p.selrow, postdata);
            } else {
                grid.jqGrid("setTreeRow", rowid, postdata);
            }
        } else {

           if (addMode) {
                grid.jqGrid("addRowData", rowid, postdata, options.addedrow);


            } else {
                grid.jqGrid("setRowData", rowid, postdata);
            }
        }

        if ((addMode && options.closeAfterAdd) || (!addMode && options.closeAfterEdit)) {
            // close the edit/add dialog
            $.jgrid.hideModal("#editmod" + grid_id,
                              { gb: "#gbox_" + grid_id, jqm: options.jqModal, onClose: options.onClose });
        }

        if (postdata[grid_p.sortname] !== oldValueOfSortColumn) {
            // if the data are changed in the column by which are currently sorted
            // we need resort the grid
            setTimeout(function () {
                grid.trigger("reloadGrid", [{ current: true}]);
            }, 100);
        }

        // !!! the most important step: skip ajax request to the server
        this.processing = true;
        return {};



    },

    editSettings = {
        //recreateForm:true,
        jqModal: false,
        reloadAfterSubmit: false,
        closeOnEscape: true,
        savekey: [true, 13],
        closeAfterEdit: true,
        onclickSubmit: onclickSubmitLocal
    },

    addSettings = {
        //recreateForm:true,
        jqModal: false,
        reloadAfterSubmit: false,
        savekey: [true, 13],
        closeOnEscape: true,
        closeAfterAdd: true,
        onclickSubmit: onclickSubmitLocal
    },

    delSettings = {
        // because I use "local" data I don't want to send the changes to the server
        // so I use "processing:true" setting and delete the row manually in onclickSubmit
        onclickSubmit: function (options, rowid) {
            var grid_id = grid[0].id,
                grid_p = grid[0].p,
                newPage = grid[0].p.page;

            // delete the row
            grid.delRowData(rowid);
            $.jgrid.hideModal("#delmod" + grid_id,
                              { gb: "#gbox_" + grid_id, jqm: options.jqModal, onClose: options.onClose });

            if (grid_p.lastpage > 1) {// on the multipage grid reload the grid
                if (grid_p.reccount === 0 && newPage === grid_p.lastpage) {
                    // if after deliting there are no rows on the current page
                    // which is the last page of the grid
                    newPage--; // go to the previous page
                }
                // reload grid to make the row from the next page visable.
                grid.trigger("reloadGrid", [{ page: newPage}]);
            }

            return true;
        },
        processing: true
    };         //,
    /*initDateEdit = function (elem) {
        setTimeout(function () {
            $(elem).datepicker({
                dateFormat: 'dd-M-yy',
                autoSize: true,
                showOn: 'button', // it dosn't work in searching dialog
                changeYear: true,
                changeMonth: true,
                showButtonPanel: true,
                showWeek: true
            });
            //$(elem).focus();
        }, 100);
    },
    initDateSearch = function (elem) {
        setTimeout(function () {
            $(elem).datepicker({
                dateFormat: 'dd-M-yy',
                autoSize: true,
                //showOn: 'button', // it dosn't work in searching dialog
                changeYear: true,
                changeMonth: true,
                showButtonPanel: true,
                showWeek: true
            });
            //$(elem).focus();
        }, 100);
    };*/

    //jQuery("#ta_form_d").jqGrid({
    grid.jqGrid({
        // url:'/admin/tg_cma/ta_allocations.asp?acmid=' + acmid + '&mid=' + merchantid + '&approval_code=' + approval_code,
        //datatype: "local",
        //data: mydata,
        datatype: 'jsonstring',
        datastr: mydata,

        colNames: ['ID', 'Monthly Fees', 'ATM Fees', 'POS Fees', 'Card to Card Fees'],
        colModel: [
        { name: 'id', index: 'id', width: 90, align: "center", editable: true, editoptions: { size: 25 }, formoptions: { rowpos: 1, colpos: 1, label: "EzyAccount ID", elmprefix: "(*) " }, editrules: { required: true} },
        { name: 'mf', index: 'mf', width: 130, align: "right", formatter: 'number', editable: true, summaryType: 'sum', editoptions: { size: 25 }, formoptions: { rowpos: 2, colpos: 1, label: "Monthly Fee", elmprefix: "(*) " }, editrules: { required: true} },
        { name: 'af', index: 'af', width: 130, align: "right", formatter: 'number', editable: true, summaryType: 'sum', editoptions: { size: 25 }, formoptions: { rowpos: 3, colpos: 1, label: "ATM Fee", elmprefix: "(*) " }, editrules: { required: true} },
        { name: 'pf', index: 'pf', width: 130, align: "right", formatter: 'number', editable: true, summaryType: 'sum', editoptions: { size: 25 }, formoptions: { rowpos: 4, colpos: 1, label: "POS Fee", elmprefix: "(*) " }, editrules: { required: true} },
        { name: 'cf', index: 'cf', width: 130, align: "right", formatter: 'number', editable: true, summaryType: 'sum', editoptions: { size: 25 }, formoptions: { rowpos: 5, colpos: 1, label: "Card to Card Fee", elmprefix: "(*) " }, editrules: { required: true} }
    ],
        rowNum: 5,
        rowList: [5, 10, 20],
        pager: '#pta_form_d',
        toolbar: [true, "top"],
        width: 500,
        height: 100,
        editurl: 'clientArray',
        sortname: 'id',
        viewrecords: true,
        sortorder: "asc",
        multiselect: false,
        cellEdit: false,
        caption: "Allocations",
        grouping: true,
        /*groupingView: { 
            groupField: ['id', 'mf', 'af', 'pf', 'cf'],
            groupColumnShow: [true],
            groupText: ['<b>{0}</b>'],
            groupCollapse: false,
            groupOrder: ['asc'],
            groupSummary: [true],
            groupDataSorted: true 
        },*/
        footerrow: true,
        userDataOnFooter: true,
        altRows: true,
        ondblClickRow: function (rowid, ri, ci) {
            var p = grid[0].p;
            if (p.selrow !== rowid) {
                // prevent the row from be unselected on double-click
                // the implementation is for "multiselect:false" which we use,
                // but one can easy modify the code for "multiselect:true"
                grid.jqGrid('setSelection', rowid);
            }
            grid.jqGrid('editGridRow', rowid, editSettings);
        },
        onSelectRow: function (id) {
            if (id && id !== lastSel) {
                // cancel editing of the previous selected row if it was in editing state.
                // jqGrid hold intern savedRow array inside of jqGrid object,
                // so it is safe to call restoreRow method with any id parameter
                // if jqGrid not in editing state
                if (typeof lastSel !== "undefined") {
                    grid.jqGrid('restoreRow', lastSel);
                }
                lastSel = id;
            }
        },
        afterEditCell: function (rowid, cellname, value, iRow, iCol) {
            alert(iCol);
        },
        gridComplete: function () {

        },
        loadComplete: function (data) {


        }

    })
.jqGrid('navGrid', '#pta_form_d', {}, editSettings, addSettings, delSettings,
          { multipleSearch: true, overlay: false,
              onClose: function (form) {
                  // if we close the search dialog during the datapicker are opened
                  // the datepicker will stay opened. To fix this we have to hide
                  // the div used by datepicker
                  //$("div#ui-datepicker-div.ui-datepicker").hide();
              }
          });

$("#t_ta_form_d").append("<input type='button' class='add' value='Add New Allocation' style='height:20px; color:green; font-size:11px;' />");
$("input.add", "#t_ta_form_d").click(function () {
    jQuery("#ta_form_d").jqGrid('editGridRow', "new", {
        //recreateForm:true,
        jqModal: false,
        reloadAfterSubmit: false,
        savekey: [true, 13],
        closeOnEscape: true,
        closeAfterAdd: true,
        onclickSubmit: onclickSubmitLocal


    })
})

I have poured through every question asked on this website related to populating the jqGrid summary footer WITHOUT the userdata being a server request. And the jqGrid wiki resource and just cannot find an answer. Is this possible to do?

I use the jqGrid in many typical ways as part of my admin portal but have one particular use where I want the look & feel of the jqGrid to start out as an empty UI container of sorts for some user interaction adding rows through a form which doesn't post on Submit (thanks to Oleg's great script below) but simply adds the row and updates the summary footer with the values from the newly added row. I have 'summarytype:"sum"' in the colModel, "grouping:true", footerrow: true, userDataOnFooter: true, altRows: true, in the grid options but apart from the initial values supplied by the userdata in the local data string, the summary footer values never change. It seems like no one wants to touch this subject. Is it because the primary nature of the jqGrid is it's database driven functionality? I use about 15 instances of the jqGrid in it's database driven stance (many of which are in a production service now) but need to use it for consistency sake (all my UI's are inside jqTabs) initially as a client side UI with no server request (everything will be saved later to db) but I am pulling my hair out trying to manipulate the summary footer programmatically on and by the client only. Can anyone suggest how to cause values to the summary footer to update when a new row is added the values of which would be summed up with any existing rows and which does not involve any posting to server, just updates inside the grid?

The code supplied is kind of long and based primarily on user Oleg's solution for adding a row from a modal form without posting to the server. I've changed the local array data to JSON string simply to better understand the notation as I'm used to xml. The jsonstring initializes the grid with one default row for the user to edit. I've left out the jsonReader because the grid would not render with it.

In a nutshell then what I want to do is to have the summary footer update when a new row is added to the grid (no posting to server occurring at this point) or edited or deleted. When a certain set of values is achieved the user is prompted by a displayed button to save row data to db.

var lastSel, mydata = { "total": 1, "page": 1, "records": 1, "rows": [{ "id": acmid, "cell": ["0.00", "0.00", "0.00", "0.00"]}], "userdata":{ "mf": 0.00, "af":0.00,"pf":0.00,"cf":0.00 }}

grid = $("#ta_form_d"),
    onclickSubmitLocal = function (options, postdata) {

        var grid_p = grid[0].p,
            idname = grid_p.prmNames.id,
            grid_id = grid[0].id,
            id_in_postdata = grid_id + "_id",
            rowid = postdata[id_in_postdata],
            addMode = rowid === "_empty",
            oldValueOfSortColumn;

        // postdata has row id property with another name. we fix it:
        if (addMode) {
            // generate new id
            var new_id = grid_p.records + 1;
            while ($("#" + new_id).length !== 0) {
                new_id++;
            }
            postdata[idname] = String(new_id);
            //alert(postdata[idname]);
        } else if (typeof (postdata[idname]) === "undefined") {
            // set id property only if the property not exist
            postdata[idname] = rowid;
        }

        delete postdata[id_in_postdata];

        // prepare postdata for tree grid
        if (grid_p.treeGrid === true) {
            if (addMode) {
                var tr_par_id = grid_p.treeGridModel === 'adjacency' ? grid_p.treeReader.parent_id_field : 'parent_id';
                postdata[tr_par_id] = grid_p.selrow;
            }

            $.each(grid_p.treeReader, function (i) {
                if (postdata.hasOwnProperty(this)) {
                    delete postdata[this];
                }
            });
        }

        // decode data if there encoded with autoencode
        if (grid_p.autoencode) {
            $.each(postdata, function (n, v) {
                postdata[n] = $.jgrid.htmlDecode(v); // TODO: some columns could be skipped
            });
        }

        // save old value from the sorted column
        oldValueOfSortColumn = grid_p.sortname === "" ? undefined : grid.jqGrid('getCell', rowid, grid_p.sortname);
        //alert(oldValueOfSortColumn);
        // save the data in the grid
        if (grid_p.treeGrid === true) {
            if (addMode) {
                grid.jqGrid("addChildNode", rowid, grid_p.selrow, postdata);
            } else {
                grid.jqGrid("setTreeRow", rowid, postdata);
            }
        } else {

           if (addMode) {
                grid.jqGrid("addRowData", rowid, postdata, options.addedrow);


            } else {
                grid.jqGrid("setRowData", rowid, postdata);
            }
        }

        if ((addMode && options.closeAfterAdd) || (!addMode && options.closeAfterEdit)) {
            // close the edit/add dialog
            $.jgrid.hideModal("#editmod" + grid_id,
                              { gb: "#gbox_" + grid_id, jqm: options.jqModal, onClose: options.onClose });
        }

        if (postdata[grid_p.sortname] !== oldValueOfSortColumn) {
            // if the data are changed in the column by which are currently sorted
            // we need resort the grid
            setTimeout(function () {
                grid.trigger("reloadGrid", [{ current: true}]);
            }, 100);
        }

        // !!! the most important step: skip ajax request to the server
        this.processing = true;
        return {};



    },

    editSettings = {
        //recreateForm:true,
        jqModal: false,
        reloadAfterSubmit: false,
        closeOnEscape: true,
        savekey: [true, 13],
        closeAfterEdit: true,
        onclickSubmit: onclickSubmitLocal
    },

    addSettings = {
        //recreateForm:true,
        jqModal: false,
        reloadAfterSubmit: false,
        savekey: [true, 13],
        closeOnEscape: true,
        closeAfterAdd: true,
        onclickSubmit: onclickSubmitLocal
    },

    delSettings = {
        // because I use "local" data I don't want to send the changes to the server
        // so I use "processing:true" setting and delete the row manually in onclickSubmit
        onclickSubmit: function (options, rowid) {
            var grid_id = grid[0].id,
                grid_p = grid[0].p,
                newPage = grid[0].p.page;

            // delete the row
            grid.delRowData(rowid);
            $.jgrid.hideModal("#delmod" + grid_id,
                              { gb: "#gbox_" + grid_id, jqm: options.jqModal, onClose: options.onClose });

            if (grid_p.lastpage > 1) {// on the multipage grid reload the grid
                if (grid_p.reccount === 0 && newPage === grid_p.lastpage) {
                    // if after deliting there are no rows on the current page
                    // which is the last page of the grid
                    newPage--; // go to the previous page
                }
                // reload grid to make the row from the next page visable.
                grid.trigger("reloadGrid", [{ page: newPage}]);
            }

            return true;
        },
        processing: true
    };         //,
    /*initDateEdit = function (elem) {
        setTimeout(function () {
            $(elem).datepicker({
                dateFormat: 'dd-M-yy',
                autoSize: true,
                showOn: 'button', // it dosn't work in searching dialog
                changeYear: true,
                changeMonth: true,
                showButtonPanel: true,
                showWeek: true
            });
            //$(elem).focus();
        }, 100);
    },
    initDateSearch = function (elem) {
        setTimeout(function () {
            $(elem).datepicker({
                dateFormat: 'dd-M-yy',
                autoSize: true,
                //showOn: 'button', // it dosn't work in searching dialog
                changeYear: true,
                changeMonth: true,
                showButtonPanel: true,
                showWeek: true
            });
            //$(elem).focus();
        }, 100);
    };*/

    //jQuery("#ta_form_d").jqGrid({
    grid.jqGrid({
        // url:'/admin/tg_cma/ta_allocations.asp?acmid=' + acmid + '&mid=' + merchantid + '&approval_code=' + approval_code,
        //datatype: "local",
        //data: mydata,
        datatype: 'jsonstring',
        datastr: mydata,

        colNames: ['ID', 'Monthly Fees', 'ATM Fees', 'POS Fees', 'Card to Card Fees'],
        colModel: [
        { name: 'id', index: 'id', width: 90, align: "center", editable: true, editoptions: { size: 25 }, formoptions: { rowpos: 1, colpos: 1, label: "EzyAccount ID", elmprefix: "(*) " }, editrules: { required: true} },
        { name: 'mf', index: 'mf', width: 130, align: "right", formatter: 'number', editable: true, summaryType: 'sum', editoptions: { size: 25 }, formoptions: { rowpos: 2, colpos: 1, label: "Monthly Fee", elmprefix: "(*) " }, editrules: { required: true} },
        { name: 'af', index: 'af', width: 130, align: "right", formatter: 'number', editable: true, summaryType: 'sum', editoptions: { size: 25 }, formoptions: { rowpos: 3, colpos: 1, label: "ATM Fee", elmprefix: "(*) " }, editrules: { required: true} },
        { name: 'pf', index: 'pf', width: 130, align: "right", formatter: 'number', editable: true, summaryType: 'sum', editoptions: { size: 25 }, formoptions: { rowpos: 4, colpos: 1, label: "POS Fee", elmprefix: "(*) " }, editrules: { required: true} },
        { name: 'cf', index: 'cf', width: 130, align: "right", formatter: 'number', editable: true, summaryType: 'sum', editoptions: { size: 25 }, formoptions: { rowpos: 5, colpos: 1, label: "Card to Card Fee", elmprefix: "(*) " }, editrules: { required: true} }
    ],
        rowNum: 5,
        rowList: [5, 10, 20],
        pager: '#pta_form_d',
        toolbar: [true, "top"],
        width: 500,
        height: 100,
        editurl: 'clientArray',
        sortname: 'id',
        viewrecords: true,
        sortorder: "asc",
        multiselect: false,
        cellEdit: false,
        caption: "Allocations",
        grouping: true,
        /*groupingView: { 
            groupField: ['id', 'mf', 'af', 'pf', 'cf'],
            groupColumnShow: [true],
            groupText: ['<b>{0}</b>'],
            groupCollapse: false,
            groupOrder: ['asc'],
            groupSummary: [true],
            groupDataSorted: true 
        },*/
        footerrow: true,
        userDataOnFooter: true,
        altRows: true,
        ondblClickRow: function (rowid, ri, ci) {
            var p = grid[0].p;
            if (p.selrow !== rowid) {
                // prevent the row from be unselected on double-click
                // the implementation is for "multiselect:false" which we use,
                // but one can easy modify the code for "multiselect:true"
                grid.jqGrid('setSelection', rowid);
            }
            grid.jqGrid('editGridRow', rowid, editSettings);
        },
        onSelectRow: function (id) {
            if (id && id !== lastSel) {
                // cancel editing of the previous selected row if it was in editing state.
                // jqGrid hold intern savedRow array inside of jqGrid object,
                // so it is safe to call restoreRow method with any id parameter
                // if jqGrid not in editing state
                if (typeof lastSel !== "undefined") {
                    grid.jqGrid('restoreRow', lastSel);
                }
                lastSel = id;
            }
        },
        afterEditCell: function (rowid, cellname, value, iRow, iCol) {
            alert(iCol);
        },
        gridComplete: function () {

        },
        loadComplete: function (data) {


        }

    })
.jqGrid('navGrid', '#pta_form_d', {}, editSettings, addSettings, delSettings,
          { multipleSearch: true, overlay: false,
              onClose: function (form) {
                  // if we close the search dialog during the datapicker are opened
                  // the datepicker will stay opened. To fix this we have to hide
                  // the div used by datepicker
                  //$("div#ui-datepicker-div.ui-datepicker").hide();
              }
          });

$("#t_ta_form_d").append("<input type='button' class='add' value='Add New Allocation' style='height:20px; color:green; font-size:11px;' />");
$("input.add", "#t_ta_form_d").click(function () {
    jQuery("#ta_form_d").jqGrid('editGridRow', "new", {
        //recreateForm:true,
        jqModal: false,
        reloadAfterSubmit: false,
        savekey: [true, 13],
        closeOnEscape: true,
        closeAfterAdd: true,
        onclickSubmit: onclickSubmitLocal


    })
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡紫姑娘! 2024-11-07 13:38:01

如果是“本地”数据类型,可以使用 footerData 方法设置(或获取)页脚中的数据。另外,可以使用方法 getCol 来计算总和列中的元素。

查看答案了解更多信息信息。我希望它能解决你的问题。

In case of "local" datatype one can use footerData method to set (or get) the data in the footer. Additionally the method getCol can be used co calculate the sum of elements in the column.

Look at the answer for more information. I hope it will solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文