firesphere/rangefield

SilverStripe 的范围选择器

安装次数: 12,493

依赖项: 1

建议者: 0

安全: 0

类型:silverstripe-vendormodule

1.2.0 2023-11-29 09:21 UTC

This package is auto-updated.

Last update: 2024-08-29 10:54:51 UTC


README

CircleCI codecov Scrutinizer Code Quality

Gitstore

范围字段

在 SilverStripe 的表单中玩转范围。

Demo animation

安装

composer require firesphere/rangefield

使用

use Firesphere\RangeField\RangeField;
use SilverStripe\CMS\Model\SiteTree;

class Page extends SiteTree
{
   private static $db = [
      'RangeValues' => 'Varchar',
   ];

   public function getCMSFields()
   {
      $fields = parent::getCMSFields();

      $fields->addFieldToTab(
         'Root.Main',
         RangeField::create('RangeValues', 'Range of values')
      );

      return $fields;
   }
}

更复杂的配置(包括范围选项)

RangeField::create(
   $field = 'RangeValues',
   $title = 'Range of values',
   $start = 0,
   $min = 0,
   $max = 100,
   $range = [
      'min' => 0, // this will override $min
      '25%' => 25,
      '50%' => 50,
      '75%' => 75,
      'max' => 100, // this will override $max
   ],
   $value = 33
)

该字段使用 noUiSlider,因此您可以在此处找到更多信息

当您更改字段上的处理程序数量时,值将被重置,因为无法确定哪个默认值是原始值。

选择将被存储为逗号分隔的值集,例如 33.00,50.00。您需要应用自己的逻辑来在前端使用这些值。

可以通过 setUnit()setDecimalPlace()setFormat() 方法对值应用有限的格式化。

选项

选项 默认值 方法 使用
吸附 false setSnap() 将吸附设置为点
数据 [] setData() 覆盖生成数据
最大值 0 setMax() 更改最大值⍟
最小值 100 setMin() 更改最小值⍟
密度 5 setDensity() 设置pip的密度
范围 [] setRange() 设置范围值⍟
起始值 [0] setStart() 设置默认值⍟
步长 null setStep() 设置步长大小
显示pip true setShowPips() 显示或隐藏pip
吸附 false setSnap() 吸附到范围值而不是流体
单位 '' setUnit() 将单位附加到输出值
小数位数 2 setDecimalPlace() 格式化输出值的小数位数
格式 '', 2 setFormat() 设置单位和十进制位数的快捷方式

⍟ 这些项可以在构造/创建方法中设置

在前端使用字段(= 公共站点)

如果您需要不仅在使用管理部分,还需要在前端使用此字段,您需要在您的 PageController 类中定义一些CSS和JavaScript要求

app/src/PageController.php

class PageController extends ContentController
{
	
	protected function init()
	{
		parent::init();
		// You can include any CSS or JS required by your project here.
		// See: https://docs.silverstripe.org/en/developer_guides/templates/requirements/
		
		// RangeField
		Requirements::css('firesphere/rangefield:client/dist/main.css');
		Requirements::javascript('firesphere/rangefield:client/dist/main.js');
		
	}
}      

如果您只在使用管理部分使用此模块,您不需要这样做,因为那些要求会自动为管理部分定义。

已知问题

  • 多个起始值似乎无法正常工作
  • 发布后滑块从最小值开始,而不是当前值

许可证

此模块在 BSD 3 条款许可证下发布,尽管这些许可证不在实际类中,但许可证仍然适用

https://open-source.org.cn/licenses/BSD-3-Clause

Copyright (c) 2012-NOW(), Simon "Firesphere" Erkelens

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

您是否阅读了整个README?您真棒!

下面的图片是一头牛,专门为您。

               /( ,,,,, )\
              _\,;;;;;;;,/_
           .-"; ;;;;;;;;; ;"-.
           '.__/`_ / \ _`\__.'
              | (')| |(') |
              | .--' '--. |
              |/ o     o \|
              |           |
             / \ _..=.._ / \
            /:. '._____.'   \
           ;::'    / \      .;
           |     _|_ _|_   ::|
         .-|     '==o=='    '|-.
        /  |  . /       \    |  \
        |  | ::|         |   | .|
        |  (  ')         (.  )::|
        |: |   |;  U U  ;|:: | `|
        |' |   | \ U U / |'  |  |
        ##V|   |_/`"""`\_|   |V##
           ##V##         ##V##