【问题标题】:jqGrid: How to add edit and delete button in every row with form editing enabled?jqGrid:如何在启用表单编辑的每一行中添加编辑和删除按钮?
【发布时间】:2016-11-22 06:34:44
【问题描述】:

我有一个如下图所示的网格:

目前,我在页脚中有添加、编辑、删除按钮。我需要在每一行中都有“编辑”和“删除”按钮。当我点击每一行的编辑按钮时,它会显示编辑表单,当我点击提交时,它会将数据保存到数据库中。

我的代码如下所示:

    $(document).ready(function () {
        //jQuery.extend(jQuery.jgrid.defaults, { altRows:true });
        $("#list_records").jqGrid({
            url: 'ajaxELearningFetchTableData.php?table=GET_TRAINING_TYPE',
            //url: 'server.php',
            editurl: 'ajaxELearningSaveTrainingType.php',
            datatype: "json",
            colNames: ["TRAINING TYPE ID", "TRAINING TYPE NAME", "REMARKS"],
            colModel: [
                {
                    label: 'TRAINING TYPE ID',
                    name: 'TRAINING_TYPE_ID',
                    index: 'TRAINING_TYPE_ID',
                    editable: true,
                    sortable: true,
                    sorttype: "text",
                    editoptions: {readonly: "readonly"},
                    width: 40
                },
                {
                    label: 'TRAINING TYPE NAME',
                    name: 'TRAINING_TYPE_NAME',
                    index: 'TRAINING_TYPE_NAME',
                    width: 120,
                    editable: true, // must set editable to true if you want to make the field editable
                    editoptions: {size: 50, maxlength: 80},
                    editrules: {required: true, maxlength: 80},
                    sortable: true,
                    sorttype: "text",
                    // set options related to the layout of the Edit and Add Forms
                    formoptions: {
                        colpos: 1, // the position of the column
                        rowpos: 2, // the position of the row
                        label: "Training Type Name:", // the label to show for each input control  
                        elmsuffix: "(*)"

                    }
                },
                {
                    label: 'REMARKS',
                    name: 'REMARKS',
                    width: 140,
                    editable: true,
                    edittype: 'textarea',
                    editoptions: {rows: 3, cols: 45},
                    formoptions: {
                        colpos: 1,
                        rowpos: 3
                    }
                }
            ],
            loadonce: true,
            viewrecords: true,
            altRows: true,
//            width: auto,
//            height: auto,
            width: 1000,
            height: 300,
            rowNum: 10,
            rowList: [10, 20, 30],
            caption: "Training Type Information",
            sortname: 'TRAINING_TYPE_ID',
            sortorder: "asc",
            emptyrecords: "No Records to Display.",

            //footerrow: true,
            pager: "#perpage"
        });

        $('#list_records').navGrid('#perpage',
                // the buttons to appear on the toolbar of the grid
                        {edit: true, add: true, del: true, search: true, refresh: true, view: false, position: "left", cloneToTop: false},
                // options for the Edit Dialog
                        {
                            height: 'auto',
                            width: 620,
                            editCaption: "Edit Training Type",
                            url: 'ajaxSaveTrainingType.php',
                            recreateForm: true,
                            closeAfterEdit: true,
                            afterComplete: function (response) {
                                alert(response.responseText);
                            },

//                            afterShowForm: function(form) {
//                                form.closest('div.ui-jqdialog').center();
//                            },
                            afterSubmit: function () {
                                $(this).jqGrid("setGridParam", {datatype: 'json'});
                                return [true];
                                //location.reload(true);
                            }
                        },
                // options for the Add Dialog
                        {
                            height: 'auto',
                            width: 620,
                            addCaption: "Add Training Type",
                            url: 'ajaxSaveTrainingType.php',
                            closeAfterAdd: true,
                            recreateForm: true,
                            afterComplete: function (response) {
                                alert(response.responseText);
                            },
//                            afterShowForm: function(form) {
//                                form.closest('div.ui-jqdialog').center();
//                            },
//                           
                            afterSubmit: function () {
                                $(this).jqGrid("setGridParam", {datatype: 'json'});
                                return [true];
                                //location.reload(true);
                            }
                        },
                // options for the Delete Dailog
                        {
                            height: 'auto',
                            width: 620,
                            addCaption: "Delete Training Type",
                            url: 'ajaxSaveTrainingType.php',
                            closeAfterAdd: true,
                            recreateForm: true,
                            //rp_ge, postdata
                            onclickSubmit: function () {

                                var sel_id = $('#list_records').jqGrid('getGridParam', 'selrow');
                                var trainingTypeId = $('#list_records').jqGrid('getCell', sel_id, 'TRAINING_TYPE_ID');
                                return {tTypeId: trainingTypeId};


                            },
                            afterComplete: function (response) {
                                alert(response.responseText);
                            },
//                            afterShowForm: function(form) {
//                                form.closest('div.ui-jqdialog').center();
//                            },
                            afterSubmit: function () {
                                $(this).jqGrid("setGridParam", {datatype: 'json'});
                                return [true];
                                //location.reload(true);
                            }
                        });
            });

欢迎任何帮助。

谢谢。

【问题讨论】:

  • 有没有办法在窗口中间显示添加和编辑表单?

标签: php jqgrid


【解决方案1】:

您可以添加使用formatter: "actions"formatoptions: { editformbutton: true } 属性的列。如果你使用free jqGrid(它是我开发的jqGrid的fork),那么你可以使用template: "actions"

{ name: "act", template: "actions", formatoptions: { editformbutton: true } }

the demo 为例。

如果使用旧的 jqGrid(版本

{ name: "act", formatter: "actions", formatoptions: { editformbutton: true },
    width: 54, align: "center", fixed: true, hidedlg: true, resizable: false,
    sortable: false, search: false, editable: false, viewable: false }

【讨论】:

  • 它正在工作。但是网格没有加载更新的数据,并且在提交后没有关闭编辑表单。并且正在弹出宽度较小的editform对话框。
  • @Jahirul:你应该写得更清楚你的意思。首先,您是指我的演示还是您的一些代码?例如,编辑对话框将关闭,数据将在我的演示中更新。您使用哪个版本的 jqGrid 以及来自哪个分支的 jqGrid(free jqGrid、商业版Guriddo jqGrid JS 或版本 你的演示中使用formatter: "actions"template: "actions"时如何指定表单编辑的选项?
  • 我使用的是jqGrid 4.6.0版本
  • { name: "act", formatter: "actions", formatoptions: { editformbutton: true }, width: 54, align: "center", fixed: true, hidedlg: true, resizable: false ,可排序:false,搜索:false,可编辑:false,可查看:false }
  • 1) 我建议您从 jqGrid 4.6.0 更新到免费的 jqGrid 4.13.5。免费的 jqGrid 向后兼容 jqGrid 4.6.0,但它可以显着减少 JavaScript 代码并提高其性能。我建议您检查我的演示代码:例如formEditing 选项。 2) 如果不能更新,则必须使用formatoptionseditOptions 属性。有关详细信息,请参阅the wiki article
猜你喜欢
  • 1970-01-01
  • 2011-11-05
  • 2011-07-31
  • 2020-10-15
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多