Remove columns with multiple checkboxes

47 views
Skip to first unread message

Pedro Guimarães

unread,
Nov 23, 2012, 10:51:01 AM11/23/12
to google-visua...@googlegroups.com
**Code below: Working well.**

    (...)
    var check_box_values = $('#myForm [type="checkbox"]:not(:checked)').map(function ()
    {
    return this.value;
    }).get();

**Code below: Working well.**


    function f(check_box_values)
    {
    (...)
    var obj = jQuery.parseJSON(jsonData);
    var data = google.visualization.arrayToDataTable(obj);

**Code below: for cycle goes fine but if statement never starts**


    for (var i = 1; i < data.getNumberOfColumns()-1; i++) {
    {
    if ($.inArray(i, check_box_values) > -1)
    {
    data.removeColumn(i);
    }
    }
    }

**Html code: Working well.**

What am I doing wrong?
note: the check_box_values array is populated.

**edit: (eg.)**
`getNumberOfColumns: 6`
 check_box_values array: [1,3,5]

asgallant

unread,
Nov 23, 2012, 11:28:51 AM11/23/12
to google-visua...@googlegroups.com
jQuery's inArray method requires data type matching, too.  Since the value of any HTML element is a string, you are trying to match a number to a string, e.g.1 === "1", which will always be false.  To fix this, parse your values as integers, ie:

var check_box_values = $('#myForm [type="checkbox"]:not(:checked)').map(function () {
    return parseInt(this.value);
}).get();

Give that a try and see if it works for you.
Message has been deleted

Pedro Guimarães

unread,
Nov 23, 2012, 12:13:24 PM11/23/12
to google-visua...@googlegroups.com
Yeah, it work it, now i'm having trouble with columns :s

It dont remove proper, something is wrong

asgallant

unread,
Nov 23, 2012, 12:17:05 PM11/23/12
to google-visua...@googlegroups.com
What is happening that shouldn't happen; or, what isn't happening that should happen?  Are you getting any error messages?

Pedro Guimarães

unread,
Nov 23, 2012, 12:19:29 PM11/23/12
to google-visua...@googlegroups.com
Hm, I think is because evertime I do removeColumn(i), column number becomes -1

eg:

Column (1)
Column (2)
Column (3)

If removeColumn(2)

It turns (old-new):

column (1) - column (1)
column (3) - cloumn (2)

asgallant

unread,
Nov 23, 2012, 12:23:59 PM11/23/12
to google-visua...@googlegroups.com
Yup, that could screw things up for you easily enough.  Try going backwards through the columns instead:

for (var i = data.getNumberOfColumns() - 1; i > 0; i--) {
    if ($.inArray(i, check_box_values) > -1) { 
        data.removeColumn(i);

Pedro Guimarães

unread,
Nov 23, 2012, 12:32:34 PM11/23/12
to google-visua...@googlegroups.com
You were rigth once more :D

I was searching new solutions already :p

eg.

var view = new google.visualization.DataView(dataResponse);
view.setColumns([0,1,3]); //here you set the columns you want to display

or should I keep using columnRemove? Whats your advice? :)

asgallant

unread,
Nov 23, 2012, 10:45:32 PM11/23/12
to google-visua...@googlegroups.com
Removing columns from the DataTable destroys them permanently, whereas specifying which columns to show/hide in a DataView preserves the underlying data.  Generally, I would recommend a DataView over using removeColumn.
Reply all
Reply to author
Forward
0 new messages