Skip to content

Query FeatureLayer

Features can be queried based on attribute, location, time. Additionally, you can choose which fields to return, control the sorting order, or calculate statistics.

This sample shows how to use a FeatureLayer to retrieve all features from a feature service when the dataset is too large to load entirely in the client. The layer's queryFeatures() method with pagination retrieves features that exceed the layer’s maxRecordCount limit. A specific number of records are fetched from a designated starting position. Fetched features are displayed in a Calcite List, showing each county's name and median household income. The county features are also highlighted on the map, and clicking a list item zooms to the feature and opens its popup. In this sample, the FeatureLayer shows 2017 median household income data by U.S. counties.

How it works

When the application starts, the total number of features available in the service is retrieved by using the layer's queryFeatureCount() method. This value is then used to determine the total number of pages displayed in the Calcite Pagination component.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
      // Get number of all features from the service and use the count
      // to set the number of pages in the calcite-pagination component
      const featureCount = await featureLayer.queryFeatureCount();
      document.getElementById("tablePager").setAttribute("total-items", featureCount);

The sample then queries the twenty U.S. counties with the highest median household incomes using the queryFeatures() method. Pagination is achieved by configuring the Query object’s start and num properties:

  • The start property specifies the zero-based index indicating where to begin retrieving features.
  • The num property defines the number of features to fetch in each query.

This approach ensures efficient data retrieval even when the dataset exceeds the layer’s maximum record count limit.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
      // Fetches 20 features from a specified start location
      // Called when the application loads first then it is called whenever
      // user changes the page number on the calcite-pagination component
      async function queryPage(page) {
        // Create the query object honoring layer settings
        // sets returnGeometry=true and outFields to "*"
        const query = featureLayer.createQuery();
        // Set query parameters for pagination and sorting
        query.start = page;
        query.num = 20;
        query.orderByFields = ["MEDHINC_CY DESC"];

        const queryResult = await featureLayer.queryFeatures(query);
        features = queryResult.features;
        convertFeatureSetToRows(features, query);
      }

Information about the retrieved counties will be displayed in a Calcite List component located on the right side of the application. The counties in this list will also be visually highlighted on the map.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
      // Called once next twenty counties are fetched
      // and sets up the list items in the calcite-list component
      function convertFeatureSetToRows(features, query) {
        // Use a local variable for the list node
        const incomeList = document.getElementById("incomeList");
        incomeList.innerHTML = "";

        // Use a DocumentFragment for efficient DOM updates
        const fragment = document.createDocumentFragment();

        features.forEach((result, index) => {
          const { NAME, MEDHINC_CY } = result.attributes;
          const item = document.createElement("calcite-list-item");
          item.setAttribute("label", NAME);
          item.setAttribute("value", index);
          item.setAttribute("description", `median income: ${MEDHINC_CY}`);
          item.addEventListener("click", onCountyClickHandler);
          fragment.appendChild(item);
        });

        incomeList.appendChild(fragment);

        // Safely remove previous highlight and add new one
        try {
          highlight?.remove();
          highlight = layerView.highlight(features, { name: "temporary" });
        } catch (error) {
          console.error("Highlight error:", error);
        }
      }

Users can navigate between pages using the pagination controls below the list. The current page number is used to query the corresponding counties sorted by median household income. The following function is executed whenever the Calcite Pagination component’s page number changes.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
      // Set query.page and fetch the features from the service corresponding
      // to the page number when user clicks a page number on calcite-pagination
      document.getElementById("tablePager").addEventListener(
        "calcitePaginationChange", async (event) => {
          // Calculate zero-based page index
          const page = event.target.startItem - 1;
          try {
            await queryPage(page);

            // Optionally reset view only if needed
            viewElement.zoom = 3;
            viewElement.center = [-98, 38];

            // Close popup if open
            if (popup?.visible) {
              viewElement.closePopup();
            }
          } catch (error) {
            console.error("Error updating page:", error);
          }
        }
      );

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.