【问题标题】:how to i search on search results in laravel 4.2如何在 laravel 4.2 中搜索搜索结果
【发布时间】:2015-04-29 06:58:02
【问题描述】:

有一个简单的页面,我在其中按关键字显示搜索结果。 但现在我想按一些标准搜索这些结果。

我该怎么做

搜索控制器

class SearchController extends \BaseController {

    public function search() {
        $keyword = Input::get('header-search-query');

        if (empty($keyword)) {
            $product = Product::paginate(10);
            $this - > setModel($product);
            return View::make('product.listbykeyword') - > with('products', $product);
        }

        return View::make('product.listbykeyword') - > with('products', Product::like('title', $keyword) - > paginate(10));
    }

    public function advance() {
        //what should i put here
    }
}

查看

@foreach($products as $product)
 <div class="media">
    <div class="media-left" style="width:16%;">
        <a href="{{ URL::to('image/'.$product->productImage()->first()['image']) }}">
        {{ HTML::image('image/'.$product->productImage()->first()['image'],'no-image',array('class'=>'media-object','height'=>'100','width'=>'100'))}}
         </a>
     </div>
 <div class="media-body">
     <h4 class="media-heading">{{ HTML::link('/product/'.$product->id,$product->title) }}</h4>
     <p>{{substr($product->description,0,85)}}</p>
     <p>Price : {{$product->price}} TK</p>
     <p class="time">{{$product->created_at->diffForHumans()}}
          <b>Near</b> {{$product->location}}
     </p>
  </div>
 </div>
 <hr></hr>
@endforeach

【问题讨论】:

  • 使用你需要搜索从视图中得到的结果?那你为什么不使用Datatables
  • 是的,我想做一些类似数据表的事情。但不像数据表那样。
  • 如果你去这个链接noneedjob.com/project/public/search你会知道我想做什么
  • MethodNotAllowedHttpException
  • 在数据表中你可以有过滤、搜索、分页,那为什么不能数据表呢?你想知道如何在 laravel 或什么中做到这一点?

标签: php search laravel eloquent


【解决方案1】:

在您的情况下,您确实不能使用 Datatables,因为它是公共搜索,我建议您在 keychange 事件上使用 ajax 调用。

第 1 步:

从你的文本框中检测 keychange

<input id='myTextbox1' type='text'/>
$('#myTextbox1').on('input', function() {
    alert('Text1 changed!');
});

这里是fiddle

第 2 步:

调用 ajax 页面到你的控制器

 $.ajax({
        type: "POST",
        url : "yourcontroller",
        data : dataString,
        success : function(data){
        $("#result").html(data);                           
        }
  });

在您的页面中放置一个名为 result 的 div。

所以,第一次你会从你用来搜索的默认控制器中得到结果,然后下次当你在 myTextbox1 中输入文本时,它会调用控制器 yourcontroller 并且在你的控制器中你应该得到datastring 并将匹配的输出返回给视图。

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 2019-11-05
    • 1970-01-01
    • 2021-05-14
    • 2017-02-24
    • 2014-11-28
    • 2018-11-19
    • 2014-07-02
    • 1970-01-01
    • 2019-02-02
    相关资源
    最近更新 更多