Solved

DataTables: SELECT filter html data

Posted on 2011-02-17
6
1,945 Views
Last Modified: 2013-11-05
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!
0
Comment
Question by:daniel_spiri
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34914294
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
 

Author Comment

by:daniel_spiri
ID: 34914548
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
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34914579
but why do you need to put an anchor tag in the header?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:daniel_spiri
ID: 34914594
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
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 34914672
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
 

Author Comment

by:daniel_spiri
ID: 34915026
Thanks alot, works perfect!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now