Solved

DataTables: SELECT filter html data

Posted on 2011-02-17
6
1,975 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to filter by key press ? 6 53
ASP.NET - Why is accordion not working? 3 44
Clear input text 15 26
Link Anchor Not working 9 44
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

685 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