?
Solved

DataTables: SELECT filter html data

Posted on 2011-02-17
6
Medium Priority
?
2,074 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:Gurvinder Pal Singh
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:Gurvinder Pal Singh
ID: 34914579
but why do you need to put an anchor tag in the header?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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:
Gurvinder Pal Singh earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

864 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