Quantcast
Channel: DataTables 1.9 — DataTables forums
Viewing all articles
Browse latest Browse all 1816

Sorting secondary table by values on a column in first table.

$
0
0
Hey everyone.

I am currently building a system for my company that allows users to see a list of contracts within a table, there is also a secondary table below listing items within the contracts in the first table.

What I am trying to do is:

- Allow the user to search the first table
- With the filtered results collect the values from the "Contract ID" column
- Then with those values filter the second table to only show items related to the filtered contracts in table1.

Here is what I currently have:

	/**
	 * Performs post initialization for tables processed by the dataTable library
	 * @param  {Object} table
	 */
	Controller.prototype.onTableInitializeComplete = function(table) {
		/**
		 * Change the placeholder for the filter element
		 */
		$("#" + table.sTableId+"_filter input").attr("placeholder", "Filter...");

		/**
		 * Get the input for the contracts table
		 */
		var input = $("#" + table.sTableId+"_filter input");

		/**
		 * Fetch the table id from the data-* api
		 */
		var id = $(table.nTable).data('tableid');

		/**
		 * Id this table is the contracts table then
		 */
		if(id == 'contracts')
		{
			/**
			 * Listen for the keyup event
			 */
			input.keyup(function(e){				
				/**
				 * We need to collect the contract numbers from the filtered results.
				 */
				var serials = [];
				var rows = $('tbody tr td:nth-child(6)', table.nTable).each(function(pos, context){
					serials.push(context.innerText.toString().replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
				});

				/**
				 * Fetch the contracts line table.
				 */
				var lineTable = $('table[data-tableid="contractsline"]').DataTable();

				/**
				 * Update the contracts line with the filter from the contracts table.
				 */
				lineTable.fnFilter(serials.join("|"), 8, true);
			});
		}
	};

It does not work as expected, I know that the
serials
array contains a list of serials and the join looks like so:

856|947|1482/01|1482/02|1482/03|1482/04|1482/05|1482/06|1482/07|1482/09 

When I start to filter the first table I get the following error within the console

Uncaught TypeError: Cannot call method 'fnGetData' of undefined datatables.js:28
v datatables.js:28
Ha datatables.js:53
K datatables.js:52
(anonymous function) datatables.js:51
jQuery.event.dispatch jquery.js:3058
elemData.handle.eventHandle

Also to note is that when I filter, the second table becomes empty, and as I continue to type into the first tables filter, once the table is empty the second table shows all results.

Something weird is going on here and I need some help trying to figure it out.

P.S, Great plugin.. amazing in fact. good job.

Viewing all articles
Browse latest Browse all 1816

Trending Articles