【问题标题】:how to make button in table to route to url如何在表格中制作按钮以路由到 url
【发布时间】:2021-04-06 22:49:49
【问题描述】:

我有表 <thead> 在刀片中初始化,foreach 在控制器中循环 <tbody>

这就是它在控制器中的样子:

foreach($data as $row)
       {
        $output .= '
        <tr>

         <td>'.$row->year.'</td>

         <td>
            <a href={{url(/edit/'.$row->id.')}} class="...">Edit</a>
            <a href="#" class="...">Delete</a>
         </td>

        </tr>
        ';
       }

在刀片中,初始化如下:

<table>
       <thead>

        <tr>
         <th>Year</th>
         <th> BUTTON </th>
        </tr>

       </thead>

       <tbody></tbody>

      </table>

script 将数据提取到&lt;tbody&gt;

<script>
$(document).ready(function(){

 fetch_customer_data();

 function fetch_customer_data(query = '')
 {
  $.ajax({
   url:"{{ route('live_search.action') }}", // live_search: is the blade where the table is
   method:'GET',
   data:{query:query},
   dataType:'json',
   success:function(data)
   {
    $('tbody').html(data.table_data); // TABLE DATA
    $('#total_records').text(data.total_data);
   }
  })
 }

 $(document).on('keyup', '#search', function(){
  var query = $(this).val();
  fetch_customer_data(query);
 });
});
</script>

数据和按钮出现在表格中,但按钮无法正常工作。我如何使用表格中的按钮存在问题,此按钮:&lt;a href= "{{url(/edit/'.$row-&gt;student_id.')}}" class="..."&gt;Edit&lt;/a&gt; 生成此网址:http://sms.test/%7B%7Burl(/edit/2)%7D%7D 而网址应如下所示:http://sms.test/edit/ID22 但是当我将href= "{{url(/edit/'.$row-&gt;student_id.')}}" 更改为@ 987654333@ 它在 url 栏中显示#。所以我想问题出在这一行"{{url(/edit/'.$row-&gt;student_id.')}}"

这是到edit 的路径: Route::get('/edit/{id}', 'StudentController@edit');

function edit @controller 看起来像这样:

public function edit($id)
    {
      $student = Student::find($id); 
      $students = Student::all();
      return view('student',['students'=>$students,'student'=>$student,'layout'=>'edit']);

    }

我尝试了所有我知道的方法,但无法让它发挥作用:(请帮助

【问题讨论】:

    标签: php jquery laravel html-table


    【解决方案1】:

    你不能在一个字符串中包含 Blade 的东西并期望它被编译器解析。要么避免在模板字符串中使用 Blade 并像上一年那样连接它:

    foreach($data as $row) {
        $output .= '
        <tr>
            <td>'.$row->year.'</td>
            <td>
                <a href="'.url('/edit/'.$row->id).'" class="...">Edit</a>
                <a href="#">Delete</a>
            </td>
        </tr>
        ';
    }
    

    也可以为此使用自定义刀片模板,就像您在 student.blade.php 中所做的那样。这将是一种更清洁的方法。

    【讨论】:

    • 效果很好。也感谢您的建议,我会尽力做到这一点
    【解决方案2】:

    您在控制器中的代码只是将其构建为字符串,实际上并没有使用这些参数调用函数 url()

    尝试稍微改变它以连接到完整的 href="'.url(/edit/$row->id).'" 返回而不是 href={{ url(/edit/'.$row->id.')}} 因为它可以从刀片模板本身工作。

    这样:

    foreach($data as $row)
       {
        $output .= '
        <tr>
    
         <td>'.$row->year.'</td>
    
         <td>
            <a href="'.url(/edit/$row->id).'" class="...">Edit</a>
            <a href="#" class="...">Delete</a>
         </td>
    
        </tr>
        ';
       }
    

    【讨论】:

    • 当然 :) 欢迎您投票并接受我的回答 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 2022-12-21
    • 2021-10-23
    • 2012-09-13
    • 2017-10-22
    • 1970-01-01
    相关资源
    最近更新 更多