yii\data\Sort::createSortParam PHP Method

createSortParam() public method

The newly created sort variable can be used to create a URL that will lead to sorting by the specified attribute.
public createSortParam ( string $attribute ) : string
$attribute string the attribute name
return string the value of the sort variable
    public function createSortParam($attribute)
    {
        if (!isset($this->attributes[$attribute])) {
            throw new InvalidConfigException("Unknown attribute: {$attribute}");
        }
        $definition = $this->attributes[$attribute];
        $directions = $this->getAttributeOrders();
        if (isset($directions[$attribute])) {
            $direction = $directions[$attribute] === SORT_DESC ? SORT_ASC : SORT_DESC;
            unset($directions[$attribute]);
        } else {
            $direction = isset($definition['default']) ? $definition['default'] : SORT_ASC;
        }
        if ($this->enableMultiSort) {
            $directions = array_merge([$attribute => $direction], $directions);
        } else {
            $directions = [$attribute => $direction];
        }
        $sorts = [];
        foreach ($directions as $attribute => $direction) {
            $sorts[] = $direction === SORT_DESC ? '-' . $attribute : $attribute;
        }
        return implode($this->separator, $sorts);
    }

Usage Example

示例#1
0
文件: Html.php 项目: kalibao/magesko
 /**
  * Generates a hyperlink that can be clicked to cause sorting.
  * @param Sort $sort the current Sort instance
  * @param string $attribute the attribute name
  * @param array $options additional HTML attributes for the hyperlink tag.
  * There is one special attribute `label` which will be used as the label of the hyperlink.
  * If this is not set, the label defined in [[attributes]] will be used.
  * If no label is defined, [[\yii\helpers\Inflector::camel2words()]] will be called to get a label.
  * Note that it will not be HTML-encoded.
  * @return string
  */
 public static function sortLink(Sort $sort, $attribute, $options = [])
 {
     if (($direction = $sort->getAttributeOrder($attribute)) !== null) {
         $class = $direction === SORT_DESC ? 'current-sort desc' : 'current-sort asc';
         $icon = $direction === SORT_DESC ? 'glyphicon-sort-by-alphabet-alt' : 'glyphicon-sort-by-alphabet';
         if (isset($options['class'])) {
             $options['class'] .= ' ' . $class;
         } else {
             $options['class'] = $class;
         }
     } else {
         $icon = 'glyphicon-sort-by-alphabet';
         if (isset($options['class'])) {
             $options['class'] .= ' asc';
         } else {
             $options['class'] = 'asc';
         }
     }
     $url = $sort->createUrl($attribute);
     $options['data-sort'] = $sort->createSortParam($attribute);
     if (isset($options['label'])) {
         $label = $options['label'];
         unset($options['label']);
     } else {
         if (isset($sort->attributes[$attribute]['label'])) {
             $label = $sort->attributes[$attribute]['label'];
         } else {
             $label = Inflector::camel2words($attribute);
         }
     }
     $label .= ' <span class="glyphicon ' . $icon . '"></span>';
     return Html::a($label, $url, $options);
 }
All Usage Examples Of yii\data\Sort::createSortParam