<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vijayakumar</title>
    <description>The latest articles on DEV Community by Vijayakumar (@gvk).</description>
    <link>https://dev.to/gvk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F714052%2F77fda50e-9e3a-42c8-a268-b69215a99516.png</url>
      <title>DEV Community: Vijayakumar</title>
      <link>https://dev.to/gvk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gvk"/>
    <language>en</language>
    <item>
      <title>Pagination and search in vue bootstrap table</title>
      <dc:creator>Vijayakumar</dc:creator>
      <pubDate>Mon, 27 Sep 2021 07:30:00 +0000</pubDate>
      <link>https://dev.to/gvk/pagination-and-search-in-vue-bootstrap-table-47l8</link>
      <guid>https://dev.to/gvk/pagination-and-search-in-vue-bootstrap-table-47l8</guid>
      <description>&lt;p&gt;Vue is really useful when you want to bring up an application up very soon with very minimal learning curve.&lt;/p&gt;

&lt;p&gt;But, when you want to fine tune the application is when you would have to make sure that you are know the finer details (though this can be said for any language/framework)&lt;/p&gt;

&lt;p&gt;Lets assume that you have a basic Vue bootstrap table in place with the below tag&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;b-table
....
  :items="employees"
  :fields="fields"
&amp;lt;/b-table&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the data being provided to the above table as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data() {
    return {
      fields: [
        { key: 'id', sortable: true },
        { key: 'name', sortable: true, },
        { key: 'delete', label: 'Delete' },
      ],
      employees: [
        {
          id: 101,
          name: 'Sam',
        },
        {
          id: 102,
          name: 'Jane',
        },
      ],
    }
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pagination in the above table can be added using the &lt;code&gt;b-pagination&lt;/code&gt; tag. Lets go ahead and add the basic pagination to the above table&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;b-pagination
  v-model="currentPage"
  :total-rows="rows"
  :per-page="perPage"
  aria-controls="my-table"
&amp;gt;&amp;lt;/b-pagination&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;currentPage&lt;/code&gt; will contain the current page the table is in, the &lt;code&gt;rows&lt;/code&gt; variable will show the total rows in the table and the &lt;code&gt;perPage&lt;/code&gt; will allow the number of items to be shown per page. &lt;/p&gt;

&lt;p&gt;A simple text box is enough to bring search which can be done using &lt;code&gt;b-form-input&lt;/code&gt; tag&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;b-form-input
  v-model="filter"
  type="search"
  placeholder="Type to filter data"
&amp;gt;&amp;lt;/b-form-input&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above tag, the &lt;code&gt;filter&lt;/code&gt; variable will be responsible to filter data in the table.&lt;/p&gt;

&lt;p&gt;So, lets link the above pagination and search to the table&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;b-table
  ...
  :filter="filter"
  :per-page="perPage"
  :current-page="currentPage"
  ...
&amp;gt;

....
....

data() {
    return {
      perPage: 10,
      currentPage: 1,
      rows: 1,
      filter: null,
      ...
    }
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code will bring basic pagination and search to our table. But to make it fully complete, we will need to update the rows count for the table.&lt;/p&gt;

&lt;p&gt;Lets do that when the component is getting mounted for the initial state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mounted() {
    this.rows = this.employees.length
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to update the row count dynamically, lets make use of &lt;code&gt;filtered&lt;/code&gt; event which also provides with &lt;code&gt;filteredItems&lt;/code&gt; arguments which is basically an array of items after filtering (before local pagination occurs)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;b-table
    ...
    @filtered="onFiltered"
    ...
&amp;gt;
...
...
methods: {
    onFiltered(filteredItems) {
      this.rows = filteredItems.length
      this.currentPage = 1
    },
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;onFiltered&lt;/code&gt; method updates the current number of rows after filtering and the current page in pagination. These 2 fields are required to be updated dynamically. Failing to do so will show a blank page when filtering from pages other than the first one.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>bootstarp</category>
    </item>
  </channel>
</rss>
