TreeGrid XML shiftKey ctrlKey选择

时间:2013-12-17 00:35:10

标签: jqgrid treegrid

我已经浏览了我能找到的每个例子,似乎没有一个能够适应我需要的特定场景。我找到了适用于ctrl或shift-selection的示例,但仅适用于JQGrid,而不适用于TreeGrid。一旦我采用这些示例并尝试将它们调整为TreeGrid,实现XML数据类型和url,而不是JSON或“local”,它们就会中断。

这次最近的尝试涉及绑定从此网址获取的多选手:http://www.trirand.com/blog/?page_id=393/feature-request/more-control-over-clickselect-behavior/&value=beforeSelectRow&type=1&include=1&search=1

我需要使用实现以下选择方法的XML创建TreeGrid:单击选择,按住Ctrl键单击追加到选择,然后按住Shift键单击选择最后一个选定行和当前行之间的每一行。我从Oleg找到了一个基于JSON的JqGrid场景,但是一旦我将其更改为使用TreeGrid和XML,它就无法工作。请帮忙。

以下是url生成的XML的一部分:

<?xml version="1.0"?>
<rows>
<page>1</page>
<total>1</total>
<records>1</records>
<row><cell>1</cell><cell>Bank Data</cell><cell></cell><cell>1</cell><cell/><cell>false</cell><cell>true</cell><cell>true</cell></row>
<row><cell>3</cell><cell>FINANCIAL PROFILE</cell><cell></cell><cell>2</cell><cell>1</cell><cell>false</cell><cell>false</cell><cell>true</cell></row>
<row><cell>4</cell><cell>Balance Sheet:</cell><cell></cell><cell>3</cell><cell>3</cell><cell>false</cell><cell>false</cell><cell>true</cell></row>
<row><cell>5</cell><cell>Total assets</cell><cell>EXFHBS01</cell><cell>4</cell><cell>4</cell><cell>true</cell><cell>false</cell><cell>true</cell></row>
<row><cell>11</cell><cell>Percent change in total assets</cell><cell>EXFHPC01</cell><cell>4</cell><cell>4</cell><cell>true</cell><cell>false</cell><cell>true</cell></row>
<row><cell>18</cell><cell>Held-to-maturity securities (Amortized cost)</cell><cell>EXFHBS03</cell><cell>4</cell><cell>4</cell><cell>true</cell><cell>false</cell><cell>true</cell></row>
<row><cell>25</cell><cell>Available-for-sale securities (Fair value)</cell><cell>EXFHBS04</cell><cell>4</cell><cell>4</cell><cell>true</cell><cell>false</cell><cell>true</cell></row>
<row><cell>31</cell><cell>Total securities</cell><cell>EXFHBS02</cell><cell>4</cell><cell>4</cell><cell>true</cell><cell>false</cell><cell>true</cell></row>
</rows>

这是我的treegrid代码:

        $(document).ready(function(){
        $("#grid").jqGrid({
            treeGrid: true,
            treeGridModel: 'adjacency',
            ExpandColumn: 'item',
            url: 'parseCreateTreeGridXml.php', 
            datatype: 'xml',
            mtype: 'GET',
            colNames: ["ID", "Item", "Identifier"], 
            colModel: [
                {name: 'id', index: 'id', width: 2, hidden:false, key: true},
                {name: 'item', index: 'item', width: 40, hidden: false, sortable: true},
                {name: 'rcon', index: 'rcon', hidden:false, width: 5}
            ],
            treeIcons: {plus:'ui-icon-circle-plus',minus:'ui-icon-circle-minus',leaf:'ui-icon-stop'},
            rowNum: 10000,
            width: 980,
            height: '100%',
            multiselect: true
            beforeSelectRow: function(){ return false; }
        });
    });
    var grid = $("#grid");
    grid.bind("click", grid, multiSelectHandler);
    //grid.jqGrid('hideCol','cb');

    function multiSelectHandler(e) {
        var mygrid = e.data;
        var ts = mygrid[0], td = e.target;
        var scb = $(td).hasClass("cbox");
        var ptr = $(td).parents("tr.jqgrow");
        if (!ptr.length || (td.tagName == 'INPUT' && !scb) || td.tagName == 'A') {
            return true;
        }
        var sel = mygrid.getGridParam('selarrrow');
        var sid = ptr[0].id;
        var selected = $.inArray(sid, sel) >= 0;
        if (e.ctrlKey || (scb && (selected || !e.shiftKey))) {
            mygrid.setSelection(false,true,ptr);
        } else {
            if (e.shiftKey) {
                var six = mygrid.getInd( sid);
                var min = six, max = six;
                $.each(sel, function() {
                var ix = mygrid.getInd( this);
                if (ix < min) min = ix;
                if (ix > max) max = ix;
                });
                while (min <= max) {
                    var row = ts.rows[min++];
                    var rid = row.id;
                    if (rid != sid && $.inArray(rid, sel)<0) {
                        mygrid.setSelection( false, false, $(row));
                    }
                }
            } else if (!selected) {
                mygrid.resetSelection();
            }
            if (!selected) {
                mygrid.setSelection( false, true, ptr);
            } else {
                var osr = mygrid.getGridParam('onSelectRow');
                if ($.isFunction(osr)) {
                    osr(sid, true);
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我通过将beforeSelectRow函数替换为:

解决了我自己的问题,即试错
        beforeSelectRow: function(rowid, e){
                var grid = $('#grid');
                if(e.ctrlKey){
                    grid.jqGrid('setGridParam', {multikey: 'ctrlKey'});
                }else if(e.shiftKey){
                    grid.jqGrid('setGridParam', {multikey: 'shiftKey'});                    
                    var rowIds = grid.jqGrid('getGridParam','selarrrow');
                    //rowIds.sort(function(a, b) { return a - b }); //this is the magic line which sort the array
                    var iHi = Math.max.apply(Math,rowIds); // 3
                    var iLow = Math.min.apply(Math,rowIds);
                    var iStart = (rowid > iHi ? iHi : (rowid > iLow ? iLow : rowId));
                    var iEnd = (rowid > iHi ? rowid : (rowid > iLow ? rowid : iLow));
                    var allIds = grid.jqGrid('getDataIDs');
                    //var msg = "rowid: "+rowid+"; rowIds: " + rowIds.join(", ")+"; iLow: "+iLow+"; iHi: "+iHi+"; iStart: "+iStart+"; iEnd: "+iEnd+"; allIds["+allIds.length+"]: ";
                    grid.jqGrid('resetSelection');
                    for (var i=0; i < allIds.length; i++){
                        var id = parseInt(allIds[i]); 
                        var bSel = ((id >= iStart && id <= iEnd) && id != rowid);
                        if(bSel){
                            msg += ", ["+i+"]:"+allIds[i]+"("+bSel+")";
                            grid.jqGrid('setSelection',allIds[i], bSel);
                        }
                        if(id > iEnd)
                            break;
                    }
                    //alert(msg);

                }else{
                    grid.jqGrid('setGridParam', {multikey: ''});
                }

                return true; 
            }