Skip to content

Commit 5a499d8

Browse files
MelloMello
authored andcommitted
Select a column for the key / value of the selection
1 parent 329cbc8 commit 5a499d8

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

docs/documentation/docs/controls/ListItemPicker.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { ListItemPicker } from '@pnp/spfx-controls-react/lib/listItemPicker';
2323
```TypeScript
2424
<ListItemPicker listId='da8daf15-d84f-4ab1-9800-7568f82fed3f'
2525
columnInternalName='Title'
26+
valueColumnInternalName='Id'
2627
itemLimit={2}
2728
onSelectedItem={this.onSelectedItem}
2829
context={this.props.context} />
@@ -33,7 +34,8 @@ import { ListItemPicker } from '@pnp/spfx-controls-react/lib/listItemPicker';
3334
```TypeScript
3435
private onSelectedItem(data: { key: string; name: string }[]) {
3536
for (const item of data) {
36-
console.log(`Item value: ${item.name}`);
37+
console.log(`Item value: ${item.key}`);
38+
console.log(`Item text: ${item.name}`);
3739
}
3840
}
3941
```
@@ -45,6 +47,7 @@ The `ListItemPicker` control can be configured with the following properties:
4547
| Property | Type | Required | Description |
4648
| ---- | ---- | ---- | ---- |
4749
| columnInternalName | string | yes | InternalName of column to search and get values. |
50+
| valueColumnInternalName | string | no | InternalName of column to use as the value or key for the selection. Must be a column with unique values. |
4851
| context | WebPartContext \| ApplicationCustomizerContext | yes | SPFx web part or extention context |
4952
| listId | string | yes | Guid of the list. |
5053
| itemLimit | number | yes | Number of items which can be selected |

src/controls/listItemPicker/IListItemPickerProps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ApplicationCustomizerContext } from "@microsoft/sp-application-base";
33

44
export interface IListItemPickerProps {
55
columnInternalName: string;
6+
valueColumnInternalName?: string;
67
context: WebPartContext | ApplicationCustomizerContext;
78
listId: string;
89
itemLimit: number;

src/controls/listItemPicker/ListItemPicker.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,14 @@ export class ListItemPicker extends React.Component<IListItemPickerProps, IListI
114114
private loadListItems = async (filterText: string): Promise<{ key: string; name: string }[]> => {
115115
let { listId, columnInternalName, webUrl } = this.props;
116116
let arrayItems: { key: string; name: string }[] = [];
117+
let valueColumn: string = columnInternalName || 'Id';
117118

118119
try {
119120
let listItems = await this._spservice.getListItems(filterText, listId, columnInternalName, webUrl);
120121
// Check if the list had items
121122
if (listItems.length > 0) {
122123
for (const item of listItems) {
123-
arrayItems.push({ key: item.Id, name: item[columnInternalName] });
124+
arrayItems.push({ key: item[valueColumn], name: item[columnInternalName] });
124125
}
125126
}
126127
return arrayItems;

src/services/SPService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ export default class SPService implements ISPService {
4747
/**
4848
* Get List Items
4949
*/
50-
public async getListItems(filterText: string, listId: string, internalColumnName: string, webUrl?: string): Promise<any[]> {
50+
public async getListItems(filterText: string, listId: string, internalColumnName: string, valueInternalColumnName?: string, webUrl?: string): Promise<any[]> {
5151
let returnItems: any[];
52-
52+
5353
try {
5454
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
55-
const apiUrl = `${webAbsoluteUrl}/_api/web/lists('${listId}')/items?$select=Id,${internalColumnName}&$filter=startswith(${internalColumnName},'${filterText}')`;
55+
const apiUrl = `${webAbsoluteUrl}/_api/web/lists('${listId}')/items?$select=${valueInternalColumnName || 'Id'},${internalColumnName}&$filter=startswith(${internalColumnName},'${filterText}')`;
5656
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
5757
if (data.ok) {
5858
const results = await data.json();

0 commit comments

Comments
 (0)