} } ?> </tr> </thead> <?php if ($listAttributes !== []) { ?> <?php if (!empty($models)) { ?> <?php foreach ($models as $model) { ?> <?php AttributeHandle::$model = $model; ?> <tr> <?php foreach ($listAttributes as $attribute => $configs) { ?> <td> <?php /** * 程序这里做了以下处理: * * 1. 检测是否有自定义的属性事件处理`handle`,如果有且为匿名函数则直接调用,否则调用相应的处理事件处理 * 2. 检测当前模型是否含有此属性,若有则输出,否则抛出异常 */ if (isset($configs['handle'])) { if ($configs['handle'] instanceof \Closure) {
/** * 获取列表数据HTML */ public function getDataHtml() { $html = ''; if (empty($this->listAttributes)) { return $html; } if (empty($this->models)) { $html .= Html::beginTag($this->dataRowTag, ['style' => 'text-align:center;']); $html .= Html::beginTag($this->dataColTag, ['colspan' => count($this->listAttributes)]); $html .= '还没有任何数据...'; $html .= Html::endTag($this->dataColTag); $html .= Html::endTag($this->dataRowTag); return $html; } $model = $this->model; $modelClass = $model::ClassName(); AttributeHandle::$pks = $modelClass::primaryKey(); foreach ($this->models as $model) { $html .= Html::beginTag($this->dataRowTag); AttributeHandle::$model = $model; foreach ($this->listAttributes as $attribute => $configs) { $html .= Html::beginTag($this->dataColTag); $html .= $this->parseAttribute($attribute, $model, $configs); $html .= Html::endTag($this->dataColTag); } $html .= Html::endTag($this->dataRowTag); } return $html; }
/** * 返回属性的值,其中在解析一个值时经历以下步骤: * * 1. 判断是否是标量,如果是直接返回标量值 * 2. 判断是否是数组,则其下进行下面检测: * - 如果存在`handle`索引(属性处理方法也存在),则使用[[AttributeHandle]]进行处理 * - 是否是`callable`类型,如果是则直接调用返回 * - 如果不符合前两者,则直接返回数组 * 3. 判断是否是匿名函数,如果是则直接调用返回 * * @param Event $event the event that triggers the current attribute updating. * @return mixed the attribute value */ protected function getValue($attribute, $value, $event) { if (is_scalar($value)) { return $value; } elseif (is_array($value)) { $handleClassName = 'common\\helpers\\AttributeHandle'; $handleEventName = $value['handle'] . 'Event'; if (isset($value['handle']) && method_exists($handleClassName, $handleEventName)) { $args = isset($value['args']) ? $value['args'] : []; AttributeHandle::$model = $this->owner; $args = [$attribute, $args]; return call_user_func_array([$handleClassName, $handleEventName], $args); } elseif (is_callable($value)) { return call_user_func($value); } elseif (isset($value[0]) && is_callable($value[0])) { $args = isset($value[1]) ? $value[1] : []; return call_user_func_array($value[0], $args); } return $value; } elseif ($value instanceof Closure) { return call_user_func($value, $event); } }