简体   繁体   中英

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. 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.

$(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).

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). 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. 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).

Next, I am getting the array of selectedRowIds directly from the grid (this grid has multiselect on). 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.

the last else block simply adds the order number to a hidden field.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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