init() public method

public init ( )
示例#1
1
 public function init()
 {
     if (empty($this->trueLabel)) {
         $this->trueLabel = Yii::t('yii', 'Yes');
     }
     if (empty($this->falseLabel)) {
         $this->falseLabel = Yii::t('yii', 'No');
     }
     parent::init();
 }
示例#2
0
    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        if (empty($this->tableName)) {
            throw new InvalidConfigException('The "tableName" property must be set.');
        }
        $view = Yii::$app->getView();
        Html::addCssClass($this->buttonOptions, $this->buttonClass);
        if (!$this->tableColumn) {
            $this->tableColumn = $this->attribute;
        }
        $id = Yii::$app->request->post('id');
        $tableColumn = Yii::$app->request->post('tableColumn');
        if (Yii::$app->request->isPost && $tableColumn === $this->tableColumn) {
            /* @var \yii\db\Connection $db */
            $db = Yii::$app->get($this->db);
            $json['r'] = false;
            $query = new Query();
            $row = $query->from($this->tableName)->select(['a' => $this->tableColumn])->where(['id' => $id])->limit(1)->one($db);
            if ($row) {
                if ($row['a']) {
                    $active = false;
                    $result = $this->falseIcon;
                } else {
                    $active = true;
                    $result = $this->trueIcon;
                }
                $update = [$this->tableColumn => $active];
                if (is_array($this->update)) {
                    foreach ($this->update as $column => $value) {
                        if (is_integer($column)) {
                            $update[$value] = new Expression('NOW()');
                        } else {
                            if ($value instanceof \Closure) {
                                $update[$column] = $value($column, $this);
                            } else {
                                $update[$column] = $value;
                            }
                        }
                    }
                }
                $db->createCommand()->update($this->tableName, $update, 'id=:id', [':id' => $id])->execute();
                $json['r'] = true;
                $json['res'] = $result;
            }
            $response = Yii::$app->getResponse();
            $response->clearOutputBuffers();
            $response->setStatusCode(200);
            $response->format = Response::FORMAT_JSON;
            $response->data = $json;
            $response->send();
            Yii::$app->end();
        }
        $view->registerJs('
            $(document).on("click", ".' . $this->buttonClass . '", function(){
                var $btn = $(this);

                $btn.html("' . $this->loading . '");
                $btn.attr("disabled", true);

                $.ajax({
                    url: "' . Url::current() . '",
                    type: "POST",
                    dataType: "json",
                    data: {id: $btn.attr("data-id"), tableColumn: $btn.attr("data-column")},
                }).done(function(d){
                    if(d.r){
                        $btn.html(d.res);
                    }
                }).always(function(jqXHR, textStatus){
                    $btn.attr("disabled", false);
                });

            });
        ');
    }