最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to bind a svelte component's state to the query string in Sapper - Stack Overflow

programmeradmin9浏览0评论

My sapper app revolves around a table ponent. This table ponent has state (offset, limit, sort, filters, etc) that I would like to represent in the query string of the URL.

The table works fine currently by directly updating its state:

<button on:click="{() => offset += limit}" disabled="{offset + rows.length >= count}">»</button>

I see that there is the page read-only store which can give me access to the current value of the query string. I imagine I can use something like URLSearchParams to generate links with the relevant value updated.

Is there a better way of handling this (some way of directly binding variables to the query string)? If not, what would be the best way to generate the links with the updated query strings?

EDIT:

My current solution is the following function:

const getLink = queryStringDiff =>  {
  const query = $page.query;
  const path = $page.path;
  const str = Object.entries({...query, ...queryStringDiff}).reduce(
    (acc, cur) => `${acc}&${cur[0]}=${cur[1]}`, ''
  ).replace('&', '?');
  return `${path}${str}`;
}

Which I reference in an a tag replacing the above button with:

href="{getLink({'offset': (offset - limit)})}"

It works, but I feel like there should be a better way

My sapper app revolves around a table ponent. This table ponent has state (offset, limit, sort, filters, etc) that I would like to represent in the query string of the URL.

The table works fine currently by directly updating its state:

<button on:click="{() => offset += limit}" disabled="{offset + rows.length >= count}">»</button>

I see that there is the page read-only store which can give me access to the current value of the query string. I imagine I can use something like URLSearchParams to generate links with the relevant value updated.

Is there a better way of handling this (some way of directly binding variables to the query string)? If not, what would be the best way to generate the links with the updated query strings?

EDIT:

My current solution is the following function:

const getLink = queryStringDiff =>  {
  const query = $page.query;
  const path = $page.path;
  const str = Object.entries({...query, ...queryStringDiff}).reduce(
    (acc, cur) => `${acc}&${cur[0]}=${cur[1]}`, ''
  ).replace('&', '?');
  return `${path}${str}`;
}

Which I reference in an a tag replacing the above button with:

href="{getLink({'offset': (offset - limit)})}"

It works, but I feel like there should be a better way

Share Improve this question edited Jun 25, 2019 at 2:38 Philip C asked Jun 23, 2019 at 14:41 Philip CPhilip C 1531 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 20

You can access query string state from the page store:

<script>
  import { stores } from '@sapper/app';
  const { page } = stores();

  $: start = $page.query.p * $page.query.itemsPerPage;
  $: end = start + $page.query.itemsPerPage;

  $: slice = myData.slice(start, end); // plus filtering, sorting etc
</script>

<table>
  <thead>...</thead>
  <tbody>
    {#each slice as row}
      <tr>...</tr>
    {/each}
  </tbody>
</table>

<a href="my-table?p={$page.query.p + 1}">next</a>

If you need to navigate programmatically, you can use goto:

<script>
  import { stores, goto } from '@sapper/app';

  // ...

  const next = () => {
    goto(`my-table?p=${$page.query.p + 1}`);
  };
</script>
发布评论

评论列表(0)

  1. 暂无评论