jqGrid行选择的奇怪行为

时间:2011-09-09 06:39:57

标签: jquery jqgrid

我有这种非常不寻常的行为,让我来描述一下这个问题。

jqgrid共有14条记录,首先它设置为rowNum:10,,从下拉列表中我选择显示所有记录容纳的50行,但在Firefox 5中我选择 11行(名称为 asd )它也会选择第10行。它在IE8中表现不同。

在IE8中,它给出了以下错误

Webpage error details

Message: 'className' is null or not an object
Line: 2496
Char: 4
Code: 0
URI: http://au1642163:10070/csm/view/include/js/jquery.jqGrid.min.js

这是IE8的图像

enter image description here

我的jqgrid代码

var xml=client.responseText;
         var xmlDoc = $.parseXML(xml); 
         var $xml = $(xml);

         xml=xml.replace(/<productId>1/g, "<productId>"+productMap['1']);
         xml=xml.replace(/<productId>2/g, "<productId>"+productMap['2']);
$('#configDiv').empty();
            $('#configDiv').html(
                    '<div id="configDetailsGrid" width="100%">' +
                    '<table id="list1" width="100%"></table><div id="gridpager"></div></div>');

            var grid = jQuery("#list1");
            var iconAlert;

            getColumnIndexByName = function (grid, columnName) {
                var cm = grid.jqGrid('getGridParam', 'colModel'), i = 0, l = cm.length;
                for (; i < l; i += 1) {
                    if (cm[i].name === columnName) {
                        return i; // return the index
                    }
                }
                return -1;
            },
            grid = jQuery("#list1"),
            iconAlert = '<span class="ui-state-error" style="border:0"><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span></span>';



            grid.jqGrid({

              datastr : xml,
              datatype: 'xmlstring',
              colNames:['cfgId','Name', 'Host','Operating System', 'Description','Product', 'Type', 'Last Updated Time','Last Updated By','',''],
              colModel:[
                  {name:'cfgId',index:'cfgId', width:90, align:"left", hidden:true},
                  //{name:'updateDate',index:'updateDate', width:12, align:'center', formatter: oldConfigurationWarning },
                  {name:'cfgName',index:'cfgName', width:70, align:"left", formatter: 'showlink', formatoptions: {baseLinkUrl: '#'} },
                  {name:'hostname',index:'hostname', width:70, align:"left"},
                  {name:'osname',index:'osname', width:90, align:"left"},
                  {name:'cfgDesc',index:'cfgDesc', width:90, align:"left"},
                  {name:'productId',index:'productId', width:40, align:"left"},
                  {name:'cfgType',index:'cfgType', width:50, align:"left"},
                  {name:'updateDate',index:'updateDate',sorttype:'Date', width:120, align:"left"},
                  {name:'emailAddress',index:'emailAddress', width:120, align:"left"},
                  {name:'absolutePath',index:'absolutePath', width:90, align:"left", hidden:true},
                  {name:'fileName',index:'fileName', width:90, align:"left", hidden:true}

              ],
              pager : '#gridpager',
              rowNum:10,
              rowList:[10,50,100],
              scrollOffset:0,
              height: 'auto',
              emptyrecords: 'No configurations loaded',
              autowidth:true,
              viewrecords: true,
              gridview: true,
              multiselect: true,
              xmlReader: {
                  root : "list",
                  row: "Response",
                  userdata: "userdata",
                  repeatitems: false
              },
              loadComplete: function () {
                    var count = grid.getGridParam();
                    var ts = grid[0];
                    if (ts.p.reccount === 0) {
                        grid.hide();
                        emptyMsgDiv.show();
                    } else {
                        grid.show();
                        emptyMsgDiv.hide();
                    }

                    //for showlink and icon alert having date difference more than 90 days
                    var iRow, row, trClasses, $cell,
                    icfgName = getColumnIndexByName(grid, 'cfgName'),
                    iupdateDate = getColumnIndexByName(grid, 'updateDate'),

                    mygrid = grid[0],
                    rows = mygrid.rows,
                    cRows = rows.length,
                    myLink = function (e) {
                        var $td = $(e.target).closest('td'),
                            text = $td.text(),
                            $tr = $td.closest('tr'),
                            rowid = $tr[0].id;
                            goToViewAllPage(rowid);
                    };

                for (iRow = 0; iRow < cRows; iRow += 1) {
                    row = rows[iRow]; // row.id is the rowid
                    trClasses = row.className.split(' ');
                    if ($.inArray('jqgrow', trClasses) > 0) {
                        // the row is a standard row (only if subGrid:true are used)

                        /*if ($(row.cells[iupdateDate]).text() === 'Science') {
                           $cell.prepend(iconAlert);
                        }*/
                        var cellvalue1,firstDate,secondDate;
                        $cell = $(row.cells[icfgName]);
                        cellvalue1=$(row.cells[iupdateDate]).text();
                        firstDate = new Date();
                        //console.info(cellvalue1+", "+cellvalue1.length);
                        //var cellvalue1="08-18-2011 11:49:01";
                        if(cellvalue1.length>25)
                        {
                            secondDate=new Date();
                            //secondDate = secondDate.substring(0, secondDate.length-3);

                            if(diffOf2Dates(firstDate,secondDate,true)>=expireCondition)
                            {
                                $cell.prepend(iconAlert);
                            }
                            $cell.click(myLink);
                        }
                        else
                        {

                            secondDate = cellvalue1.substring(0, cellvalue1.length-6);
                            if(diffOf2Dates(firstDate,secondDate,false)>=expireCondition)
                            {
                                $cell.prepend(iconAlert);
                            }
                            $cell.click(myLink);
                        }



                    }
                }
                },
              onSelectRow: function(id,status){
                  //var rowData = jQuery(this).getRowData(id); 
                  }


            });
            grid.jqGrid('navGrid','#gridpager',{edit:false,add:false,del:false});
            jQuery("#m1").click( function() {
                var s;
                s = grid.jqGrid('getGridParam','selarrrow');
                alert(s);
            });
            var myGrid = $("#list1");
            $("#cb_"+myGrid[0].id).hide();
            // place div with empty message insde of bdiv
            emptyMsgDiv.insertAfter(grid.parent());
     }

};

我的XML回复

<list>
<Response>
<cfgId>704</cfgId>
<cfgName>IIR Windows1</cfgName>
<cfgDesc>asda</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientIIR.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientIIR.xml</absolutePath>
<emailAddress>asimon@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>IIR</hostname>
<createDate>2011-09-07 10:55:31.0 IST</createDate>
<updateDate>2011-09-07 10:55:31.0 IST</updateDate>
<state>1</state>
<productId>1</productId>
<osname>Windows_NT</osname>
</Response>
<Response>
<cfgId>717</cfgId>
<cfgName>ilmwin</cfgName>
<cfgDesc>asd</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientin164302.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientin164302.xml</absolutePath>
<emailAddress>asimon@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>IN164302</hostname>
<createDate>2011-09-07 16:45:03.0 IST</createDate>
<updateDate>2011-09-07 16:45:03.0 IST</updateDate>
<state>1</state>
<productId>2</productId>
<osname>Windows_NT</osname>
</Response>
<Response>
<cfgId>718</cfgId>
<cfgName>ilmwin1</cfgName>
<cfgDesc>asdasd</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientin164302.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientin164302.xml</absolutePath>
<emailAddress>asimon@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>IN164302</hostname>
<createDate>2011-09-07 16:46:35.0 IST</createDate>
<updateDate>2011-09-08 10:19:26.0 IST</updateDate>
<state>2</state>
<productId>2</productId>
<osname>Windows_NT</osname>
</Response>
<Response>
<cfgId>723</cfgId>
<cfgName>ilm linux</cfgName>
<cfgDesc>asd</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientestilo.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientestilo.xml</absolutePath>
<emailAddress>asimon@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>estilo</hostname>
<createDate>2011-09-08 14:20:09.0 IST</createDate>
<updateDate>2011-09-08 14:20:09.0 IST</updateDate>
<state>1</state>
<productId>2</productId>
<osname>Linux</osname>
</Response>
<Response>
<cfgId>696</cfgId>
<cfgName>ILMwin1</cfgName>
<cfgDesc>asfas</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientin164302.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientin164302.xml</absolutePath>
<emailAddress>mkuamrln@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>IN164302</hostname>
<createDate>2011-09-06 16:15:06.0 IST</createDate>
<updateDate>2011-09-06 16:15:06.0 IST</updateDate>
<state>1</state>
<productId>2</productId>
<osname>Windows_NT</osname>
</Response>
<Response>
<cfgId>697</cfgId>
<cfgName>ILMlin</cfgName>
<cfgDesc>dgds</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientestilo.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientestilo.xml</absolutePath>
<emailAddress>mkuamrln@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>estilo</hostname>
<createDate>2011-09-06 16:20:58.0 IST</createDate>
<updateDate>2011-09-06 16:20:58.0 IST</updateDate>
<state>1</state>
<productId>2</productId>
<osname>Linux</osname>
</Response>
<Response>
<cfgId>706</cfgId>
<cfgName>ILM linux</cfgName>
<cfgDesc>wwrwrwr</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientestilo.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientestilo.xml</absolutePath>
<emailAddress>mkuamrln@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>estilo</hostname>
<createDate>2011-09-07 14:22:03.0 IST</createDate>
<updateDate>2011-09-07 14:22:03.0 IST</updateDate>
<state>1</state>
<productId>2</productId>
<osname>Linux</osname>
</Response>
<Response>
<cfgId>698</cfgId>
<cfgName>test1</cfgName>
<cfgDesc>asfta</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientgcslpar1.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientgcslpar1.xml</absolutePath>
<emailAddress>asimon@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>gcslpar1</hostname>
<createDate>2011-09-06 17:14:31.0 IST</createDate>
<updateDate>2011-09-08 10:20:15.0 IST</updateDate>
<state>2</state>
<productId>1</productId>
<osname>AIX</osname>
</Response>
<Response>
<cfgId>699</cfgId>
<cfgName>test2</cfgName>
<cfgDesc>adAD</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientgcslpar2.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientgcslpar2.xml</absolutePath>
<emailAddress>mkuamrln@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>gcslpar2</hostname>
<createDate>2011-09-06 17:14:46.0 IST</createDate>
<updateDate>2011-09-06 17:14:46.0 IST</updateDate>
<state>1</state>
<productId>1</productId>
<osname>AIX</osname>
</Response>
<Response>
<cfgId>702</cfgId>
<cfgName>IIR Windows</cfgName>
<cfgDesc>asdad</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientIIR.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientIIR.xml</absolutePath>
<emailAddress>asimon@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>IIR</hostname>
<createDate>2011-09-07 10:47:10.0 IST</createDate>
<updateDate>2011-09-07 10:47:10.0 IST</updateDate>
<state>1</state>
<productId>1</productId>
<osname>Windows_NT</osname>
</Response>
<Response>
<cfgId>687</cfgId>
<cfgName>asd</cfgName>
<cfgDesc>a</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientestilo.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientestilo.xml</absolutePath>
<emailAddress>asimon@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>estilo</hostname>
<createDate>2011-09-06 13:28:28.0 IST</createDate>
<updateDate>2011-09-06 13:28:28.0 IST</updateDate>
<state>1</state>
<productId>1</productId>
<osname>Linux</osname>
</Response>
<Response>
<cfgId>688</cfgId>
<cfgName>asd1</cfgName>
<cfgDesc>asd</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientbuckeye.ABC.com.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientbuckeye.ABC.com.xml</absolutePath>
<emailAddress>asimon@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>buckeye.ABC.com</hostname>
<createDate>2011-09-06 13:30:10.0 IST</createDate>
<updateDate>2011-09-06 13:30:10.0 IST</updateDate>
<state>1</state>
<productId>1</productId>
<osname>Linux</osname>
</Response>
<Response>
<cfgId>701</cfgId>
<cfgName>NULL</cfgName>
<cfgDesc>asda</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientin164302.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientin164302.xml</absolutePath>
<emailAddress>asimon@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>IN164302</hostname>
<createDate>2011-09-07 10:43:19.0 IST</createDate>
<updateDate>2011-09-08 16:44:00.0 IST</updateDate>
<state>2</state>
<productId>2</productId>
<osname>Windows_NT</osname>
</Response>
<Response>
<cfgId>705</cfgId>
<cfgName>Sunos config</cfgName>
<cfgDesc>asdasd</cfgDesc>
<cfgType>Production</cfgType>
<fileName>csmclientzenvo.xml</fileName>
<absolutePath>../webapps/csm/files//12345/csmclientzenvo.xml</absolutePath>
<emailAddress>asimon@ABC.com</emailAddress>
<projectId>12345</projectId>
<hostname>zenvo</hostname>
<createDate>2011-09-07 11:22:56.0 IST</createDate>
<updateDate>2011-09-07 11:22:56.0 IST</updateDate>
<state>1</state>
<productId>1</productId>
<osname>SunOS</osname>
</Response>
</list>

1 个答案:

答案 0 :(得分:1)

您应该测试自己发布的代码。你应该仔细阅读the question with the answer。如果您不想遇到所述问题,则应以其他方式设置问题。不过,我会回答你当前的问题。

当前代码包含许多未定义的函数和变量。此外,它具有小的语法错误。最重要的一个是

var grid = jQuery("#list1");
var iconAlert;

getColumnIndexByName = function (grid, columnName) {
    ...
},
grid = jQuery("#list1"),
iconAlert = '<span class="ui-state-error" ...'

你不应该两次定义相同的变量,并且应该以任何方式替换';'在var iconAlert;逗号以逗号将变量getColumnIndexByName定义为函数。

开头的代码(xml=xml.replace(/<productId>1/g, "<productId>"+productMap['1']);)我根本无法理解。如果您有$xml并且可以使用jQuery选择和修改任何XML元素,为什么需要对XML进行奇怪的修改。

如果您继续使用如此脏的代码,则可能会出现更严重的问题。我建议您始终在JSLint中验证您的代码。您可以包含JSLint选项,例如

/*global $, jQuery */
/*jslint devel: true, browser: true, vars: true, white: true, plusplus: true*/

完成所有准备测试示例的工作后,只有the resulting code没有您所描述的问题。