将值从控制器传递到模板并动态添加一个数字。

huangapple go评论73阅读模式
英文:

Pass value from controller to the blade and dynamically add a number to it

问题

在使用jQuery添加新行时,我想在id中添加一个数字,该数字是从控制器传递到Blade的。

以下是Blade中的JavaScript代码:

$('#add4').click(function(){
  var rowCount = $('#table_field4 .trr').length + 1;
  var html = '<tr><td><input type="text" value="{{$serialNumberUnits+rowCount}}"></td></tr>';
  $('#table_field5').append(html);
});

这是从控制器传递到Blade的变量:

$serialNumberUnits = DB::table('product_unit_prices')->latest('id')->first()->id + 1;
return view('admin.addProduct', compact('serialNumberUnits'));

请注意,上述代码中的变量$serialNumberUnits会在Blade模板中的JavaScript代码中使用,以生成新行的id。

英文:

I want to add one number in the case of adding a new line by jQuery, and this number is added to the id that I pass from the controller to the blade

将值从控制器传递到模板并动态添加一个数字。

Here is the js Code in the blade

$(&#39;#add4&#39;).click(function(){
  var rowCount = $(&#39;#table_field4 .trr&#39;).length + 1;
  var html = &#39;&lt;tr&gt;&#39;+&#39;&lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;{{$serialNumberUnits+&#39;rowCount&#39;}}&quot;&gt;&lt;/td&gt;&lt;/tr&gt;&#39;;
  $(&#39;#table_field5&#39;).append(html);
});

And here is the variable that I passed from the controller to the blade

$serialNumberUnits = DB::table(&#39;product_unit_prices&#39;)-&gt;latest(&#39;id&#39;)-&gt;first()-&gt;id + 1;
return  view(&#39;admin.addProduct&#39;,compact(&#39;serialNumberUnits&#39;));

答案1

得分: 2

The $serialNumberUnits will be a string, try to convert to an integer using parseInt() and do the calculations

$('#add4').click(function () {
    let rowCount = $('#table_field4 .trr').length + 1;

    /**
     * Keeping the passed value in a variable 
     * to keeping it clean
     */
    const serialNoUnits = "{{$serialNumberUnits ?? 0}}";

    /**
     * Parsing into integer and adding 
     */
    let newValue = parseInt(serialNoUnits) + rowCount;

    /**
     * Adding value into field
     */
    let html = '<tr>' + '<td><input type="text" value="' + newValue + '"></td></tr>';
    $('#table_field5').append(html);
});
英文:

The $serialNumberUnits will be a string, try to convert to an integer using parseInt() and do the calculations

 $(&#39;#add4&#39;).click(function () {

    let rowCount = $(&#39;#table_field4 .trr&#39;).length + 1;

    /**
     * Keeping the passed value in a variable 
     * to keeping it clean
     */
    const serialNoUnits = &quot;{{$serialNumberUnits ?? 0}}&quot;;

    /**
     * Parsing into integer and adding 
     */
    let newValue = parseInt(serialNoUnits) + rowCount;

    /**
     * Adding value into field
     */
    let html = &#39;&lt;tr&gt;&#39; + &#39;&lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;&#39;+newField+&#39;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;&#39;;
    $(&#39;#table_field5&#39;).append(html);
  });

huangapple
  • 本文由 发表于 2023年2月19日 21:21:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定