简体   繁体   English

确定值是否与jqGrid数据中的任何键匹配的正确方法是什么?

[英]What is the proper way to identify if a value matches any key in the data of a jqGrid?

I have a jqGrid that uses an order number as the key field for the grid. 我有一个jqGrid使用订单号作为网格的关键字段。 Users can enter an order number into a textbox on the page and I need to "search" through the grid and see if the the value entered matches any of the rows of the grid. 用户可以在页面上的文本框中输入订单号,我需要在网格中“搜索”并查看输入的值是否与网格的任何行匹配。 Then if it does I want to select the grid-row, and if it doesn't I want to dump that key off into a hidden field elsewhere on the page. 然后,如果它,我想选择网格行,如果不是,我想将该密钥转储到页面上其他地方的隐藏字段。

From what I can tell, there only seems to be one way to do this by searching through the table generated by the grid. 据我所知,通过搜索网格生成的表格似乎只有一种方法可以做到这一点。

$("#list > tbody > tr:has(td:contains('" + str + "'))");

But, doing it this way only searches through the current page of the grid, not the entirety of the grid data (thanks pagination). 但是,这样做只搜索网格的当前页面,而不是整个网格数据(感谢分页)。 So then I expected to be able to call the setSelection method and catch if the key passed was not found in the jqGrid. 所以我希望能够调用setSelection方法并捕获是否在jqGrid中找不到传递的密钥。

$(this).jqGrid('setSelection', idOfSelectedRow);

but calling the setSelection method returns the entire jquery object, even if the id passed is not found (as described in the documentation, that was my bad). 但是调用setSelection方法会返回整个jquery对象,即使找不到传递的id(如文档中所述,这是我的坏事)。

So, this is where I am kind of stuck. 所以,这就是我陷入困境的地方。 I need to be able to search through the entire grids' data looking for a key. 我需要能够搜索整个网格的数据,寻找密钥。 I can expound on my implementation more if needed. 如果需要,我可以更多地阐述我的实施。

So, after digging around in the docs I believe I've found a better way. 因此,在深入研究文档后,我相信我找到了更好的方法。

var orderNum = $('#tbUserInput').val();
var gridData = $('#GridData').jqGrid('getGridParam', 'data');
var gridDataRow = $.grep(gridData, function (e) { return e._id_ == orderNum; });

    if (gridDataRow.length > 0) {
        var selRowIds = $('#GridData').jqGrid("getGridParam", "selarrrow");
        if ($.inArray(gridDataRow[0]._id_, selRowIds) < 0) {
            $('#GridData').jqGrid('setSelection', gridDataRow[0]._id_, true);
        }
    }
    else {
        AddSelectedOrder(orderNum, $('[id*=hfSelectedOrders]'));
    }

First, I'm grabbing the user's input, then getting the entire jqGrid data object from the grid parameters (never knew about this before today). 首先,我抓住用户的输入,然后从网格参数中获取整个jqGrid数据对象(在今天之前从未了解过)。 After that I decided to use .grep to look for an object inside of the grid data array that has an id == the user input. 之后,我决定使用.grep在网格数据数组中查找id ==用户输入的对象。 Assuming that the grep is successful gridDataRow will have a non-zero length (and since the order number is a key field in the jqgrid, I was OK assuming the length will always be 1, never more). 假设grep成功,gridDataRow将具有非零长度(并且由于订单号是jqgrid中的关键字段,我可以假设长度始终为1,从不更多)。

Next, I am getting the array of selectedRowIds directly from the grid (this grid has multiselect on). 接下来,我直接从网格获取selectedRowIds数组(此网格具有多选)。 Then if the id from the grid data is not found in the selectedRows array, I'm triggering the jqGrids' setSelection to select the row. 然后,如果在selectedRows数组中找不到网格数据的id,我将触发jqGrids的setSelection来选择行。

the last else block simply adds the order number to a hidden field. 最后一个else块只是将订单号添加到隐藏字段。

Seems to work. 似乎工作。 If anyone else runs into this problem I hope this helps! 如果有其他人遇到这个问题,我希望这有帮助!

edit just a note, I have the if ($.inArray(gridDataRow[0]. id , selRowIds) < 0) check to see if the row is NOT already selected. 编辑只是一个说明,我有,如果($ .inArray(gridDataRow [0],ID,selRowIds)<0)检查,看看是否尚未被选中的行。 If it is I do NOT want to unselect it (customer asked for it to behave this way). 如果是,我不想取消选择(客户要求它以这种方式行事)。 If you end up using this code you may want to remove that IF check and simply call the setSelection so it will toggle the selection of that row. 如果您最终使用此代码,您可能希望删除该IF检查并简单地调用setSelection,以便切换该行的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM