Solved

JQuery Mobile - Listview slow

Posted on 2013-01-15
3
2,577 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
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)

749 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