Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/leaflet-search.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@
}

this._curReq = this._retrieveData.call(this, inputText, function (data) {
self._recordsCache = self._formatData(self, data)
self._recordsCache = self._formatData.call(self, data)

// TODO refact!
if (self.options.sourceData) { records = self._filterData(self._input.value, self._recordsCache) } else { records = self._recordsCache }
Expand Down
71 changes: 71 additions & 0 deletions examples/simple-sourceData-bug.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Leaflet.Control.Search sourceData array bug</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<link rel="stylesheet" href="../src/leaflet-search.css" />
<link rel="stylesheet" href="style.css" />
</head>

<body>

<h3><a href="../"><big>◄</big> Leaflet.Control.Search</a></h3>

<h4>
Bug reproduction: <em>sourceData() returning an array</em>
</h4>

<p>
This example demonstrates a bug where <code>sourceData</code> returns an array
of objects and the default <code>formatData</code> implementation produces
incorrect results.
</p>

<div id="map"></div>

<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="../dist/leaflet-search.src.js"></script>
<script>

var map = new L.Map('map', {
zoom: 5,
center: [48.8566, 2.3522]
});

map.addLayer(new L.TileLayer(
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
));

var searchControl = new L.Control.Search({
position: 'topright',
marker: false,

// IMPORTANT:
// sourceData returns an ARRAY of objects
// (this triggers the bug in current versions)
sourceData: function (text, callResponse) {
callResponse([
{ title: 'Paris', la: 48.8566, lo: 2.3522 },
{ title: 'London', la: 51.5074, lo: -0.1278 },
{ title: 'Berlin', la: 52.5200, lo: 13.4050 }
]);
},

// Use default formatter intentionally
propertyName: 'title',
propertyLoc: ['la', 'lo']
});

map.addControl(searchControl);

</script>

<div id="copy">
<a href="https://opengeo.tech/">Website</a> &bull;
<a rel="author" href="https://opengeo.tech/stefano-cudini/">Stefano Cudini</a>
</div>

</body>
</html>
2 changes: 1 addition & 1 deletion src/leaflet-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@
}

this._curReq = this._retrieveData.call(this, inputText, function (data) {
self._recordsCache = self._formatData(self, data)
self._recordsCache = self._formatData.call(self, data)

// TODO refact!
if (self.options.sourceData) { records = self._filterData(self._input.value, self._recordsCache) } else { records = self._recordsCache }
Expand Down