1

This is pretty simple, but I need to be able to toggle showing columns in a table.

The idea is to have a function that will take the name of the column (all the rows have the same data-field in the same column) and hide it, adjusting the width of the table as it goes.

Using Chrome's inspector, I came up with a simple proof of concept

$('[data-id="column"],[data-field="column"]').toggle()

After running the above, I need to adjust the width of the element plus / minus 4 pixels for the table to fit correctly - The table renders a white box on the right hand side where the column(s). This is just a proof of concept and the actual width will be stored in a variable that will be manipulated to allow multiple columns to be hidden.

$(".oe_list_content").width($(window).width()+/-4)

I made a simple function to come up with the initial idea, but when I run the function, it returns undefined and doesn't make the changes. I'm fairly certain that it can, but is the jQuery selector not able to take a function variable?

function rowToggle(row){
    // Verify the rows even exist in the DOM
    if($('[data-id="'+row+'"]').length){
        $('[data-id="'+row+'"]','[data-field="'+row+'"]').toggle();
        if($('[data-id="'+row+'"]').is(':hidden')){
            $(".table").width($(window).width()+4);
        }else{
            $(".table").width($(window).width()-4);
        }
    }
}

This really seems like a variable problem because

var row = "column";
$('[data-id="'+ row +'"]').toggle();

doesn't actually toggle the field. Am I missing something?

3
  • 2
    could you post html you are working with ? Commented Dec 29, 2015 at 21:10
  • var row = "column"; I hope you realize that is weird. Also if you want a function to return something you must specify a return statement. Commented Dec 29, 2015 at 21:11
  • The selector is just a string, concatenating a variable will work as well. For example Commented Dec 29, 2015 at 21:13

1 Answer 1

2

This line $('[data-id="'+row+'"]','[data-field="'+row+'"]').toggle();

Should be: $('[data-id="'+row+'"],[data-field="'+row+'"]').toggle();

It's a single selector string. Not two strings separated by a comma as you had it.

Further reference to this issue: jQuery attribute selector for multiple values

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.