创建元素的组合

问题描述 投票:1回答:1

我想创建产品选项系统。我的表单看起来像这样:

表单的输入是标签输入。第一个输入是选项名称。当您添加任何选项名称时,表单中添加了新标记输入。

我的问题:

我无法在控制器中创建组合,因为输入名称的数量将是随机的。

现行代码:

我找到了这个代码,但我不能为我的系统定制它

首先输入代码id:

当我发布数据去控制器发布另一个刀片。我在我的页面中包含这个

在控制器中

 public function sendd(Request $request){
    $a = $request['data'];

    return view ('system.modules.variations', compact('a'));
}

在另一个刀片

@if(!empty($a))
@foreach($a as $b)
    <label class="sr-only" for="{{$b}}"></label>
    <input type="text" class="form-control" placeholder="{{$b}}" name="{{$b}}" data-role="tagsinput" id="{{$b}}"/></br>
@endforeach

@endif

如果将创建新的标签输入,我可以添加变化,例如:红色,蓝色

在我发送表格到另一个控制器后

 $(".btn-submit").click(function(e){

        e.preventDefault();

        $.ajax({

            type:'POST',

            url:'/mauuu',

            data: $("#addpro").serialize(),

            success:function(){

            }
        });

    });

我发送这样的数据,但我无法创建组合:

哦,我添加了我的所有代码

这是我的刀片形式

<form class="floating-labels m-t-40" id="addpro">
                                {{csrf_field()}}
                                <div class="form-group m-b-40">
                                    <input type="text" class="form-control" name="title" id="title" required>
                                    <span class="bar"></span>
                                    <label for="title">Title</label>
                                </div>
                                <div class="form-group m-b-5">
                                    <textarea class="form-control" rows="4"  name="description" id="description" required></textarea>
                                    <span class="bar"></span>
                                    <label for="description">Description</label>
                                </div>
                                <div class="form-group m-b-40">
                                    <textarea class="form-control" rows="4" name="bullet" id="bullet" required></textarea>
                                    <span class="bar"></span>
                                    <label for="bullet">Box</label>
                                </div>
                                <div class="form-group m-b-40">
                                    <a onclick="myFunction()" class="btn btn-outline-info btn-small" id="btn-x">Add variations</a>
                                </div>
                                <div id="options" style="display:none;">
                                    <div class="row">
                                        <div class="col-md-3">
                                            <div class="form-group m-b-40">
                                                <input type="text" class="form-control" name="sku" id="sku" required>
                                                <span class="bar"></span>
                                                <label for="sku">Sku</label>
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="form-group m-b-40">
                                                <input type="text" class="form-control" name="price" id="price" required>
                                                <span class="bar"></span>
                                                <label for="price">Price</label>
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="form-group m-b-40">
                                                <input type="text" class="form-control" name="barcode" id="barcode" required>
                                                <span class="bar"></span>
                                                <label for="barcode">Barcode</label>
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="form-group m-b-40">
                                                <input type="text" class="form-control" name="rprice" id="rprice" required>
                                                <span class="bar"></span>
                                                <label for="rprice">Refence Price</label>
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="form-group m-b-40">

                                                <input type="text" class="form-control" name="option" data-role="tagsinput" id="option"/>


                                            </div>
                                        </div>
                                        <div class="col-md-12 asd">
                                            @include('system.modules.variations')
                                        </div>
                                    </div>
                                </div>
                                <button class="btn btn-success btn-submit">Submit</button>
                                <div class="col-md-12">
                                    @include('system.modules.variants')
                                </div>
                            </form>

和ajax的帖子

       <script>
    $('#option').change(function(){
    var data = $('#option').tagsinput('items');

        $.ajax({

            type:'POST',

            url:'/mauu',

            data:{ _token: '{{ csrf_token() }}', data:data},

            success:function(returnedHtml){
                $(".asd").html(returnedHtml);

            }

        });
    });
</script>
<script>
    $(".btn-submit").click(function(e){

        e.preventDefault();

        $.ajax({

            type:'POST',

            url:'/mauuu',

            data: $("#addpro").serialize(),

            success:function(){

            }
        });

    });


</script>

这是变种刀片

    @if(!empty($a))
@foreach($a as $b)
    <label class="sr-only" for="{{$b}}"></label>
    <input type="text" class="form-control" placeholder="{{$b}}" name="{{$b}}" data-role="tagsinput" id="{{$b}}"/></br>
@endforeach
    @endif

这是我的控制者

 public function sendd(Request $request){
    $a = $request['data'];

    return view ('system.modules.variations', compact('a'));
}

public function sent(Request $request){

  foreach ($request as $req ){
      $option = explode(',', $request['option']);

      $inputs = explode(',', $request->$option); // eg ['Color','Size']
      dd($inputs);
  }

发送提交按钮的功能

php laravel combinatorics cartesian-product
1个回答
0
投票

您需要具有表示其他输入的固定输入。

例如inputs然后使用inputs字段中给出的输入名称(在您的示例中它们是ColorSize),您可以调整当前代码以使用它们。例如,为了让你开始:

$inputs = explode(',', $request->input('inputs', '')); // eg ['Color','Size']

// now foreach input in inputs, create new input controls combinations
$props = [];
foreach($inputs as $input)
{
    $input_key = strtolower($input);
    if ( !$request->has($input_key) ) continue;
    $input_values = explode(',', $request->input($input_key));
    $props[$input_key] = $input_values;
}
$combinations = make_combinations($props); // NOTE this is destructive, it will destroy the current $props array, copy it if you need it
// now you can handle the combinations as you want

然后有一个实用程序递归函数make_combinations()(你可以使它成为一个控制器方法,如果你需要并调用$this->make_combinations($props)也更新下面的代码从make_combinations(..)$this->make_combinations(..))例如:

// adjust this function to return the information you need
// right now it returns only the names of the combinations
// adjust or add comment on having more information, eg price
function make_combinations($props)
{
    if ( empty($props) ) return [];
    $keys = array_keys($props);
    $key = $keys[0];
    $values = $props[$key];
    unset($props[$key]);  // this prop is being processed, remove it
    $rest =  make_combinations($props); // process the rest
    if ( empty($values) ) return $rest;
    if ( empty($rest) ) return $values;
    $combinations = []
    foreach($rest as $comb)
    {
        foreach($values as $value)
        {
           $combinations[] = $value . '-' . $comb;
        }
    }
    return $combinations;
}
© www.soinside.com 2019 - 2024. All rights reserved.