简体   繁体   English

ajax laravel 实时搜索问题

[英]ajax laravel live search issue

public function search_inv(Request $request)
{
    if($request->ajax()) {
        $data = inventory::where('p_name', 'LIKE', $request->country.'%')->get();
        $output = '';
       
        if (count($data)>0) {
            $output = '<ul class="list-group" style="display: block; position: relative; z-index: 1">';
            foreach ($data as $row) {
                $output .= '<table class="table table-bordered table-hover" id="abc" style="width:100%; float:right;">
                <tr style="width:100%;">
                    <th style="width:17.5%; text-align:center;">Piece Name</th>
                    <th style="width:15%; text-align:center;">Piece Model</th>
                    <th style="width:14%; text-align:center;">Piece Quantity</th>
                    <th style="width:13%; text-align:center;">Piece Price</th>
                   
                </tr>';
                
              $output.="<tr><td style=text-align=center>$row->p_name</td><td style=text-align=center>$row->p_model</td><td style=text-align=center>$row->p_Pieces</td><td style=text-align=center>$row->p_amount</td></tr>";
            }
            $output .=   '</table>';
        } else {
            $output .= '<li class="list-group-item">'.'No results'.'</li>';
        }
        return $output;
    }
}

I don't know what is the issue it is searching perfect but when I erase the search it duplicates the table header.我不知道它搜索完美的问题是什么,但是当我删除搜索时,它会复制表 header。

在此处输入图像描述

That's because your headers are within your foreach loop so every time it iterates, it's appending the headers to the output string (also your opening table element).那是因为您的标头在您的 foreach 循环中,因此每次迭代时,它都会将标头附加到 output 字符串(也是您的打开表元素)。

This should fix your issue:这应该可以解决您的问题:

 if($request->ajax()) {
      
    $data = inventory::where('p_name', 'LIKE', $request->country.'%')->get();
    
    $output = '<table class="table table-bordered table-hover" id="abc" style="width:100%; float:right;">';
    $output .= '<tr style="width:100%;">
                    <th style="width:17.5%; text-align:center;">Piece Name</th>
                    <th style="width:15%; text-align:center;">Piece Model</th>
                    <th style="width:14%; text-align:center;">Piece Quantity</th>
                    <th style="width:13%; text-align:center;">Piece Price</th>
                </tr>';

    if (! $data) {
        $output .= '<li class="list-group-item">No results</li>';
    }else{
        $data->each( function( $row ) use ( &$output ){
            $output .= '<tr>
                <td style="text-align:center">' . $row->p_name . '</td>
                <td style="text-align:center">' . $row->p_model . '</td>
                <td style="text-align:center">' . $row->p_Pieces . '</td>
                <td style="text-align:center">' . $row->p_amount . '</td>
            </tr>';
        });
    }

    $output .= '</table>';
    
    return $output;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM