Solved

JQuery Mobile - Listview slow

Posted on 2013-01-15
3
2,542 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: blockā€¦
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

744 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