protected function getClientOptions()
    {
        $counter = count($this->allModels) ? max(array_keys($this->allModels)) + 1 : 0;
        $clientOptions = $this->clientOptions;

        $clientOptions['counter'] = $counter;
        if (empty($clientOptions['itemSelector'])) {
            throw new InvalidConfigException('Value of "clientOptions[\'itemSelector\']" must be specified.');
        }
        if ($this->form instanceof ActiveForm) {
            $clientOptions['formSelector'] = '#' . $this->form->options['id'];
            $oldAttrs = $this->form->attributes;
            $this->form->attributes = [];
        }

        // template and js
        $view = $this->getView();
        $oldJs = $view->js;
        $view->js = [];

        $template = $this->renderItem($this->model, "_dkey{$this->level}_", "_dindex{$this->level}_");
        if (isset($oldAttrs)) {
            $clientOptions['validations'] = $this->form->attributes;
            $this->form->attributes = $oldAttrs;
        }
        $js = [];
        foreach ($view->js as $pieces) {
            $js[] = implode("\n", $pieces);
        }
        if (count($js)) {
            $clientOptions['templateJs'] = implode("\n", $js);
        }
        $view->js = $oldJs;
        // ***

        $clientOptions['template'] = $template;
        $clientOptions['replaces'] = [
            'key' => new JsExpression("/_dkey{$this->level}_/g"),
            'index' => new JsExpression("/_dindex{$this->level}_/g"),
        ];
        return $clientOptions;
    }