1

I'm hoping to replace my tables with Dynatable, but I need to be able to hide and show certain groups of columns to do so. I do this in the existing, regular html table by giving a group to each , for example:

<td class = "group1">cell value</td>

Then I have some hide and show javascript functions:

function hideCol(columnClass){
      $('table .'+columnClass).each(function(index) {
        $(this).hide();
      });

      $('ul#hiddenCols').append('<li id="'+columnClass+'"><a href="javascript:;" onclick="showCol(\''+columnClass+'\');">Show '+columnClass+'</a></li>');
    }

function showCol(columnClass){
  $('table .'+columnClass).each(function(index) {
    $(this).show();
  });

  $('li#'+columnClass).remove();
    }

$(document).ready(function() {
    hideCol("Group2");
    hideCol("Group3");
    hideCol("Group4");

    $('#radio1').click(function() {
    /*$(this).parent().addClass("active").siblings().removeClass("active")*/
        showCol("Group1");        
        hideCol("Group2");
        hideCol("Group3");    
        hideCol("Group4");
    });

Is there a reasonably straight forward way I can adapt Dynatable to do something similar? Is there a way I can assign classes to each and in a Dynatable?

Thanks a lot, Alex

4

2 回答 2

0

您可以按如下方式编辑 dynatable.js 的源代码,然后使用 show() 和 hide() 方法...

function DomColumns(obj, settings) {
...
this.hide = function(indexOrId) {
    var columns = settings.table.columns;
    if (typeof indexOrId == "number")
        columns[indexOrId].hidden = true;
    else {
        for (var i = columns.length - 1; i >= 0; i--) {
            if (columns[i].id == indexOrId) {
                 columns[i].hidden = true;
                 break;
            }
        }
    }
    obj.$element.find(settings.table.headRowSelector).children('[data-dynatable-column="' + indexOrId + '"]')
        .first().hide();
    obj.dom.update();
};
this.show = function(indexOrId) {
    var columns = settings.table.columns;
    if (typeof indexOrId == "number")
        columns[indexOrId].hidden = false;
    else {
        for (var i = columns.length - 1; i >= 0; i--) {
            if (columns[i].id == indexOrId) {
                columns[i].hidden = false;
                break;
            }
        }
    }
    obj.$element.find(settings.table.headRowSelector).children('[data-dynatable-column="' + indexOrId + '"]')
    .first().show();
    obj.dom.update();
};
...
};
于 2014-12-03T09:30:05.263 回答
0

有一个设置可以让每列中的数据继承列标题的类。但是,正如我在此问题中提到的那样,在对隐藏列进行排序时似乎存在错误。

我无法使用 Dynatable 进行这项工作,所以我切换到Datatables,其中隐藏/排序列似乎更加稳定。

于 2014-06-03T21:38:58.047 回答