Solved

JQuery Mobile - Listview slow

Posted on 2013-01-15
3
2,551 Views
Last Modified: 2013-01-26
Hi,

I have a listview with many records taken from a mysql table, there are about 4000. When I search it all happens very slowly.
Doing research on google I found a solution https://github.com/jquery/jquery-mobile/blob/4ffb0a5f33ecf7a0df5caa1ed69dc02b57126195/js/jquery.mobile.listview.filter.js

I tested it and it works really well, the only problem I have is that displays two search input.
Can anyone help me?

Simple page:
<!DOCTYPE html> 
<html> 
	<head> 
	<title>Elenco Clienti</title> 

	<meta name="viewport" content="width=device-width, initial-scale=1"> 
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
	<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
	<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head>



<script type="text/javascript">

/*
* jQuery Mobile Framework : "listview" filter extension
* Copyright (c) jQuery Project
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*/
(function($, undefined ) {

$.mobile.listview.prototype.options.filter = false;
$.mobile.listview.prototype.options.filterPlaceholder = "Filter items...";

$( ":jqmData(role='listview')" ).live( "listviewcreate", function() {
	var list = $( this ),
		listview = list.data( "listview" );

	if ( !listview.options.filter ) {
		return;
	}

	var wrapper = $( "<form>", { "class": "ui-listview-filter ui-bar-c", "role": "search" } ),

		search = $( "<input>", {
				placeholder: listview.options.filterPlaceholder
			})
			.attr( "data-" + $.mobile.ns + "type", "search" )
			.bind( "keyup change", function() {

				var val = this.value.toLowerCase(),
					listItems=null,
					lastval = $( this ).jqmData('lastval')+"";

				//change val as lastval for next execution  
				$(this).jqmData( 'lastval' , val );

				change = val.replace( new RegExp( "^" + lastval ) , "" );

				if( val.length < lastval.length || change.length != ( val.length - lastval.length ) ){

					//removed chars or pasted something totaly different, check all items
					listItems = list.children();
				} else {

					//only chars added, not removed, only use visible subset
					listItems = list.children( ':not(.ui-screen-hidden)' );
				}

				if ( val ) {

					// This handles hiding regular rows without the text we search for
					// and any list dividers without regular rows shown under it
					var item, 
						childItems = false,                        
						itemtext="";

					for (var i = listItems.length; i >= 0; i--) {
						item = $( listItems[i] );
						itemtext = item.jqmData( 'filtertext' ) || item.text();

						if ( item.is( "li:jqmData(role=list-divider)" ) ) {

							item.toggleClass( 'ui-filter-hidequeue' , !childItems );

							// New bucket!
							childItems = false;

						} else if ( itemtext.toLowerCase().indexOf( val ) === -1) {

							//mark to be hidden
							item.toggleClass( 'ui-filter-hidequeue' , true );
						} else {

							// There's a shown item in the bucket
							childItems = true;
						}
					}

					// show items, not marked to be hidden
					listItems
						.filter( ':not(.ui-filter-hidequeue)' )
						.toggleClass('ui-screen-hidden',false);

					// hide items, marked to be hidden
					listItems
						.filter( '.ui-filter-hidequeue' )
						.toggleClass('ui-screen-hidden',true)
						.toggleClass( 'ui-filter-hidequeue' , false );

				}else{

					//filtervalue is empty => show all
					listItems.toggleClass('ui-screen-hidden',false);
				}
			})
			.appendTo( wrapper )
			.textinput();

	if ($( this ).jqmData( "inset" ) ) {
		wrapper.addClass( "ui-listview-filter-inset" );
	}

	wrapper.insertBefore( list );
});

})( jQuery );

</script>

<body>

<div data-role="page">

	<div data-role="header">
		<h1>My Title</h1>
	</div><!-- /header -->

	<div data-role="content">	
		<p>Hello world</p>
		<ul data-role="listview" add data-filter="true">
			
	<li><a href="acura.html">Acura</a></li>
	<li><a href="audi.html">Audi</a></li>
	<li><a href="bmw.html">BMW</a></li>
</ul>
	</div><!-- /content -->

</div><!-- /page -->	

</body>
</html>

Open in new window

0
Comment
Question by:danz67
  • 2
3 Comments
 
LVL 18

Expert Comment

by:nap0leon
ID: 38778605
Try removing this line.
wrapper.insertBefore( list );

Open in new window

0
 

Accepted Solution

by:
danz67 earned 0 total points
ID: 38778948
I resolved,
just add id name.

(function($, custom-format-listview )

Open in new window


<div data-role="content">
	    <ul id="custom-format-listview" data-role="listview" data-filter="true" data-filter-placeholder="Cerca...">

Open in new window


this is pretty good, but I do not think it is the best solution. Is there a way to show other records after a query with limit?
0
 

Author Closing Comment

by:danz67
ID: 38821876
Best Solution find in the web
0

Featured Post

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

Suggested Solutions

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

895 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

15 Experts available now in Live!

Get 1:1 Help Now