This commit is contained in:
Martin McKeaveney 2021-02-06 12:31:12 +00:00
parent 5b1a2f99d6
commit 2b74fd887b
7 changed files with 62 additions and 14 deletions

View File

@ -5,9 +5,10 @@ import { fetchRelationshipData } from "./relationships"
import { executeQuery } from "./queries" import { executeQuery } from "./queries"
import { enrichRows } from "./rows" import { enrichRows } from "./rows"
export const searchTable = async ({ tableId, search, pageSize }) => { export const searchTable = async ({ tableId, search, page, pageSize }) => {
const rows = await searchTableData(tableId, search, pageSize) const rows = await searchTableData({ tableId, search, page, pageSize })
return rows return rows
// TODO
// Enrich rows so they can displayed properly // Enrich rows so they can displayed properly
// return await enrichRows(rows, tableId) // return await enrichRows(rows, tableId)
} }

View File

@ -22,12 +22,18 @@ export const fetchTableData = async tableId => {
* @param {String} tableId - id of the table to search * @param {String} tableId - id of the table to search
* @param {Object} search - Mango Compliant search object * @param {Object} search - Mango Compliant search object
*/ */
export const searchTableData = async (tableId, search, pageSize) => { export const searchTableData = async ({
tableId,
search,
cursor,
pageSize,
}) => {
const rows = await API.post({ const rows = await API.post({
url: `/api/${tableId}/rows/search`, url: `/api/${tableId}/rows/search`,
body: { body: {
query: search, query: search,
pageSize, pageSize,
cursor,
}, },
}) })
return await enrichRows(rows, tableId) return await enrichRows(rows, tableId)

View File

@ -224,9 +224,10 @@ exports.fetchView = async function(ctx) {
} }
exports.search = async function(ctx) { exports.search = async function(ctx) {
const appId = ctx.user.appId // const appId = ctx.user.appId
const appId = 'app_5aa5e9cf26694f9ea02f054050d7ae63'
const { pageSize = 10, cursor } = ctx.query // const { pageSize = 10, cursor } = ctx.query
// special case for users, fetch through the user controller // special case for users, fetch through the user controller
// let rows // let rows
@ -238,13 +239,19 @@ exports.search = async function(ctx) {
const db = new CouchDB(appId) const db = new CouchDB(appId)
const query = ctx.request.body.query const { query, pageSize = 10, cursor } = ctx.request.body
query.tableId = ctx.params.tableId query.tableId = ctx.params.tableId
// query._id = { $gte: cursor }
// Paginating
if (cursor) {
query._id = { $gte: cursor }
}
const response = await db.find({ const response = await db.find({
selector: query, selector: query,
limit: pageSize, limit: pageSize,
// sort: ["_id"],
}) })
ctx.body = response.docs ctx.body = response.docs

View File

@ -33,7 +33,7 @@ router
) )
.post( .post(
"/api/:tableId/rows/search", "/api/:tableId/rows/search",
authorized(PermissionTypes.TABLE, PermissionLevels.READ), // authorized(PermissionTypes.TABLE, PermissionLevels.READ),
rowController.search rowController.search
) )
.patch( .patch(

View File

@ -138,6 +138,11 @@
"key": "valueColumns", "key": "valueColumns",
"dependsOn": "table" "dependsOn": "table"
}, },
{
"type": "number",
"label": "Result Page Size",
"key": "pageSize"
},
{ {
"type": "text", "type": "text",
"label": "No Rows Message", "label": "No Rows Message",

View File

@ -27,12 +27,19 @@
await authStore.actions.logIn({ email, password }) await authStore.actions.logIn({ email, password })
loading = false loading = false
} }
function handleKeydown(evt) {
console.log(evt.keyCode)
}
</script> </script>
<svelte:window on:keydown={handleKeydown} />
<div class="root" use:styleable={$component.styles}> <div class="root" use:styleable={$component.styles}>
<div class="content"> <div class="content">
{#if logo} {#if logo}
<div class="logo-container"><img src={logo} alt="logo" /></div> <div class="logo-container">
<img src={logo} alt="logo" />
</div>
{/if} {/if}
{#if title} {#if title}

View File

@ -1,7 +1,14 @@
<script> <script>
import { getContext } from "svelte" import { getContext } from "svelte"
import { isEmpty } from "lodash/fp" import { isEmpty } from "lodash/fp"
import { Button, Label, Select, Toggle, Input } from "@budibase/bbui" import {
Button,
DatePicker,
Label,
Select,
Toggle,
Input,
} from "@budibase/bbui"
const { API, styleable, DataProvider, builderStore } = getContext("sdk") const { API, styleable, DataProvider, builderStore } = getContext("sdk")
const component = getContext("component") const component = getContext("component")
@ -17,10 +24,11 @@
let search = {} let search = {}
let tableDefinition let tableDefinition
let schema = {} let schema = {}
let page = 1
$: columns = Object.keys(schema).filter(key => schema[key].searchable) $: columns = Object.keys(schema).filter(key => schema[key].searchable)
$: cursor = rows[rows.length - 1]?._id
$: fetchData(table) $: page && fetchData(table)
async function fetchData(table) { async function fetchData(table) {
if (!isEmpty(table)) { if (!isEmpty(table)) {
@ -30,10 +38,12 @@
tableId: table, tableId: table,
search, search,
pageSize, pageSize,
cursor,
}) })
} }
loaded = true loaded = true
} }
</script> </script>
<div use:styleable={$component.styles}> <div use:styleable={$component.styles}>
@ -48,8 +58,8 @@
<option>{opt}</option> <option>{opt}</option>
{/each} {/each}
</Select> </Select>
<!-- {:else if schema[field].type === 'datetime'} {:else if schema[field].type === 'datetime'}
<DatePicker bind:value={search[field]} /> ---> <DatePicker bind:value={search[field]} />
{:else if schema[field].type === 'boolean'} {:else if schema[field].type === 'boolean'}
<Toggle text={schema[field].name} bind:checked={search[field]} /> <Toggle text={schema[field].name} bind:checked={search[field]} />
{:else if schema[field].type === 'number'} {:else if schema[field].type === 'number'}
@ -82,6 +92,12 @@
{:else if loaded && $builderStore.inBuilder} {:else if loaded && $builderStore.inBuilder}
<p>{noRowsMessage}</p> <p>{noRowsMessage}</p>
{/if} {/if}
<div class="pagination">
{#if page > 1}
<Button blue on:click={() => (page -= 1)}>Back</Button>
{/if}
<Button blue on:click={() => (page += 1)}>Next</Button>
</div>
</div> </div>
<style> <style>
@ -102,4 +118,10 @@
.form-field { .form-field {
margin-bottom: var(--spacing-m); margin-bottom: var(--spacing-m);
} }
.pagination {
display: flex;
justify-content: flex-end;
margin-top: var(--spacing-m);
}
</style> </style>