【问题标题】:jqGrid Act Strange Reloading Data After Insert and UpdatejqGrid 插入和更新后奇怪的重新加载数据
【发布时间】:2013-06-28 17:45:51
【问题描述】:

JqGrid 行为怪异。我只是创建了一些母版页,其中包含引用数据库中某个表的 jqGrid。

此页面运行良好,然后我需要使用相同的逻辑,并复制带有附加 php 文件的确切页面并创建一些调整,使其指向正确的表。

在我的母版页(部门页——这是第一页(100% 工作))中,jqgrid 函数工作正常,但是在基于第一页的第二页和第三页中,jqgrid 表现得很奇怪。

当我创建新值或更新某些值时,jqgrid 应该自动重新加载带有新数据的网格。但就我而言,网格会重新加载数据,但根本不写入。

这种奇怪的行为没有发生在我的第一个 jQGrid 页面(部门页面)中。

我还插入了一些屏幕截图以使其更清晰

然后简单地将值添加到网格中,参考一些 php 文件。该值被执行,然后存储在数据库中。此方法使用 POST 方法。

然后 jQgrid 会自动从数据库中获取新数据,并且应该写在 GRID 上。但在我的案例中,网格没有写入数据。

如您在屏幕截图的右下角看到的,有 11 个值,从第一个屏幕截图开始,只有 10 个值。所以,网格实际上执行了 INSERT 语句,但是当它重新加载时,它就坏了。

有没有办法克服这个问题?谢谢。

已编辑:HTML代码:

<table id="location"><tr><td /></tr></table>
<div id="pager-location"></div>

JavaScript 代码:

$(document).ready(function() {
    //alert("start");
    jQuery("#location").jqGrid({
        mtype:'GET',
        url:'functions/get_location.php',
        editurl:'functions/edit_location.php',
        datatype: "JSON",
        colNames:['Location ID','Location'],
        colModel:[
            {name:'idms_location',index:'idms_location', width:150, editable:true,add:true, del:true, key:true},
            {name:'location',index:'location', width:800,editable:true, add:true, del:true}     
        ],
        loadComplete: function () {
        alert("OK");
        },    
        loadError: function (jqXHR, textStatus, errorThrown) {
            alert('HTTP status code: ' + jqXHR.status + '\n' +
                  'textStatus: ' + textStatus + '\n' +
                  'errorThrown: ' + errorThrown);
            alert('HTTP message body (jqXHR.responseText): ' + '\n' + jqXHR.responseText);
        },
        rowNum:10,
        rowList:[5,10,15],
        pager: '#pager-location',
        sortname: 'idms_location',
        viewrecords: true,
        jsonReader: {repeatitems: true, idms_location: "idms_location" },
        sortorder: "asc",
        caption:"MSC Locations"
    });
    jQuery("#location").jqGrid('navGrid','#pager-location',{edit:true,add:true,del:true},{},{},{},{closeAfterSearch:true},{});
    jQuery("#location").jqGrid('gridResize',{minWidth:350,maxWidth:850,minHeight:80, maxHeight:350});
    //alert("end");
});

【问题讨论】:

    标签: php mysql json jqgrid reload


    【解决方案1】:

    我验证了您使用的代码并找到了原因。 您的代码中有id 重复问题。您定义了用于 jqGrid 的 &lt;table&gt; 元素如下

    <table id="location"><tr><td /></tr></table>
    <div id="pager-location"></div>
    

    它有"location" 作为id。后来你定义了

    colModel: [
        {name:'idms_location',index:'idms_location', width:150, editable:true,add:true, del:true, key:true},
        {name:'location',index:'location', width:800,editable:true, add:true, del:true}     
    ],
    

    名称location 将用作列名。问题是列名将用于构建网格中不同元素的id 名称。另外表单编辑使用列名直接作为&lt;input&gt;字段的id值,代表位置。使用后添加表单以下元素

    <input name="location" class="FormElement ui-widget-content ui-corner-all" id="location" role="textbox" type="text">
    

    也存在于带有id="location" 的页面上。如果用户关闭表单,它将被隐藏,但不会被销毁。因为编辑表单将放置在页面之前 &lt;table id="location"&gt; 下一个$("#location tbody:first")the line 中使用,所以没有找到更多表格并且网格保持空白。

    你应该做的只是将&lt;table id="location"&gt;重命名为&lt;table id="grid-location">`或选择任何其他名称。你应该更新相应的JavaScript代码。

    应在网格中完成的其他更改:

    • jsonReader: {repeatitems: true, idms_location: "idms_location" } 更改为jsonReader: {id: "idms_location" }
    • 添加gridview: true 选项。
    • 添加autoencode: true 选项。
    • colModel 中删除不存在的选项add:true, del:true 属性
    • colModel 中删除index 属性。
    • 您应该修复Content-Type HTTP 标头,您在服务器响应中使用 JSON 数据。它应该是 Content-Type: application/json 而不是您当前使用的 Content-Type: text/html。这只是一行 PHP 代码。
    • 您可以删除 navGrid{edit:true,add:true,del:true} 选项 - 这是默认选项。

    【讨论】:

    • 我明白了,它现在有效,但还有一个问题,为什么这在部门页面中有效,但在另一个页面中无效?
    • @randytan:您在“部门”页面上使用了不同的名称。该表有id="departments"(末尾有“s”),列名是"department"(末尾没有“s”)。
    • 天哪!只是因为's'。这让我今天真的很困惑。感谢奥列格的大力帮助!祝你有美好的一天。
    • @randytan:再说一句:你几乎不使用答案和问题的投票。这是非常重要的权利,因为投票是搜索引擎使用的主要标准。您每天有权投票 30 个问题或答案(请参阅 here)。因此,如果您想帮助其他用户找到一些您认为有帮助的答案,您应该投票。我个人有足够的声誉,我不能使用。我只想帮助有同样问题的其他用户找到答案。您不仅可以对问题的答案进行投票,还可以对任何有帮助的信息进行投票
    • 我明白了,我只是在这里提问,并没有阅读stackoverflow 101。哈哈哈。 Anywany oleg,似乎如果我想添加新值,似乎只有 10 个字符适合。我必须在哪里添加一些脚本?
    猜你喜欢
    • 1970-01-01
    • 2011-09-11
    • 2011-11-07
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多