当我已经使用idPrefix时,行会重复 [英] Rows are duplicated when I have already used idPrefix

查看:64
本文介绍了当我已经使用idPrefix时,行会重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个页面上,我有多个网格,其中一些值是从同一数据库中查询的.当网格不断刷新时,我得到了重复的行.这是为什么?硬刷新页面时,重复的行消失了.

On a single page, I have multiple grids where some of the values are same/queried from the same database. While the grid keeps refreshing itself, I get duplicate rows. Why is that? The duplicate rows disappear when I hard refresh the page.

我用过:

idPrefix: $(gridObject).attr('id') + "_" 
// gridObject is a reference to a particular jqGrid

对于页面上的每个jqGrid

,但行ID的重复如下所示:

for each jqGrid on the page, but the row id's are duplicated as shown below:

界面外观的快照:

应该只有2个组头dhoopafshan.这可能是什么原因?我正在使用jqGrid 4.6.0

There should be only 2 group headers dhoop and afshan. What could be the reason for this? I am using jqGrid 4.6.0

推荐答案

对于您发布的代码,我不想写太多评论.我只描述与您发布的问题有关的部分.

I don't want to write too much critic about the code which you posted. I describe only the part which concerned the problem which you posted.

在我看来,服务器代码向jqGrid返回了不正确的数据.您从服务器加载数据(datatype不是"local").这意味着服务器代码必须准备数据进行分组.了解加载数据的两个基本知识很重要:

It seems to me that the server code returns incorrect data to jqGrid. You loads the data from the server (datatype is not "local"). It means that the server code have to prepare the data for grouping. It's important to understand two base things for loading the data:

  • 数据项必须具有唯一的ID,您应该正确地告知jqGrid该项中的哪个值是ID.
  • 从服务器返回的数据必须正确排序.

例如,您使用选项groupingView: {groupField: "column1", ...}sortname: "column2",则必须在服务器上按column1, column2对数据进行排序.我的意思是,第一个排序条件必须是分组字段(字段)(列column1),第二个排序条件必须是column2(来自sortname: "column2").我想数据在后端被错误地排序了.您的问题描述:我得到重复的行.为什么?当我硬刷新页面时,重复的行会消失."让我们假设一下.

It you for example use the options groupingView: {groupField: "column1", ...} and sortname: "column2" then the data have to be sorted by column1, column2 on the server. I mean that the first sorting criteria have to be the grouping field (fields) (the column column1) and the second sorting criteria have to be column2 (from sortname: "column2"). I suppose that the data are incorrectly sorted on the backend. Your problem description: "I get duplicate rows. Why is that? The duplicate rows disappear when I hard refresh the page." let to suppose this.

其次,您在对问题的评论中写道,返回数据的每一项的格式都类似于{"JobID":"1","FileName":" ","StartIndex":"0","EndIndex":"0","SplitIndex":"0","Duration":"0","FileSize":"0","LogStatus":" ","Name":"afshaan"}.我可以猜测JobID属性的值对于每个项目都是唯一的.如果正确的jsonReader

Seconds, you wrote in the comment to your question, that the format of every item of returned data looks like {"JobID":"1","FileName":" ","StartIndex":"0","EndIndex":"0","SplitIndex":"0","Duration":"0","FileSize":"0","LogStatus":" ","Name":"afshaan"}. I can guess that the value from JobID property is unique for every item. In the case the correct jsonReader would be

jsonReader: {
    repeatitems: false,
    id: "JobID"
}

jsonReader通知jqGrid如何解析输入数据项.您使用以下jsonReader代替

The jsonReader informs jqGrid how to parse the items of input data. You use the following jsonReader instead

jsonReader : {
    root: "rows",
    records: "records",
    viewrecords: true,
    repeatitems: true,
    cell: "",
    id: "0"
}

这似乎是错误的.

此外,您使用jqGrid的datatype参数定义为函数.我在stackoverflow上看到的所有使用datatype作为函数的代码示例都是错误的.这些示例来自为旧版本的jqGrid(版本3.6或更早版本)创建的一些旧演示.我强烈建议您使用datatype: "json"代替.在我看来,您只需要添加选项ajaxGridOptions: { contentType: "application/json" }并使用正确的jsonReader.我猜想可能是

Moreover you use datatype parameter of jqGrid defined as function. All code example which I see on stackoverflow and which uses datatype as function was wrong. The examples come from some old demos created for very old version of jqGrid (version 3.6 or earlier). I would strictly recommend you to use datatype: "json" instead. It seems to me that you need just add the option ajaxGridOptions: { contentType: "application/json" } and to use correct jsonReader. I can guess that it could be something like

jsonReader: {
    root: "responseJSON.rows",
    page: "responseJSON.page",
    total: "responseJSON.total",
    records: "responseJSON.records",
    repeatitems: false,
    id: "JobID"
}

这篇关于当我已经使用idPrefix时,行会重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆