jqGrid的始终突出第一行使用addRowData时, [英] jqGrid always highlighting first row when using addRowData

查看:2122
本文介绍了jqGrid的始终突出第一行使用addRowData时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直试图让ROWID无果而我刚刚突然意识到有一个在我的code故障的真实影响如何jqGrid的运行。

我的code是如下:

 函数showSearchResults(K1,K2,K3,K4,K5){

jQuery的(#list2中)jqGrid的({数据类型:功能(PDATA){
        的getData(PDATA,K1,K2,K3,K4,K5);
    },
                            colNames:['标题','部分',年份,月,网页,等级,K1,K2,K3,K4,K5]
                            colModel:
                                        {名字:标题,索引:标题,宽度:300},
                                        {名字:部分,索引:部分,宽度:100},
                                        {名字:yearEdition,索引:yearEdition',宽度:60,对齐:中心},
                                        {名字:monthEdition,索引:monthEdition',宽度:60,对齐:中心},
                                        {名字:页面编号,索引:页面编号,宽度:60,对齐:中心},
                                        {名字:军衔,索引:'排名',宽度:100,对齐:中心},
                                        {名字:关键字1,索引:关键字1,宽度:100,对齐:中心},
                                        {名字:关键字2,索引:关键字2,宽度:100,对齐:中心},
                                        {名字:keyword3,索引:keyword3,宽度:100,对齐:中心},
                                        {名字:keyword4,索引:keyword4',宽度:100,对齐:中心},
                                        {名字:keyword5,索引:keyword5',宽度:100,对齐:中心}
                                    ]
                            的rowNum:10,
                            rowList:[10,20,30]
                            寻呼机:#pager2,
                            sortname:'身份证',
                            viewrecords:真正的,
                            排序顺序:递减,
                            loadonce:真正的,
                            标题:结果});

。jQuery的(#list2中)jqGrid的('navGrid','#pager2',{编辑:假的,加:假的,德尔:假});
 

该GetData函数仅仅是这样的:

 函数的getData(PDATA,关键词1,关键词,keyword3,keyword4,keyword5){

$ .getJSON('addresshere?回调=?,
    {
        K1:关键字1,
        K2:1关键字,
        K3:keyword3,
        K4:keyword4,
        K5:keyword5,
        异步:假的
    },

    功能(数据){
    变种一个= JSON.parse(数据);

    如果(则为a.length!= 0){
        $每个(A,函数(指数,项)
                        {
                        变种thegrid = jQuery的(#list2中);
                        thegrid.addRowData(0项);
                        });
                        }
                    });
    }
 

返回的行从WebService,填充网格中的所有做工精细,但无论我在哪里点击了网格,第一行总是被高亮显示。

这有什么明显的或已知的问题(如字段名称与jsGrid code相冲突,等等)。

解决方案

是的,当你说:

  thegrid.addRowData(0项);
 

正在分配每个行中的栅极0.相同ID

这是为 addRowData

  

参数

     
      
  • ROWID
  •   
  • 数据
  •   
  • 位置
  •   
  • srcrowid
  •   
     

说明

     

插入一个新行id为rowid的含有指定的位置中的数据的数据(对象)(表中第一个,最后在表或之前或在srcrowid指定的行之后)...      

此方法可以同时插入多行。在这种情况下,数据参数应阵列定义为   [{名1:值1,名称2:值2 ...},{名1:值1,名称2:值2 ...}]和第一个选项ROWID应包含哪些应作为该行的ID数据对象的名称。这是没有必要,在这种情况下,其rowid的名称应该是一部分从colModel

既然你是在一次插入一行(而不是如上所述多行),在调用的时候 addRowData 需要分配每行一个唯一的ID。我建议你​​获取一个唯一的ID作为您的网络请求的一部分,并重构code的需要分配ID:

  thegrid.addRowData(item.id,项目);
 

显然,这是一个问题,如果你没有在后台一个唯一的ID - 尽管这可能表明一个更大的设计问题。在任何情况下,如果你无法检索一个ID变量,我建议你使用一个临时的JavaScript变量分配各行不断增加ID:

  VAR计数器= 0;
...
    thegrid.addRowData(计数器,项目);
    反++;
...
 


更新

由于奥列格指出的那样,你也可以通过未定义作为ID,让jqGrid的自动生成一个唯一的ID。你可以看到这个从源头code在grid.base.js相关部分:

 如果(typeof运算(ROWID)=未定义!){ROWID = ROWID +;}
其他 {
    ROWID = $ .jgrid.randId();
    ...
 

我看不到文档中提到的让您自担风险使用此功能。话虽如此,这似乎是一个不错的功能,presumably的jqGrid的球队没有理由删除。

Been trying to get the rowID to no avail and I've just suddenly realised there's a fault in my code that's affecting how jqGrid is operating.

My code is as follows:

function showSearchResults(k1,k2,k3,k4,k5){

jQuery("#list2").jqGrid(    {   datatype: function(pdata) {
        getData(pdata,k1,k2,k3,k4,k5);
    },
                            colNames:['title','section','Year','Month', 'Page', 'rank', k1,k2,k3,k4,k5],
                            colModel:[
                                        {name:'title',index:'title', width:300},
                                        {name:'section',index:'section', width:100},                                    
                                        {name:'yearEdition',index:'yearEdition', width:60, align:"center"},
                                        {name:'monthEdition',index:'monthEdition', width:60, align:"center"},
                                        {name:'pageNumber',index:'pageNumber', width:60, align:"center"},
                                        {name:'rank',index:'rank', width:100, align:"center"},
                                        {name:'keyword1',index:'keyword1', width:100, align:"center"},
                                        {name:'keyword2',index:'keyword2', width:100, align:"center"},
                                        {name:'keyword3',index:'keyword3', width:100,align:"center"},
                                        {name:'keyword4',index:'keyword4', width:100,align:"center"},
                                        {name:'keyword5',index:'keyword5', width:100,align:"center"}
                                    ],
                            rowNum:10,
                            rowList:[10,20,30],
                            pager: '#pager2',
                            sortname: 'id',
                            viewrecords: true,
                            sortorder: "desc",
                            loadonce:true,
                            caption:"Results" });

jQuery("#list2").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false});

The GetData function is merely this:

function getData(pdata,keyword1,keyword2,keyword3,keyword4,keyword5) {

$.getJSON('addresshere?callback=?',
    {
        k1:keyword1,
        k2:keyword2,
        k3:keyword3,
        k4:keyword4,
        k5:keyword5,
        async:false
    },

    function (data) {
    var a = JSON.parse(data);

    if (a.length != 0)  {
        $.each(a, function (index, item)
                        {
                        var thegrid = jQuery("#list2"); 
                        thegrid.addRowData(0,item);
                        });
                        }
                    });
    }

Returning the rows from the WebService, populating the grid all work fine, but no matter where I click on the grid, the first row is always highlighted.

Is there anything obvious or any known issues (like fields names conflicting with the jsGrid code, etc).

解决方案

Yes, when you say:

thegrid.addRowData(0,item);

You are assigning each row in the grid the same ID of 0.

From the documentation for addRowData:

Parameters

  • rowid
  • data
  • position
  • srcrowid

Description

Inserts a new row with id = rowid containing the data in data (an object) at the position specified (first in the table, last in the table or before or after the row specified in srcrowid) ...

This method can insert multiple rows at once. In this case the data parameter should be array defined as [{name1:value1,name2: value2…}, {name1:value1,name2: value2…} ] and the first option rowid should contain the name from data object which should act as id of the row. It is not necessary that the name of the rowid in this case should be a part from colModel.

Since you are inserting one row at a time (instead of multiple rows as noted above), you need to assign each row a unique ID when calling addRowData. I recommend you retrieve a unique ID as part of your web request, and refactor your code as necessary to assign the ID:

 thegrid.addRowData(item.id, item);

Obviously that is a problem if you do not have a unique ID on the back-end - although it may indicate a larger design problem. In any case if you cannot retrieve an ID variable, I suggest you use a temporary JavaScript variable to assign each row an ever increasing ID:

var counter = 0;
...
    thegrid.addRowData(counter, item);
    counter++;
...


Update

As Oleg points out, you can also pass undefined as the ID to allow jqGrid to automatically generate a unique ID. You can see this from the relevant portion of the source code in grid.base.js:

if(typeof(rowid) != 'undefined') { rowid = rowid+"";}
else {
    rowid = $.jgrid.randId();
    ...

I do not see this feature mentioned in the documentation so use it at your own risk. But that said, this seems like a nice feature that presumably the jqGrid team would have no reason to remove.

这篇关于jqGrid的始终突出第一行使用addRowData时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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