你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / Web开发
extend juqeryUI datagrid.methods
 
少说废话,转入正题 
[javascript] view plaincopy
$.extend($.fn.datagrid.methods,{ 
    columnMoving: function(jq){ 
        return jq.each(function(){ 
            var target = this; 
            var cells = $(this).datagrid('getPanel').find('div.datagrid-header td[field]'); 
            cells.draggable({ 
                revert:true, 
                cursor:'pointer', 
                edge:5, 
                proxy:function(source){ 
                    var p = $('<div class="tree-node-proxy tree-dnd-no" style="position:absolute;border:1px solid #ff0000"/>').appendTo('body'); 
                    p.html($(source).text()); 
                    p.hide(); 
                    return p; 
                }, 
                onBeforeDrag:function(e){ 
                    e.data.startLeft = $(this).offset().left; 
                    e.data.startTop = $(this).offset().top; 
                }, 
                onStartDrag: function(){ 
                    $(this).draggable('proxy').css({ 
                        left:-10000, 
                        top:-10000 
                    }); 
                }, 
                onDrag:function(e){ 
                    $(this).draggable('proxy').show().css({ 
                        left:e.pageX+15, 
                        top:e.pageY+15 
                    }); 
                    return false; 
                } 
            }).droppable({ 
                accept:'td[field]', 
                onDragOver:function(e,source){ 
                    $(source).draggable('proxy').removeClass('tree-dnd-no').addClass('tree-dnd-yes'); 
                    $(this).css('border-left','1px solid #ff0000'); 
                }, 
                onDragLeave:function(e,source){ 
                    $(source).draggable('proxy').removeClass('tree-dnd-yes').addClass('tree-dnd-no'); 
                    $(this).css('border-left',0); 
                }, 
                onDrop:function(e,source){ 
                    $(this).css('border-left',0); 
                    var fromField = $(source).attr('field'); 
                    var toField = $(this).attr('field'); 
                    setTimeout(function(){ 
                        moveField(fromField,toField); 
                        $(target).datagrid(); 
                        $(target).datagrid('columnMoving'); 
                    },0); 
                } 
            }); 
             
            // move field to another location 
            function moveField(from,to){ 
                var columns = $(target).datagrid('options').columns; 
                var cc = columns[0]; 
                var c = _remove(from); 
                if (c){ 
                    _insert(to,c); 
                } 
                 
                function _remove(field){ 
                    for(var i=0; i<cc.length; i++){ 
                        if (cc[i].field == field){ 
                            var c = cc[i]; 
                            cc.splice(i,1); 
                            return c; 
                        } 
                    } 
                    return null; 
                } 
                function _insert(field,c){ 
                    var newcc = []; 
                    for(var i=0; i<cc.length; i++){ 
                        if (cc[i].field == field){ 
                            newcc.push(c); 
                        } 
                        newcc.push(cc[i]); 
                    } 
                    columns[0] = newcc; 
                } 
            } 
        }); 
    } 
}); 

调用:

[javascript] 
$('#tt').datagrid({ 
    url: 'datagrid_data3.json', 
    title: 'DataGrid', 
    width: 700, 
    height: 220, 
    fitColumns: true, 
    nowrap:false, 
    rownumbers:true, 
    showFooter:true, 
    columns:[[ 
        {field:'itemid',title:'Item ID',width:80}, 
        {field:'productid',title:'Product ID',width:120}, 
        {field:'listprice',title:'List Price',width:80,align:'right'}, 
        {field:'unitcost',title:'Unit Cost',width:80,align:'right'}, 
        {field:'attr1',title:'Attribute',width:250}, 
        {field:'status',title:'Status',width:60,align:'center'} 
    ]] 
}).datagrid("columnMoving"); 

  推荐精品文章

·2024年9月目录 
·2024年8月目录 
·2024年7月目录 
·2024年6月目录 
·2024年5月目录 
·2024年4月目录 
·2024年3月目录 
·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录
·2023年10月目录
·2023年9月目录 
·2023年8月目录 

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089