October CMS resources and help articles

Simple and to the point. Optimized by the community.

create and use custom taglist for backend form

0
by eahmadi1988, last modified on April 9th, 2020

I have reportController and for more filter use talglist for form but some models have many records(20,000 or more) and take much time to load all data for report so I use my custom taglist and ajax request for loading data.

1- In yourNamepace/yourPlugin/models/YourModel/report.yaml

test:
  type: partial
  path: customtags

2- In yourNamepace/yourPlugin/controllers/reportcontrollers/ Create _customtags.htm

In this file

<select id="tags" style="width:200px" name="data[]"></select>

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

    $("#tags").select2({
      multiple: true,
      ajax: {
        dataType: 'json',
        delay: 250,
        url: function (params) {
          return 'http://localhost/anbar/kalas/' + params.term;
        },
        data: function (params) {
          let query = {
            search: params.term,
          };
          // Query parameters will be ?search=[term]&type=public
          // return query;
          return query;
        },
        processResults: function (data, page) {
          console.log(data);
          // parse the results into the format expected by Select2.
          // since we are using custom formatting functions we do not need to
          // alter the remote JSON data
          return {
            results: data
          };
        },
        // cache: true
      },
      escapeMarkup: function (markup) {
        return markup;
      },
      minimumInputLength: 1,
      templateResult: formatRepo,
      templateSelection: formatRepoSelection

    });

    function formatRepo(repo) {

      if (repo.loading) return repo.text;

      var markup =
        '<div>' + repo.name + '</div>'
      ;

      markup += '</div></div>';

      return markup;
    }

    function formatRepoSelection(repo) {
      return repo.name || repo.text;
    }
  });
</script>

3- In plugin create routes.php and get data from your model like this example

Route::get('/kalas/{name}', function ($name) {
  $query = substr($name, strpos($name, '?'));

  $kalas = \Ehsan\Anbar\Models\Kala::where('name', 'like', '%' . $query . '%')
    ->select(['id','name'])
    ->get()
    ->toJson();
  return $kalas;
});

and thanks @LarryB and @OFFLINE

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?