Skip to content

Commit

Permalink
Select a column for the key / value of the selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Mello authored and Mello committed Aug 9, 2019
1 parent 329cbc8 commit 5a499d8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
5 changes: 4 additions & 1 deletion docs/documentation/docs/controls/ListItemPicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ListItemPicker } from '@pnp/spfx-controls-react/lib/listItemPicker';
```TypeScript
<ListItemPicker listId='da8daf15-d84f-4ab1-9800-7568f82fed3f'
columnInternalName='Title'
valueColumnInternalName='Id'
itemLimit={2}
onSelectedItem={this.onSelectedItem}
context={this.props.context} />
Expand All @@ -33,7 +34,8 @@ import { ListItemPicker } from '@pnp/spfx-controls-react/lib/listItemPicker';
```TypeScript
private onSelectedItem(data: { key: string; name: string }[]) {
for (const item of data) {
console.log(`Item value: ${item.name}`);
console.log(`Item value: ${item.key}`);
console.log(`Item text: ${item.name}`);
}
}
```
Expand All @@ -45,6 +47,7 @@ The `ListItemPicker` control can be configured with the following properties:
| Property | Type | Required | Description |
| ---- | ---- | ---- | ---- |
| columnInternalName | string | yes | InternalName of column to search and get values. |
| valueColumnInternalName | string | no | InternalName of column to use as the value or key for the selection. Must be a column with unique values. |
| context | WebPartContext \| ApplicationCustomizerContext | yes | SPFx web part or extention context |
| listId | string | yes | Guid of the list. |
| itemLimit | number | yes | Number of items which can be selected |
Expand Down
1 change: 1 addition & 0 deletions src/controls/listItemPicker/IListItemPickerProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ApplicationCustomizerContext } from "@microsoft/sp-application-base";

export interface IListItemPickerProps {
columnInternalName: string;
valueColumnInternalName?: string;
context: WebPartContext | ApplicationCustomizerContext;
listId: string;
itemLimit: number;
Expand Down
3 changes: 2 additions & 1 deletion src/controls/listItemPicker/ListItemPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@ export class ListItemPicker extends React.Component<IListItemPickerProps, IListI
private loadListItems = async (filterText: string): Promise<{ key: string; name: string }[]> => {
let { listId, columnInternalName, webUrl } = this.props;
let arrayItems: { key: string; name: string }[] = [];
let valueColumn: string = columnInternalName || 'Id';

try {
let listItems = await this._spservice.getListItems(filterText, listId, columnInternalName, webUrl);
// Check if the list had items
if (listItems.length > 0) {
for (const item of listItems) {
arrayItems.push({ key: item.Id, name: item[columnInternalName] });
arrayItems.push({ key: item[valueColumn], name: item[columnInternalName] });
}
}
return arrayItems;
Expand Down
6 changes: 3 additions & 3 deletions src/services/SPService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ export default class SPService implements ISPService {
/**
* Get List Items
*/
public async getListItems(filterText: string, listId: string, internalColumnName: string, webUrl?: string): Promise<any[]> {
public async getListItems(filterText: string, listId: string, internalColumnName: string, valueInternalColumnName?: string, webUrl?: string): Promise<any[]> {
let returnItems: any[];

try {
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
const apiUrl = `${webAbsoluteUrl}/_api/web/lists('${listId}')/items?$select=Id,${internalColumnName}&$filter=startswith(${internalColumnName},'${filterText}')`;
const apiUrl = `${webAbsoluteUrl}/_api/web/lists('${listId}')/items?$select=${valueInternalColumnName || 'Id'},${internalColumnName}&$filter=startswith(${internalColumnName},'${filterText}')`;
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
if (data.ok) {
const results = await data.json();
Expand Down

0 comments on commit 5a499d8

Please sign in to comment.