Laravel:具有步长的SelectRange [英] Laravel: SelectRange with Step size

查看:78
本文介绍了Laravel:具有步长的SelectRange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Laravel中创建一个具有选择范围属性的字段

I would like to create a field in Laravel with a select range attribute

 {{ Form::selectRange('number', 1, 1500000) }}

我希望范围在1到1500000之间,增量步长为50000

I would like the range to be between 1 and 1500000 with an increment step size of 50000

当前,当我创建它时,它只是创建一个增量为1的超长选择字段.

Currently when I create this, it just creates a super long select field with increments of 1

推荐答案

为此我创建了一个Form宏,基本上扩展了现有的selectRange宏:

I created a Form macro for this, basically extending the existing selectRange macro:

class FormBuilder extends FB
{
    public function selectRangeWithInterval($name, $start, $end, $interval, $default = null, $attributes = [])
    {
        if ($interval == 0) {
            return $this->selectRange($name, $start, $end, $default, $attributes);
        }
        $items = [];
        $startValue = $start;
        $endValue = $end;
        if ($interval < 0) {
            $interval *= -1;
        }
        if ($start > $end) {
            if ($interval > 0) {
                $interval *= -1;
            }
            $startValue = $end;
            $endValue = $start;
        }
        for ($i=$startValue; $i<$endValue; $i+=$interval) {
            $items[$i . ""] = $i;
        }
        $items[$endValue] = $endValue;
        if (!in_array($default, $items)) {
            $items[$default] = $default;
        }

        return $this->select($name, $items, $default, $attributes);
    }
}

然后可以在您的视图中使用以下内容:

This can then be used in your view something like this:

{{ Form::selectRangeWithInterval('weightOfSackOfFeathers', 0, 7500, 150, null, ['class' => 'form-control input-xs']) }}

这篇关于Laravel:具有步长的SelectRange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆