使用 jQuery 隐藏和显示列

PC上的Geaper

我正在尝试显示和隐藏列,但我的代码没有完成它的工作。

基本上我希望能够根据类名隐藏和显示列。

我隐藏和显示列的功能不起作用。

$(document).ready(function () {
    appendHeader();
    select();
    var amountOfDayEnds = parseInt($('#amountOfDayEnds').val());
    appendBody(amountOfDayEnds);
});

$('#group').change(function () {
   select();
});

//Change Header based on the select
function select() {
    var group = $('#group').val();
    // Get the column API object
    console.log(group);
    switch (group) {
        case "DDA":
            hideColumn("mtg");
            hideColumn("sav");
            showColumn("dda");
            break;
        case "SAV":
            hideColumn("mtg");
            showColumn("sav");
            hideColumn("dda");
            break;
        case "MTG":
            showColumn("mtg");
            hideColumn("sav");
            hideColumn("dda");
            break;
    }
}

function hideColumn(className){

    var columnIndex = $("#dataTable thead tr th."+className).index();

    $("#dataTable thead tr th:eq("+columnIndex+")").hide();

    $("#dataTable tbody tr").each(function(){
        $(this).find("td:eq("+columnIndex+")").hide();
    });
}

function showColumn(className){

    var columnIndex = $("#dataTable thead tr th."+className).index();

    $("#dataTable thead tr th:eq("+columnIndex+")").show();

    $("#dataTable tbody tr").each(function(){
        $(this).find("td:eq("+columnIndex+")").show();
    });
}


//Append Header
function appendHeader() {
    var thead = '<thead>';
    thead += "<tr class='text-primary text-center'>";
    thead += '<th>Day</th>';
    thead += '<th class="dda">Type 400</th>';
    thead += '<th class="dda">Type 4044</th>';
    thead += '<th class="dda">Type 4045</th>';
    thead += '<th class="sav">Type 300</th>';
    thead += '<th class="sav">Type 310</th>';
    thead += '<th class="mtg">Type 700</th>';
    thead += '<th class="mtg">Type 710</th>';
    thead += '</tr>';
    thead += '</thead>';

    $('#dataTableHead').append(thead);
}

编辑:似乎我只隐藏了每种类型的一列。我想隐藏所有带有类名的列。

有人可以告诉我我在这里缺少什么吗?

科林·克莱恩

它可能比这更简单。您通过 js 将一个元素添加到 DOM 中,因此最好通过缓存它,$(document).find()因此在这种情况下,您始终可以确定 js 会找到它并且永远不会得到元素不存在错误。您使用在您的情况下不推荐的开关盒(如果/否则更好)

注意:有一些函数可以操作/添加一些元素到这个表中,你错过了为我们添加到你的代码中,所以这个代码可以很好地与你当前的代码一起使用。如果它不起作用td,因为你没有在你的代码中添加它们,我不知道它们是什么样子或它们有什么类。
提琴手

JS/jQuery

$(document).ready(function () {
    appendHeader();
    select();
    var amountOfDayEnds = parseInt($('#amountOfDayEnds').val());
    appendBody(amountOfDayEnds);
});

$('#group').change(function () {
   select();
});

//Change Header based on the select
function select() {
    var group = $('#group').val();
    // Get the column API object
    console.log(group);
    if(group == 'dda'){
            hideColumn("mtg");
            hideColumn("sav");
            showColumn("dda");
    } else if(group == 'sav'){
            hideColumn("mtg");
            showColumn("sav");
            hideColumn("dda");

    } else {
            showColumn("mtg");
            hideColumn("sav");
            hideColumn("dda");

    }
}

function hideColumn(className){
    $(document).find("#dataTableHead thead th."+className).hide();
}

function showColumn(className){
    $(document).find("#dataTableHead thead th."+className).show();

}


//Append Header
function appendHeader() {
    var thead = '<thead>';
    thead += "<tr class='text-primary text-center'>";
    thead += '<th>Day</th>';
    thead += '<th class="dda">Type 400</th>';
    thead += '<th class="dda">Type 4044</th>';
    thead += '<th class="dda">Type 4045</th>';
    thead += '<th class="sav">Type 300</th>';
    thead += '<th class="sav">Type 310</th>';
    thead += '<th class="mtg">Type 700</th>';
    thead += '<th class="mtg">Type 710</th>';
    thead += '</tr>';
    thead += '</thead>';

    $('#dataTableHead').append(thead);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用jQuery隐藏/显示列

来自分类Dev

使用jQuery显示和隐藏菜单

来自分类Dev

使用JavaScript或jQuery显示和隐藏框架

来自分类Dev

使用jQuery显示和隐藏表列

来自分类Dev

隐藏和显示Div而不使用Jquery

来自分类Dev

使用jQuery显示和隐藏每次点击

来自分类Dev

使用jQuery Mobile隐藏和显示div

来自分类Dev

使用JavaScript或jQuery显示和隐藏框架

来自分类Dev

jQuery使用单选按钮隐藏和显示

来自分类Dev

隐藏和显示Div而不使用Jquery

来自分类Dev

如何使用 jQuery 隐藏和显示 div

来自分类Dev

如何使用jQuery显示隐藏和显示模式弹出窗口

来自分类Dev

隐藏,使用jquery显示

来自分类Dev

隐藏,使用jquery显示

来自分类Dev

如何使用 jquery 和 css 隐藏和显示侧边栏?

来自分类Dev

如何使用if语句使用jquery显示和隐藏2 div?

来自分类Dev

使用jQuery显示和隐藏动态生成的表元素

来自分类Dev

使用CSS和jQuery隐藏/显示表单字段

来自分类Dev

使用单选按钮和JQuery显示/隐藏Dropbox

来自分类Dev

无法使用jQuery隐藏和显示文本框

来自分类Dev

如何使用jQuery显示和隐藏这项工作

来自分类Dev

使用jQuery隐藏和显示HTML中的字段

来自分类Dev

在jQuery中使用setInterval显示和隐藏div

来自分类Dev

使用CSS和jQuery隐藏/显示表单字段

来自分类Dev

在jQuery中使用悬停显示和隐藏文本

来自分类Dev

使用jQuery显示和隐藏2个按钮

来自分类Dev

使用jQuery隐藏和显示具有相同类的类

来自分类Dev

在jQuery中使用keydown函数隐藏和显示图像

来自分类Dev

使用 javascript 或 jquery 隐藏和显示 2 个不同的元素