如何在extjs网格面板中合并单元格?

zte4gxcn  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(309)

我有extjsgrid.Panel,并且我尝试模拟当连续行的第一列中存在重复数据时行中的单元格合并。使用列呈现器,如果某行的第一列值与前一行的值重复,则清除该值。我希望修改前一行的第一个单元格的tdAttr,以使行跨越适当的行数。我不知道如何到达前一行。'下面是我目前的渲染器代码。

renderer: function(value, metaData, record, row, col, store, gridView){                    

                     var dataIndex = metaData.column.dataIndex;
                     var overrideValue = value;

                     // check previous rows and determine whether this row's first column has the same
                     // value as a previous row's first column
                     if (col == 0){
                        var i = row-1;
                        while (i >= 0 && overrideValue != ""){
                           if (store.data.items[i].get(dataIndex) == value){
                              overrideValue = "";
                              // ----- here i want to update record i's metaData...
                              // metaData.tdAttr = 'rowspan=' + row-i+1
                           } 
                           i--;     
                        }
                     }

                     return overrideValue;
                  }

我尝试了很多方法来获取与记录关联的元数据,但是都不成功。gridView对象的getnodes和getnode方法返回undefined。

1zmg4dgp

1zmg4dgp1#

我创建了以下解决方案。我使用了列呈现器和视图侦听器的组合,特别是“viewready”。网格的行线设置为“false”,我使用列呈现器应用自定义样式来替换行线,并清除相应的单元格值。我在“viewready”侦听器中修改了“合并”单元格的背景颜色。

<style>
.mywrapcell .x-grid-cell-inner {
   white-space: normal;
   border-top-color: #c6c6c6;
   border-top-width: 1px;
   border-top-style: solid;
}
</style>

_addGrid: function() {

      var myModel = Ext.define('User', {
        extend: 'Ext.data.Model',
        fields: [
            {name: 'col1', type: 'string'},
            {name: 'col2',  type: 'string'},
        ]
       });

      var myData = [{col1: 'U2', col2: 'Jan 5'},
                    {col1: 'U2', col2: 'Jan 6'},
                    {col1: 'Crowded House', col2: 'Feb 19'},
                    {col1: 'Jack Johnson', col2: 'Apr 4'},
                    {col1: 'Jack Johnson', col2: 'Apr 5'},
                    {col1: 'Cage the Elephant', col2: 'May 10'},
                    {col1: 'Cage the Elephant', col2: 'May 11'},
                    {col1: 'Blondie', col2: 'Jun 6'},
                    {col1: 'Gorillaz', col2: 'Jun 19'},
                    {col1: 'Gorillaz', col2: 'Jun 20'}];

      var mystore = Ext.create('Ext.data.Store', {
         model: myModel,
         data: myData,
         proxy: {
            type: 'memory',
            enablePaging: false
         }
      });

      var mygrid = Ext.create('Ext.grid.Panel', {
         store: mystore,
         itemId: 'grid',
         viewConfig:{
            listeners: {
               viewready: function(view){

                  var dataIndex = 'col1';

                  // find the index of the column for row spanning
                  var colindex = view.ownerCt.columns.findIndex((x) => x.dataIndex == dataIndex);

                  if (colindex != -1){
                     var viewrows = view.getNodes();
                     var bgcolor = '#fff';

                     // set all the cells to the                      
                     // same background color
                     for(i=0;i<viewrows.length;i++){
                    viewrows[i].cells[colindex].style.backgroundColor = bgcolor;
                     }
                  }
            }
         },
         enableEditing: false,
         enableBulkEdit: false,
         showRowActionsColumn: false,
         enableRanking: false,
         sortableColumns: false,
         columnLines: true,
         rowLines: false, //row lines handled in renderer
         width: 225,
         columns: [
               {text: 'Artist',
                dataIndex: 'col1',
                flex: 1,
                renderer: function(value, metaData, record, row, col, store, view){                    

                      var overrideValue = value;

                      // check previous rows and determine whether this row's first column has the same
                      // value as a previous row's first column
                      var i = row-1;

                      while (i >= 0 && overrideValue != ""){
                         if (store.data.items[i].get(metaData.column.dataIndex) == value){
                            overrideValue = '';
                         } 
                         i--;     
                      }

                      if (overrideValue != ""){
                         metaData.tdCls = 'mywrapcell';
                      }
                      return overrideValue;
                }
               },
               {
                  text: 'Show Date',
                  dataIndex: 'col2',
                  tdCls: 'mywrapcell'
               }
         ],
      });

      this.add(mygrid);

 }

and the result...

相关问题