【问题标题】:How to initialize a jqGrid with the proper events for row re-ordering (Sortable)如何使用适当的事件初始化 jqGrid 以进行行重新排序(可排序)
【发布时间】:2012-01-27 20:41:15
【问题描述】:

我希望能够订阅在可排序拖放操作期间引发的事件(3.6 可排序行中的新功能),因为我需要将此信息保存回存储。我已经从http://www.trirand.com/jqgridwiki/doku.php?id=wiki:jquery_ui_methods#drag_and_drop_rows_between_grids 尝试过 onstop 和 onstart,但它似乎只适用于作为另一个表的放置目标。

谢谢你, 斯蒂芬

列:

var col_names = ['Qty', 'SFC', 'Item Nbr', 'Brand', 'Product', 'Catalog', 'Price', 'UOM', 'Case', 'Remarks', 'Wt.', 'Par', 'Purchased', 'ProductId', 'SortPriority'];
var col_model = [
{ name: 'Quantity', index: 'Quantity', width: 22, sorttype: "number", editable: true, edittype: 'text', editoptions: { size: 10, maxlength: 15} },
{ name: 'ProductAttributes', index: 'ProductAttributes', width: 50 },
{ name: 'ItemNum', index: 'ItemNum', width: 50, align: "right" },
{ name: 'BrandName', index: 'BrandName', width: 100 },
{ name: 'ProducName', index: 'ProducName', width: 150 },
{ name: 'Catalog', index: 'Catalog', width: 100 },
{ name: 'Price', index: 'Price', width: 40, sorttype: "number", align: "right" },
{ name: 'UOM', index: 'UOM', width: 30 },
{ name: 'CasePack', index: 'CasePack', width: 30 },
{ name: 'PackageRemarks', index: 'PackageRemarks', width: 80 },
{ name: 'AveWeight', index: 'AveWeight', width: 30, align: "right" },
{ name: 'Par', index: 'Par', width: 25, align: "right", editable: true, edittype: 'text', editoptions: { size: 15, maxlength: 15} },
{ name: 'LastPurchaseDate', index: 'LastPurchaseDate', width: 40, align: "right" },
{ name: 'ProductId', index: 'ProductId', hidden: true, key: true },
{ name: 'SortPriority', index: 'SortPriority', hidden: true }
 ];

网格:

favoriteGrid = $('#favoriteGrid');

favoriteGrid.jqGrid({
    url: '/xxx/yyy/',
    datatype: 'json',
    ajaxGridOptions: { contentType: "application/json" },
    jsonReader: {
        id: "ProductId",
        cell: "",
        root: function (obj) { return obj.rows; },
        page: function () { return 1; },
        total: function () { return 1; },
        records: function (obj) { return obj.rows.length; },
        repeatitems: true
    },
    colNames: col_names,
    colModel: col_model,
    pager: $('#favoritePager'),
    pginput: false,
    rownumbers: true,
    rownumWidth: 25,
    rowNum: 1000,
    autowidth: true,
    height: '500px',
    sortable: true, // enable column sorting
    multiselect: true, // enable multiselct
    gridview: true,
    ignoreCase: true,
    loadonce: true, // one ajax call per 
    loadui: 'block',
    loadComplete: function () {
        var gr = $('#favoriteGrid');
        fixGridSize(gr);
    },
    onSelectRow: function (id) {
        if (id && id !== lastSel) {
            favoriteGrid.restoreRow(lastSel);
            lastSel = id;
        }
        favoriteGrid.editRow(id, true);
    },
    onstop: function (event, ui) {
        alert("onstop");
    }
}).jqGrid('navGrid', '#favoritePager',
    { add: false, edit: false, del: false, search: true, refresh: false },
    {},
    {},
    {},
    { multipleSearch: true, showQuery: false },
    {}).jqGrid('sortableRows').jqGrid('gridDnD');

编辑1:

从 jqGrid 生成的表是可排序的,所以我认为这是在初始化之后使用这些 jquery 函数的问题。

$('#favoriteGrid').bind("sortstart", function (event, ui) {
    alert("start");
});

$('#favoriteGrid').bind("sortstop", function (event, ui) {
    alert("stop");
});

【问题讨论】:

    标签: jquery jqgrid


    【解决方案1】:

    这是个好问题!

    要捕获列的结果,您应该使用sortable 作为函数 而不是布尔值true

    sortable: function (permutation) {
        alert ('permutation=' + permutation.join(','));
    }
    

    the demo。如果您重新排序“客户”和“日期”列,您将收到警报消息

    内部用于行号和多选复选框的列 'rn' 和 'cb' 排在第一位,并具有索引 0 和 1。列 'Client' 具有索引 2,而 'Date' 具有索引 3。到 @ 'Client' 和 'Date' 列重新排序后的 987654332@ 数组将是 [0, 1, 3, 2, 4, 5, 6, 7, 8, 9]

    需要说明的是,如果你需要设置jQuery UI Sortable的一些选项,你应该使用jqGrid的sortable参数的另一种格式:

    sortable: {
        update: function (permutation) {
            alert ('permutation=' + permutation.join(','));
        },
        options: {
            opacity: 0.8
        }
    }
    

    the next demo:

    更新:要监控行的重新排序,您可以执行以下操作:

    favoriteGrid.jqGrid('sortableRows', {
        update: function (ev, ui) {
            alert ("The row with the id=" + ui.item[0].id +
                " is moved. New row index is " + ui.item[0].rowIndex);
        }});
    

    the demo。您可以通过以下方式获取有关移动行的新位置之前和之后的行的更多详细信息

    favoriteGrid.jqGrid('sortableRows', {
        update: function (ev, ui) {
            var item = ui.item[0], ri = item.rowIndex, itemId = item.id,
                message = "The row with the id=" + itemId +
                    " is moved. The new row index is " + ri;
            if (ri > 1 && ri < this.rows.length - 1) {
                alert(message + '\nThe row is inserted between item with rowid=' +
                    this.rows[ri-1].id + ' and the item with the rowid=' +
                    this.rows[ri+1].id);
            } else if (ri > 1) {
                alert(message +
                    '\nThe row is inserted as the last item after the item with rowid=' +
                    this.rows[ri-1].id);
            } else if (ri < this.rows.length - 1) {
                alert(message +
                    '\nThe row is inserted as the first item before the item with rowid=' +
                    this.rows[ri+1].id);
            } else {
                alert(message);
            }
        }});
    

    the next demo

    【讨论】:

    • 我认为我没有正确描述我正在寻找的内容......抱歉。 trirand.com/blog/jqgrid/jqgrid.html > 3.6 版中的新功能 > 可排序的行。谢谢你,斯蒂芬
    • @SPATEN:对不起,这是我的错误。我更新了sortableRows 的答案。
    【解决方案2】:

    当前可行的实现是

    $('#favoriteGrid').bind("sortstart", function (event, ui) {
        // I had no need for this action
    });
    
    $('#favoriteGrid').bind("sortstop", function (event, ui) {
    
        var lista = jQuery("#favoriteGrid").getDataIDs();
        var items = [];
    
        for (i = 0; i < lista.length; i++) {
            var rowData = jQuery("#favoriteGrid").getRowData(lista[i]);
            items.push({ productId: rowData.ProductId, rowNum: i });
        }
    
        var fromFolder = $("#favorite-products > a.selected").attr("data-folderId");
        var payload = { FolderId: fromFolder, Items: items };
    
        $.blockUI({ message: '<h3><img src="../../../../Content/images/busy.gif" /> Saving your sort order...</h3>' });
    
        $.ajax({
            type: "POST",
            url: "/xxx/yyy/zzz/",
            data: JSON.stringify(payload),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $("#favoriteStatusMessages").html(data)
                $.unblockUI();
            },
            failure: function (errMsg) {
                $("#favoriteStatusMessages").html(errMsg).addClass("ui-state-error");
                $.unblockUI();
            }
        });
    
    
    });
    

    【讨论】:

      猜你喜欢
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多