Search code examples
javascriptjqueryjquery-uijquery-pluginsfixed-header-tables

How to pass a dynamic list of comma separated elements into a jQuery Plugin's options


I am working with the jQuery fixedheadertable plugin. This plugin allows user to have a fixed header that stays in a static position whenever scrolling down the table. My question is more of a general js question, however.

Here is my code that initializes the plugin:

setupClipboardTable:function(){
    var height  = jQuery('#clipboardTypeContainer').height();
    var width   = jQuery('#clipboardTypeContainer').width();
    var colW    = 150 
    var colCnt  = jQuery('#adCbColCnt').val();

    //var colratioString = [ colW, colW, colW, colW, colW, colW, colW, colW, colW, colW, colW, colW, colW, colW, colW, colW, colW, colW ];
    var colratioString = [ 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150 ];

    jQuery('#Clipboard'+cbType).fixheadertable({
         height : height,
         whiteSpace     : 'nowrap', //normal
         addTitles      : true,
         colratio       : colratioString,
    });
},

Basically, my problem is that for my table, the number of columns will change based on user selected preferences. Once that number changes, we have to re-run this function in order to get the table to appear correctly. The main idea is that the var 'colratioString' has to have the same exact amount of values in it as there are number of columns in the table. Those numbers (ex: 150) can be set to whatever we want, but for the sake of simplicity I'm keeping them all at 150 right now.

The hurdle I am trying to get over is the fact that sometimes I may need the colratioString to look like this:

colratioString = [ 150, 150, 150, 150]

and sometimes I may need it to look like this

colratioString = [ 150, 150, 150, 150, 150, 150, 150]

and so on.

Is there some way I can take the colCnt value (which is the correct # of columns I will have), and create a colratioString with the correct number of values?

Here's an example. If...

var colCnt = 5

Then

var colratioString = [ 150, 150, 150, 150, 150 ]

I'm having trouble with how I can take the value of colCnt, and make a colratioString variable that contains the exact formatting that I need.

Thank you for reading!


Solution

  • You can create the array, and the fill it :

    var colratioString = [];
    for (var i = 0; i < colCnt; i++) colratioString[i] = 150;