描述
本例展示了如何使用查找任务搜索数据里的记录,然后在HTML表格中显示结果。
尽管FindTask不需要显示一个地图,但是构造函数需要一个ArcGIS Server地图服务的URL。 本例使用ESRI sample server上的ESRI_StatesCitiesRivers_USA地图服务。 FindParameters限定了仅仅搜索州图层(索引2)的STATE_NAME和STATE_FIPS字段。
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 5 <meta http-equiv="X-UA-Compatible" content="IE=7" /> 6 <title>Simple Find</title> 7 <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.5/js/dojo/dijit/themes/tundra/tundra.css"> 8 <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.5"></script> 9 <script type="text/javascript"> 10 dojo.require("esri.tasks.find"); 11 12 var find, params; 13 function init() { 14 find = new esri.tasks.FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/"); 15 params = new esri.tasks.FindParameters(); 16 params.layerIds = [2]; 17 params.searchFields = ["STATE_NAME","STATE_FIPS"]; 18 } 19 20 function doFind() { 21 params.searchText = dojo.byId("searchText").value; 22 find.execute(params, showResults); 23 } 24 25 function showResults(results) { 26 var result, attribs; 27 var s = ["<table border="1"><thead><tr style="background-color:#ccc;"><td>State Name</td><td>FIPS</td><td>Population (1990)</td><td>Population (1999)</td></tr></thead><tbody id="states">"]; 28 for (var i=0, il=results.length; i<il; i++) { 29 result = results[i]; 30 attribs = result.feature.attributes; 31 s.push("<tr><td>" + attribs.STATE_NAME + "</td><td>" + attribs.STATE_FIPS + "</td><td>" + attribs.POP1990 + "</td><td>" + attribs.POP1999 + "</td></tr>"); 32 } 33 s.push("</tbody></table>"); 34 dojo.byId("tbl").innerHTML = s.join(""); 35 } 36 37 dojo.addOnLoad(init); 38 </script> 39 40 </head> 41 <body class="tundra"> 42 Find by State Name or State FIPS: <input type="text" id="searchText" size="40" value="Kansas" /> 43 <input type="button" value="Find" onclick="doFind()" /> 44 45 <div id="tbl"></div> 46 47 </body> 48 </html> 49 50 51 52 53 54