Link to home
Start Free TrialLog in
Avatar of daniel_spiri
daniel_spiri

asked on

DataTables: SELECT filter html data

Hello,

I am using "DataTables individual column filtering example (using select menus)" http://datatables.net/examples/api/multi_filter_select.html for creating this functionality for my table.

One of the columns has rows with A HREF elements:

<a href="http://localhost/France">France</a><br />


Problem are the generated SELECT filters.
The elements in the list are generated like:
France">France
and not
France

Thanks a lot for your support!
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

This is happening in the header or the body rows' cells?

Looks more link a missed placed quote problem. Can't say much unless i see either the source code or live link on internet
Avatar of daniel_spiri
daniel_spiri

ASKER

This happens in the header, where select boxes are generated by JS (like in the example).

JS:
<script type="text/javascript" src="http://localhost/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="http://localhost/TableTools.min.js"></script>

<script type="text/javascript" charset="utf-8">
			(function($) {
			/*
			 * Function: fnGetColumnData
			 * Purpose:  Return an array of table values from a particular column.
			 * Returns:  array string: 1d data array 
			 * Inputs:   object:oSettings - dataTable settings object. This is always the last argument past to the function
			 *           int:iColumn - the id of the column to extract the data from
			 *           bool:bUnique - optional - if set to false duplicated values are not filtered out
			 *           bool:bFiltered - optional - if set to false all the table data is used (not only the filtered)
			 *           bool:bIgnoreEmpty - optional - if set to false empty values are not filtered from the result array
			 * Author:   Benedikt Forchhammer <b.forchhammer /AT\ mind2.de>
			 */
			$.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) {
				// check that we have a column id
				if ( typeof iColumn == "undefined" ) return new Array();
				
				// by default we only wany unique data
				if ( typeof bUnique == "undefined" ) bUnique = true;
				
				// by default we do want to only look at filtered data
				if ( typeof bFiltered == "undefined" ) bFiltered = true;
				
				// by default we do not wany to include empty values
				if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true;
				
				// list of rows which we're going to loop through
				var aiRows;
				
				// use only filtered rows
				if (bFiltered == true) aiRows = oSettings.aiDisplay; 
				// use all rows
				else aiRows = oSettings.aiDisplayMaster; // all row numbers
			
				// set up data array	
				var asResultData = new Array();
				
				for (var i=0,c=aiRows.length; i<c; i++) {
					iRow = aiRows[i];
					var aData = this.fnGetData(iRow);
					var sValue = aData[iColumn];
					
					// ignore empty values?
					if (bIgnoreEmpty == true && sValue.length == 0) continue;
			
					// ignore unique values?
					else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
					
					// else push the value onto the result data array
					else asResultData.push(sValue);
				}
				
				return asResultData;
			}}(jQuery));
			
			
			function fnCreateSelect( aData )
			{
				var r='<select><option value="">All</option>', i, iLen=aData.length;
				for ( i=0 ; i<iLen ; i++ )
				{
					r += '<option value="' + aData[i] + '">' + aData[i] + '</option>';
				}
				return r+'</select>';
			}
	
			jQuery.fn.dataTableExt.aTypes.push(
				function ( sData ) {
					return 'html';
				}
			);				

			function fnShowHide( iCol )
			{
				/* Get the DataTables object again - this is not a recreation, just a get of the object */
				var oTable = $('#example').dataTable();
				
				var bVis = oTable.fnSettings().aoColumns[iCol].bVisible;
				oTable.fnSetColumnVis( iCol, bVis ? false : true );
			}

			$(document).ready(function() {
				/* Initialise the DataTable */
				var oTable = $('#example').dataTable( {
					"bPaginate": false,
					"bSort": true,
					"bSortClasses": true,
					"oLanguage": {
						"sSearch": "Search all columns:",
						"sLengthMenu": ''
					}

				} );
				
				/* Add a select menu for each TH element in the table footer */
				$("thead th").each( function ( i ) {
					this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(i) );
					$('select', this).change( function () {
						oTable.fnFilter( $(this).val(), i );
					} );
				} );
			} );
		</script>

Open in new window


Table:
 
<tr class="one">
        <td>
        <a href="http://localhost/right_now">Right Now</a>
        </td>
        <td>
        Level1 X + Level2 Y
        </td>
        <td>
        
        </td>

</tr>


<tr class="two">
        <td>
        <a href="http://localhost/content_viewn">Content Viewn</a>
        </td>
        <td>
        Level1 X + Level2 Y
        </td>
        <td>
        
        </td>
</tr>

Open in new window

but why do you need to put an anchor tag in the header?
It is not in the header, it is in the rows.
Information in the rows need to link somewhere.

Problem is with the table headers, where select boxes are generated based on values from the rows.
These select boxes act like filters on table data. They are populated with unique records from the rows.
Problem is when they are generated, they also containt part of the a href HTML (as you can see in the example).

Thanks alot!
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks alot, works perfect!