zofe / rapyd-laravel

deprecated rewritten in rapyd-livewire
MIT License
866 stars 297 forks source link

Autocomplete remote ajax with all the fields #326

Open aperezlillo opened 8 years ago

aperezlillo commented 8 years ago

Actually the autocomplete feature only sends in the ajax request the query inserted by the user. But probably could be interesting to send all the parameters of the current form, to be able to filter the result obtained from the ajax request.

One example it's a classic selector country and cities, the second one could be filled with only the cities of the selected country.

Thank's!

zofe commented 8 years ago

by default rapyd can handle basic cruds.. but you can reach some complex behavior. keep in mind that every "field" can be build stand-alone.

Just a sample about how to render single fields via ajax to handle some kind of parent-child (or cascade selection). In this example (I just tried to cleanup a real controller ..) there is an "article" with tree fields:

Hope it can help


    public function anyArticle()
    {
        $edit = \DataEdit::source(new Articles());
        $edit->add('title', 'Title', 'text')->rule('required');

        //note an onchange event
        $edit->add('id_category','Categoria','select')
            ->rule('required')
            ->option("","")->options(Categories::lists('catname','id'))
            ->onchange('$.get("/admin/article/checkboxes", {id_category: $("#id_category").val()}, 
function (data) {
        $("#div_options").html(data);
    });');

        //dependend field, empty on create.. filled on other statuses 
        $edit->add('options','Options','Checkboxgroup')->separator('<br>');
        if ($edit->status!='create') {
                $edit->field('options')->options(
                Options::whereHas('categories', function($query) use ($edit){
                    return  $query->where('id','=',$edit->model->id_category);
                })->lists('option_title','id'));
        }    

        $form = $edit->getForm();
        return $edit->view('admin.articles.article', array('content' => $form));
    }

    //ajax rendered field on route: /admin/article/checkboxes
    public function anyCheckboxes()
    {
        $id_category = \Input::get('id_category');
        $checks =  new Checkboxgroup('options', 'Options');
        $checks->status = 'modify';
        $checks->options(
                Options::whereHas('categories', function($query) use ($id_category){
                    return  $query->where('id','=',$id_category);
                })->pluck('option_title','id'))->separator('<br>');
        $checks->build();
        return $checks->output;
    }