0

I've used Dock of PrimeReact.

<Dock model={options.buttons} position="right"/>

According to its document, For model we should pass its dockItems.

So I defined options like this:

I have an array of objects. Inside it I have buttons, I want to check if the xValue doesn't have value then I show an error message.

const options = [
        {
            xValue: '',
            uperMessage: 'Select a start date',
            lowerMessage: '',
            setMessage: function (time) {
                this.lowerMessage = `Selected start date: ${dateHelper.formatDate(time)}`;
            },
            buttons: [
                {
                    label: 'Next',
                    icon: () => <Button label="Next" className="p-button-success" />,
                    command: function () {
                        if (!this.xValue) {
                            toast.current.show({ severity: 'error', summary: 'Error', detail: 'Select a  start date!', life: 3000 });
                        } else {
                            setCurrentStep(2);
                        }

                    }
                },
                {
                    label: 'Cancel',
                    icon: () => <Button label="Cancel" className="p-button-secondary" />,
                    command: () => {
                        cancel()
                    }
                }
            ]
        },
        {
            xValue: '',
            uperMessage: 'Select a end date',
            lowerMessage: '',
            setMessage: function (time) {
                this.lowerMessage = `Selected end date: ${dateHelper.formatDate(time)}`;
            },
            buttons: [
                {
                    label: 'Next',
                    icon: () => <Button label="Next" className="p-button-success" />,
                    command: function (xValue) {
                        if (!this.xValue) {
                            toast.current.show({ severity: 'error', summary: 'Error', detail: 'Select a Divergence end date!', life: 3000 });
                        } else {
                            setCurrentStep(3);
                        }
                    }
                },
                {
                    label: 'Cancel',
                    icon: () => <Button label="Cancel" className="p-button-secondary" />,
                    command: () => {
                        cancel()
                    }
                }]
        }];


How can I access the xValue in the command function?

If I use this, it gets the items of the current scope. How can I access the items of the parent?

Any help would be greatly appreciated.

Fateme Mirjalili
  • 762
  • 7
  • 16

2 Answers2

0

You can change command function and map on the options items, also buttons items. Finally you can find the current item and get the xValue :

command: function () {
         let $this = this;
         options.map((option,oindex)=>{
             option.buttons.map(button,binex)=>{
                 if(button == $this)
                    if (!option.xValue) {
                           toast.current.show({ severity: 'error', summary: 'Error', detail: 'Select a  start date!', life: 3000 });
                    } else {
                        setCurrentStep(2);
                    }
            })
        })
Majid M.
  • 4,467
  • 1
  • 11
  • 29
-1

you could use the array name "options" to access xValue like that if(!options[item_index]['xValue']){ .... } for example:- options[0]["xValue"] => // return ''