锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

easyui之datagrid控件分析

时间:2023-06-02 15:07:00 optex激光位移传感器cd3

/**  * datagrid - jQuery EasyUI  *  * Licensed under the GPL:  * http://www.gnu.org/licenses/gpl.txt  *  * Copyright 2010 stworthy [ stworthy@gmail.com ]  *  * Dependencies:  * resizable  * linkbutton  * pagination  *  */ /**  * 整个表格对象说的是最外层div.datagrid元素  * 整体布局:  *   *   *   *   *   *   *   *   *   *   * * * * * * * */ (function ($) {      function setSize(target) {         var grid = $.data(target, 'datagrid').grid;         var opts = $.data(target, 'datagrid').options;         if (opts.
     
      fit == 
      true) {
            
      var 
      p = 
      grid.
      parent();
            
      opts.
      width = 
      p.
      width();
            
      opts.
      height = 
      p.
      height();
        }

        
      if (
      opts.
      rownumbers || (
      opts.
      frozenColumns && 
      opts.
      frozenColumns.
      length > 
      0)) {
            $(
      '.datagrid-body .datagrid-cell,.datagrid-body .datagrid-cell-rownumber', 
      grid).
      addClass(
      'datagrid-cell-height');
        }

        
      var 
      gridWidth = 
      opts.
      width;
        
      if (
      gridWidth == 
      'auto') {
            
      if ($.boxModel == 
      true) {
                
      gridWidth = 
      grid.
      width();
            } 
      else {
                
      gridWidth = 
      grid.
      outerWidth();
            }
        } 
      else {
            
      if ($.boxModel == 
      true) {
                
      gridWidth -= 
      grid.
      outerWidth() - 
      grid.
      width();
            }
        }
        
      grid.
      width(
      gridWidth);

        
      var 
      innerWidth = 
      gridWidth;
        
      if ($.boxModel == 
      false) {
            
      innerWidth = 
      gridWidth - 
      grid.
      outerWidth() + 
      grid.
      width();
        }

        $(
      '.datagrid-wrap', 
      grid).
      width(
      innerWidth);
        $(
      '.datagrid-view', 
      grid).
      width(
      innerWidth);
        $(
      '.datagrid-view1', 
      grid).
      width($(
      '.datagrid-view1 table', 
      grid).
      width());
        $(
      '.datagrid-view2', 
      grid).
      width(
      innerWidth - $(
      '.datagrid-view1', 
      grid).
      outerWidth());
        $(
      '.datagrid-view1 .datagrid-header', 
      grid).
      width($(
      '.datagrid-view1', 
      grid).
      width());
        $(
      '.datagrid-view1 .datagrid-body', 
      grid).
      width($(
      '.datagrid-view1', 
      grid).
      width());
        $(
      '.datagrid-view2 .datagrid-header', 
      grid).
      width($(
      '.datagrid-view2', 
      grid).
      width());
        $(
      '.datagrid-view2 .datagrid-body', 
      grid).
      width($(
      '.datagrid-view2', 
      grid).
      width());

        
      var 
      hh;
        
      var 
      header1 = $(
      '.datagrid-view1 .datagrid-header', 
      grid);
        
      var 
      header2 = $(
      '.datagrid-view2 .datagrid-header', 
      grid);
        
      header1.
      css(
      'height', 
      null);
        
      header2.
      css(
      'height', 
      null);
        
      if ($.boxModel == 
      true) {
            
      hh = 
      Math.
      max(
      header1.
      height(), 
      header2.
      height());
        } 
      else {
            
      hh = 
      Math.
      max(
      header1.
      outerHeight(), 
      header2.
      outerHeight());
        }
        $(
      '.datagrid-view1 .datagrid-header table', 
      grid).
      height(
      hh);
        $(
      '.datagrid-view2 .datagrid-header table', 
      grid).
      height(
      hh);
        
      header1.
      height(
      hh);
        
      header2.
      height(
      hh);

        
      if (
      opts.
      height == 
      'auto') {
            $(
      '.datagrid-body', 
      grid).
      height($(
      '.datagrid-view2 .datagrid-body table', 
      grid).
      height());
        } 
      else {
            $(
      '.datagrid-body', 
      grid).
      height(
                
      opts.
      height 
       - (
      grid.
      outerHeight() - 
      grid.
      height())
                - $(
      '.datagrid-header', 
      grid).
      outerHeight(
      true)
                - $(
      '.datagrid-title', 
      grid).
      outerHeight(
      true)
                - $(
      '.datagrid-toolbar', 
      grid).
      outerHeight(
      true)
                - $(
      '.datagrid-pager', 
      grid).
      outerHeight(
      true)
            );
        }

        $(
      '.datagrid-view', 
      grid).
      height($(
      '.datagrid-view2', 
      grid).
      height());
        $(
      '.datagrid-view1', 
      grid).
      height($(
      '.datagrid-view2', 
      grid).
      height());
        $(
      '.datagrid-view2', 
      grid).
      css(
      'left', $(
      '.datagrid-view1', 
      grid).
      outerWidth());
    }

    
      /** 
       * wrap and return the grid object, fields and columns 
       */ 
       
      function 
      wrapGrid(target, rownumbers) {
        
      //
      首先在最外层包装一个
      div.datagrid,
      并且返回这个
      div.datagrid 
       
      var 
      grid = $(target).
      wrap(
      '
'
). parent(); // 下面开始在外层 div.datagrid 里面装东西 , 层次结构应该是 // // // // // // // // //
// // // // // grid. append( '
' + '
' + '
' + '
' + '
'
+ '
'
+ '
' + '
' + '
'
+ '
' + '
' + '
' + '
' + '
' + '
'
+ '
'
+ '
'
+ '
'
+ '
'
+ '
' + '
' ); // 获取哪些冻结列的信息 var frozenColumns = getColumns($( 'thead[frozen=true]', target)); // 把冻结列的表头删除掉 $( 'thead[frozen=true]', target). remove(); // 再获取非冻结列的信息 var columns = getColumns($( 'thead', target)); // 再把非冻结列的表头删掉 $( 'thead', target). remove(); // 把这个表格转移到之前包装好的层次 :div.datagrid->datagrid-wrap->datagrid-view->div.datagrid-vew2->div.datagrid-body $(target). attr({ cellspacing: 0, cellpadding: 0, border: 0 }). removeAttr( 'width'). removeAttr( 'height'). appendTo($( '.datagrid-view2 .datagrid-body', grid)); function getColumns(thead) { // thead 标签中获取列的设置信息 , 获取冻结列的信息或者是非冻结列的信息 // 返回的 columns 是纯二维对象数组 , : //[ // [ // {title:"",align:"",sortable:"",checkbox:""}, // {title:"",align:"",sortable:"",checkbox:""} // ], // [{title:"",align:"",sortable:"",checkbox:""}] //] var columns = []; $( 'tr', thead). each( function () { var cols = []; $( 'th', this). each( function () { // 可能这个版本不支持在 data-options 属性上设置列的属性 , 所以这里手动添加下面的代码块 var opx = eval( "({" + $( this). attr( "data-options") + "})"); var th = $( this); var col = { title: th. html(), align: th. attr( 'align') || 'left', sortable: th. attr( 'sortable') == 'true' || false, checkbox: th. attr( 'checkbox') == 'true' || false }; if ( th. attr( 'field')) { col. field = th. attr( 'field'); } if ( th. attr( 'formatter')) { col. formatter = eval( th. attr( 'formatter')); } if ( th. attr( 'rowspan')) col. rowspan = parseInt( th. attr( 'rowspan')); if ( th. attr( 'colspan')) col. colspan = parseInt( th. attr( 'colspan')); if ( th. attr( 'width')) col. width = parseInt( th. attr( 'width')); col = $. extend( col, opx); cols. push( col); }); columns. push( cols); }); return columns; } // 根据获取到的行列配置信息去拿字段 var fields = getColumnFields( columns); // 将盛放内容的 td 全部使用 class:datagrid-column-filedname 标记 , 并且里面放置 div.datagrid-cell $( '.datagrid-view2 .datagrid-body tr', grid). each( function () { for ( var i = 0; i < fields. length; i++) { $( 'td:eq(' + i + ')', this) . addClass( 'datagrid-column-' + fields[ i]) . wrapInner( '
'
); } }); // 给表格绑定 "_resize" 事件 grid. bind( '_resize', function () { var opts = $. data(target, 'datagrid'). options; if ( opts. fit == true) { setSize(target); fixColumnSize(target); } return false; }); return { grid: grid, frozenColumns: frozenColumns, columns: columns }; } function createColumnHeader(columns) { // 创建表头 , 创建的表头是一个 table 标签 , 这个 table 标签里 , 只有一个 thead, 里面可能有多个 tr var t = $( '
'
); for ( var i = 0; i < columns. length; i++) { var tr = $( ''). appendTo($( 'thead', t)); var cols = columns[ i]; for ( var j = 0; j < cols. length; j++) { var col = cols[ j]; var attr = ''; if ( col. rowspan) attr += 'rowspan="' + col. rowspan + '" '; if ( col. colspan) attr += 'colspan="' + col. colspan + '" '; var th = $( '+ attr + '>'). appendTo( tr); if ( col. checkbox) { th. attr( 'field', col. field); $( '
'
) . html( '') . appendTo( th); } else if ( col. field) { th. append( '
'
); th. attr( 'field', col. field); $( '.datagrid-cell', th). width( col. width); $( 'span', th). html( col. title); $( 'span.datagrid-sort-icon', th). html( ' '); } else { th. append( '
'
); $( '.datagrid-cell-group', th). html( col. title); } } } return t; } /** * set the common properties */ function setProperties(target) { var grid = $. data(target, 'datagrid'). grid; var opts = $. data(target, 'datagrid'). options; var data = $. data(target, 'datagrid'). data; if ( opts. striped) { $( '.datagrid-view1 .datagrid-body tr:odd', grid). addClass( 'datagrid-row-alt'); $( '.datagrid-view2 .datagrid-body tr:odd', grid). addClass( 'datagrid-row-alt'); } if ( opts. nowrap == false) { $( '.datagrid-body .datagrid-cell', grid). css( 'white-space', 'normal'); } $( '.datagrid-header th:has(.datagrid-cell)', grid). hover( function () { $( this). addClass( 'datagrid-header-over'); }, function () { $( this). removeClass( 'datagrid-header-over'); } ); $( '.datagrid-body tr', grid). mouseover( function () { var index = $( this). attr( 'datagrid-row-index'); $( '.datagrid-body tr[datagrid-row-index=' + index + ']', grid). addClass( 'datagrid-row-over'); }). mouseout( function () { var index = $( this). attr( 'datagrid-row-index'); $( '.datagrid-body tr[datagrid-row-index=' + index + ']', grid). removeClass( 'datagrid-row-over'); }). click( function () { var index = $( this). attr( 'datagrid-row-index'); if ($( this). hasClass( 'datagrid-row-selected')) { unselectRow(target, index); } else { selectRow(target, index); } if ( opts. onClickRow) { opts. onClickRow. call( this, index, data. rows[ index]); } }). dblclick( function () { var index = $( this). attr( 'datagrid-row-index'); if ( opts. onDblClickRow) { opts. onDblClickRow. call( this, index, data. rows[ index]); } }); function onHeaderCellClick() { var field = $( this). parent(). attr( 'field'); var opt = getColumnOption(target, field); if (! opt. sortable) return; opts. sortName = field; opts. sortOrder = 'asc'; var c = 'datagrid-sort-asc'; if ($( this). hasClass( 'datagrid-sort-asc')) { c = 'datagrid-sort-desc'; opts. sortOrder = 'desc'; } $( '.datagrid-header .datagrid-cell', grid). removeClass( 'datagrid-sort-asc'); $( '.datagrid-header .datagrid-cell', grid). removeClass( 'datagrid-sort-desc'); $( this). addClass( c); if ( opts. onSortColumn) { opts. onSortColumn. call( this, opts. sortName, opts. sortOrder); } request(target); } function onHeaderCheckboxClick() { if ($( this). attr( 'checked')) { $( '.datagrid-view2 .datagrid-body tr', grid). each( function () { if (!$( this). hasClass( 'datagrid-row-selected')) { $( this). trigger( 'click'); } }); } else { $( '.datagrid-view2 .datagrid-body tr', grid). each( function () { if ($( this). hasClass( 'datagrid-row-selected')) { $( this). trigger( 'click'); } }); } } $( '.datagrid-header .datagrid-cell', grid). unbind( '.datagrid'); $( '.datagrid-header .datagrid-cell', grid). bind( 'click.datagrid', onHeaderCellClick); $( '.datagrid-header .datagrid-header-check input[type=checkbox]', grid). unbind( '.datagrid'); $( '.datagrid-header .datagrid-header-check input[type=checkbox]', grid). bind( 'click.datagrid', onHeaderCheckboxClick); $( '.datagrid-header .datagrid-cell', grid). resizable({ handles: 'e', minWidth: 50,
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章