You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取jqxGrid中与目标数组匹配的行索引并选中对应行

问题描述

我已经修改了包含使用代码的示例,需求是获取与const intersection值匹配的行索引,进而选中这些匹配的行,但不知道如何获取并传递这些索引(可查看代码末尾注释部分)。

可参考jqxGrid API文档。


HTML代码

<div id='jqxWidget'>
    <div id="jqxgrid"></div>
</div>
<input type="button" style="margin: 50px;" id="jqxbutton" value="Get displayed rows" />

JavaScript代码

var data = new Array();
var firstNames = [34286, 34288, 34293];
var allObjectIDS = [];
var allRowIndexes = [];
var roomsAssignedData = [];
roomsAssignedData = [34288, 34293];
allObjectIDS = []
for (var i = 0; i < 3; i++) {
    var row = {};
    row["firstname"] = firstNames[Math.floor(Math.random() * firstNames.length)];
    data[i] = row;
}
var source = {
    localdata: data,
    datatype: "array"
};
var dataAdapter = new $.jqx.dataAdapter(source, {
    loadComplete: function (data) { },
    loadError: function (xhr, status, error) { }
});
$("#jqxgrid").jqxGrid({
    theme: 'energyblue',
    altrows: true,
    altstep: 2,
    width: 200,
    height: 200,
    source: dataAdapter,
    columns: [{
        text: 'First Name',
        datafield: 'firstname',
        width: 100
    }]
});

$('#jqxbutton').click(function () {
    var rows = $('#jqxgrid').jqxGrid('getrows');
    var rowsB = $('#jqxgrid').jqxGrid('getdisplayrows');
    var rowsCount = rows.length;
    console.log(rowsCount)


    for (var i = 0; i < rowsCount; i++) {
        var valueObjectID = $('#jqxgrid').jqxGrid('getcellvalue', i, "firstname");
        var row = rowsB[i];
        var boundIndex = row.boundindex
        var rowID = row.uid
        allObjectIDS.push(
            row.firstname

        );
        allRowIndexes.push(
            boundIndex,
        );
    }

    console.log("allObjectIDS array")
    console.log(allObjectIDS)
    console.log("allRowIndexes array")
    console.log(allRowIndexes)
    console.log("roomsAssignedData")
    console.log(roomsAssignedData)
    const intersection = allObjectIDS.filter(element => roomsAssignedData.includes(element));
    console.log("intersection")
    console.log(intersection)
    var sortA = roomsAssignedData.sort();
    var sortB = intersection.sort();

    console.log("sortA")
    console.log(sortA)
    console.log("sortB")
    console.log(sortB)
//$('#jqxgrid').jqxGrid('selectrow', value);
//this is where I need to select the rows that match the intersected values but it do not know how to grab them and then pass them
});

解决方案

要实现匹配值对应的行选中,有两种简洁的方式:

方式一:直接遍历行数据选中

不需要维护额外数组,直接遍历网格的显示行,判断匹配后选中:

$('#jqxbutton').click(function () {
    var rows = $('#jqxgrid').jqxGrid('getdisplayrows');
    // 清空之前的选中状态
    $('#jqxgrid').jqxGrid('clearselection');
    // 遍历每行,匹配则选中
    rows.forEach(row => {
        if (roomsAssignedData.includes(row.firstname)) {
            $('#jqxgrid').jqxGrid('selectrow', row.boundindex);
        }
    });
});

逻辑说明:

  • getdisplayrows获取所有显示的行对象,每个对象包含boundindex(用于选中行的索引)和firstname字段
  • 先调用clearselection避免残留选中状态
  • 遍历判断firstname是否在目标数组中,匹配则调用selectrow传入boundindex选中该行

方式二:保留交集逻辑,通过映射找索引

如果需要保留原有的交集计算逻辑,可以先建立firstname到行索引的映射,再通过交集值找到对应索引选中:

$('#jqxbutton').click(function () {
    var rowsB = $('#jqxgrid').jqxGrid('getdisplayrows');
    var rowsCount = rowsB.length;
    const nameToIndexMap = {};
    var allObjectIDS = [];

    for (var i = 0; i < rowsCount; i++) {
        var row = rowsB[i];
        nameToIndexMap[row.firstname] = row.boundindex;
        allObjectIDS.push(row.firstname);
    }

    console.log("allObjectIDS array")
    console.log(allObjectIDS)
    console.log("roomsAssignedData")
    console.log(roomsAssignedData)
    const intersection = allObjectIDS.filter(element => roomsAssignedData.includes(element));
    console.log("intersection")
    console.log(intersection)

    // 清空选中状态,再批量选中匹配行
    $('#jqxgrid').jqxGrid('clearselection');
    intersection.forEach(name => {
        const index = nameToIndexMap[name];
        if (index !== undefined) {
            $('#jqxgrid').jqxGrid('selectrow', index);
        }
    });
});

逻辑说明:

  • 遍历行数据时,把每个firstname对应的boundindex存入对象nameToIndexMap,建立映射关系
  • 计算出交集后,遍历交集值,通过映射找到对应的行索引,调用selectrow选中

内容的提问来源于stack exchange,提问作者MrKirkwood

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 08:04:59