Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82012

Select the datatable instance in a function under fnServerData

$
0
0
Hell again!

I don't completely understand things in javascript so I was hoping someone could answer this question.

The following code works great! And it is repeated on all 4 tables. I wanted to create a function for it and send the variables I need but that won't work for some reason. Here is the code I am talking about:

Works:
                  "fnServerData": function ( sSource, aoData, fnCallback ) {
				/* ... additional variables ... */
				$.getJSON( sSource, aoData, function (json) {
					if(json.error == 'expired'){
						timeoutLogoff();
					}else{
						/* Create the select elements on the first run */
						if ( json.sEcho == 1 )
						{
							build_dropdowns('Assignments',json,oTable);
						}
						/* DataTables callback */
						fnCallback(json)
					}
				} );
			}

note that build_dropdowns is a function that will create teh drop downs based off the json data. Here is it's code:
          function build_dropdowns(tableID, json, refName){
			$("#"+tableID+" tfoot th.dropdown").each( function () {
				index = refName.fnVisibleToColumnIndex($(this).index());
				$(this).html(fnCreateSelect(json.select[index], index));
				$('select', $(this)).change( function () {
					refName.fnFilter( $(this).val(), refName.fnVisibleToColumnIndex($(this).parent().index() ));
				} );
			} );
		}

Notice how this function needs the refName (like oTable or wTable) to call fnFilter etc... I couldn't figure out a way to get the instance of datatable based off the ID of the table.

So, I changed fnServerData to this:
//**in the global scope area at the top of the page**//
                function build_fnServerData(table,refName,sSource, aoData,fnCallback){
			$.getJSON( sSource, aoData, function (json) {
				if(json.error == 'expired'){
					timeoutLogoff();
				}else{
					if ( json.sEcho == 1 ) {
						build_dropdowns(table,json,refName); // Create the select elements on the first run
					}
					return fnCallback(json)
				}
			} );
		}
 /* ... */

//**in the wTable datatable setup area**//
                                       "fnServerData": function ( sSource, aoData, fnCallback ) {
						build_fnServerData('WO',wTable,sSource, aoData,fnCallback);
					}

I don't know the best solution. Is there a way to reference wTable dynamically?
When I pass wTable from fnServerData it says "wTable not defined" but if I hard code wTable into the build_dropdowns function within build_fnServerData it will work... Is this because getJSON is asyncronous so when getJSON is called the datatables object is created? Is there a way to dynamically get the datatable instance like $('#WO').thisInstanceOfDatatables? Should I pass a string (or have a global has value to convert) and use eval to use the correct variable name when it is needed?

Thanks for the help!

Viewing all articles
Browse latest Browse all 82012

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>