ajax - ajax laravel 实时搜索问题
问题描述
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;
}
}
我不知道它搜索完美的问题是什么,但是当我删除搜索时,它会复制表头。
解决方案
那是因为您的标题在您的 foreach 循环中,因此每次迭代时,它都会将标题附加到输出字符串(也是您的打开表元素)。
这应该可以解决您的问题:
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;
}
推荐阅读
- python - Django - 我如何复制同一类中的字段
- reactjs - React hooks - useEffect 方法不断获取
- c# - 如何在 LINQ 查询中使用从 Request.Query 获得的多值/数组参数(枚举值)?
- r - 在 Linux 上升级 R 时会删除软件包吗?
- javascript - 从 React 数组中过滤过期卡片
- json - 如何从clojure中的字符串中获取每个json对象?
- tensorflow - 批量标准化会破坏验证性能
- python - 从数组的每一行创建一个对象
- java - 使用 Spring webflow 处理外部身份验证流
- multithreading - 如何保持线程的消息泵反应