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!
daniel_spiriAsked:
Who is Participating?
 
Gurvinder Pal SinghConnect With a Mentor Commented:
i suggest rather than using anchor tag in the cells of body rows', use an onclick event on the table cell

<td onclick="gotTo('france')" onmouseover = "this.style.cursor = 'pointer'" onmouseout="this.style.cursor='auto'">CellText</td>


function goTo(site)
{
   location.href = site;
}

0
 
Gurvinder Pal SinghCommented:
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
0
 
daniel_spiriAuthor Commented:
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

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Gurvinder Pal SinghCommented:
but why do you need to put an anchor tag in the header?
0
 
daniel_spiriAuthor Commented:
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!
0
 
daniel_spiriAuthor Commented:
Thanks alot, works perfect!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.