如何在Laravel Nova 4自定义工具中使分页功能正常工作?

huangapple go评论65阅读模式
英文:

How do I get pagination working in laravel a Nova 4 custom tool

问题

Here's the translated code with the requested changes:

<template>
    <LoadingView :loading="initialLoading">


        <Heading :level="1" class="mb-3 flex items-center">
            <span>广告</span>
        </Heading>

        <div class="flex mb-6">
            <IndexSearchInput
                :placeholder="searchPlaceholder"
            />
            <div class="w-full flex items-center" >
                <!-- 创建/附加按钮 -->
                <CreateResourceButton
                    :label="createButtonLabel"
                    :singular-name="singularName"
                    :resource-name="resourceName"
                    class="flex-shrink-0 ml-auto mb-6"
                />
            </div>
        </div>
        <Card>
            <ResourceTableToolbar>

            </ResourceTableToolbar>
            <LoadingView :loading="loading">
                <table class="w-full divide-y divide-gray-100 dark:divide-gray-700"
                       data-testid="resource-table">
                    <thead class="bg-gray-50 dark:bg-gray-800">
                    <tr>
                        <th class="td-fit uppercase text-xxs text-gray-500 tracking-wide pl-5 pr-2 py-2"><span class="sr-only">已选资源</span></th>
                        <th class="text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2">ID</th>
                        <th class="text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2">标题</th>
                        <th class="text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2">开始日期</th>
                        <th class="text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2">结束日期</th>
                    </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100 dark:divide-gray-700">
                    <tr class="group" v-for="(item, index) in items" :key="index">
                        <td class="py-2 cursor-pointer td-fit pl-5 pr-5 dark:bg-gray-800 group-hover:bg-gray-50 dark:group-hover:bg-gray-900"><input type="checkbox" class="checkbox" aria-label="选择资源 1" data-testid="adverts-items-0-checkbox" dusk="1-checkbox"></td>
                        <td class="px-2 py-2 whitespace-nowrap cursor-pointer dark:bg-gray-800 group-hover:bg-gray-50 dark:group-hover:bg-gray-900">
                            <div class="text-left"> <span class="whitespace-nowrap">{{ item.id }}</span></div>
                        </td>
                        <td class="px-2 py-2 whitespace-nowrap cursor-pointer dark:bg-gray-800 group-hover:bg-gray-50 dark:group-hover:bg-gray-900">{{ item.headline }}</td>
                        <td class="px-2 py-2 whitespace-nowrap cursor-pointer dark:bg-gray-800 group-hover:bg-gray-50 dark:group-hover:bg-gray-900">{{ item.start_date }}</td>
                        <td class="px-2 py-2 whitespace-nowrap cursor-pointer dark:bg-gray-800 group-hover:bg-gray-50 dark:group-hover:bg-gray-900">{{ item.end_date }}</td>
                    </tr>
                    </tbody>
                </table>
                <pagination-links
                    v-if="resourceResponse"
                    :resource-name="resourceName"
                    :resources="resources"
                    :resource-response="resourceResponse"
                    @previous="selectPreviousPage"
                    @next="selectNextPage">
                </pagination-links>
            </LoadingView>
        </Card>
    </LoadingView>
</template>

<script>
import axios from 'axios';
import { Paginatable, PerPageable } from 'laravel-nova';

export default {

    mixins: [
        Paginatable,
        PerPageable
    ],

    data() {
        return {
            items: [],
            initialLoading: true,
            loading:false,
            createButtonLabel: '创建新广告',
            searchPlaceholder: '搜索广告',
            singularName: '广告',
            resourceName: '广告',
        };
    },
    mounted() {
        this.getAdverts();
    },
    methods: {
        getAdverts() {
            this.initialLoading = false;
            this.loading = true;
            this.paginate(axios.get('/nova-vendor/manager/index'))
                .then(response => {
                    this.items = response.data;
                    this.loading = false;
                })
                .catch(error => {
                    console.log(error);
                });

        },
        computed: {
            /**
             * 返回视图的标题
             */
            headingTitle() {
                if (this.initialLoading) {
                    return '&nbsp;';
                } else {
                    return '广告';
                }
            },
        }
    }
}
</script>

<style>
/* 在此添加自定义样式 */
</style>

These changes should help make the code work properly with the Paginatable mixin from the 'laravel-nova' library.

英文:

This is the tool.vue file

&lt;template&gt;
&lt;LoadingView :loading=&quot;initialLoading&quot;&gt;
&lt;Heading :level=&quot;1&quot; class=&quot;mb-3 flex items-center&quot;&gt;
&lt;span&gt;Adverts&lt;/span&gt;
&lt;/Heading&gt;
&lt;div class=&quot;flex mb-6&quot;&gt;
&lt;IndexSearchInput
:placeholder=&quot;searchPlaceholder&quot;
/&gt;
&lt;div class=&quot;w-full flex items-center&quot; &gt;
&lt;!-- Create / Attach Button --&gt;
&lt;CreateResourceButton
:label=&quot;createButtonLabel&quot;
:singular-name=&quot;singularName&quot;
:resource-name=&quot;resourceName&quot;
class=&quot;flex-shrink-0 ml-auto mb-6&quot;
/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;Card&gt;
&lt;ResourceTableToolbar&gt;
&lt;/ResourceTableToolbar&gt;
&lt;LoadingView :loading=&quot;loading&quot;&gt;
&lt;table class=&quot;w-full divide-y divide-gray-100 dark:divide-gray-700&quot;
data-testid=&quot;resource-table&quot;&gt;
&lt;thead class=&quot;bg-gray-50 dark:bg-gray-800&quot;&gt;
&lt;tr&gt;
&lt;th class=&quot;td-fit uppercase text-xxs text-gray-500 tracking-wide pl-5 pr-2 py-2&quot;&gt;&lt;span class=&quot;sr-only&quot;&gt;Selected Resources&lt;/span&gt;&lt;/th&gt;
&lt;th class=&quot;text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2&quot;&gt;ID&lt;/th&gt;
&lt;th class=&quot;text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2&quot;&gt;Headline&lt;/th&gt;
&lt;th class=&quot;text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2&quot;&gt;Start Date&lt;/th&gt;
&lt;th class=&quot;text-left px-2 whitespace-nowrap uppercase text-gray-500 text-xxs tracking-wide py-2&quot;&gt;End Date&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody class=&quot;divide-y divide-gray-100 dark:divide-gray-700&quot;&gt;
&lt;tr class=&quot;group&quot; v-for=&quot;(item, index) in items&quot; :key=&quot;index&quot;&gt;
&lt;td class=&quot;py-2 cursor-pointer td-fit pl-5 pr-5 dark:bg-gray-800 group-hover:bg-gray-50 dark:group-hover:bg-gray-900&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;checkbox&quot; aria-label=&quot;Select Resource 1&quot; data-testid=&quot;adverts-items-0-checkbox&quot; dusk=&quot;1-checkbox&quot;&gt;&lt;/td&gt;
&lt;td class=&quot;px-2 py-2 whitespace-nowrap cursor-pointer dark:bg-gray-800 group-hover:bg-gray-50 dark:group-hover:bg-gray-900&quot;&gt;
&lt;div class=&quot;text-left&quot;&gt; &lt;span class=&quot;whitespace-nowrap&quot;&gt;{{ item.id }}&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
&lt;td class=&quot;px-2 py-2 whitespace-nowrap cursor-pointer dark:bg-gray-800 group-hover:bg-gray-50 dark:group-hover:bg-gray-900&quot;&gt;{{ item.headline }}&lt;/td&gt;
&lt;td class=&quot;px-2 py-2 whitespace-nowrap cursor-pointer dark:bg-gray-800 group-hover:bg-gray-50 dark:group-hover:bg-gray-900&quot;&gt;{{ item.start_date }}&lt;/td&gt;
&lt;td class=&quot;px-2 py-2 whitespace-nowrap cursor-pointer dark:bg-gray-800 group-hover:bg-gray-50 dark:group-hover:bg-gray-900&quot;&gt;{{ item.end_date }}&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;pagination-links
v-if=&quot;resourceResponse&quot;
:resource-name=&quot;resourceName&quot;
:resources=&quot;resources&quot;
:resource-response=&quot;resourceResponse&quot;
@previous=&quot;selectPreviousPage&quot;
@next=&quot;selectNextPage&quot;&gt;
&lt;/pagination-links&gt;
&lt;/LoadingView&gt;
&lt;/Card&gt;
&lt;/LoadingView&gt;
&lt;/template&gt;
&lt;script&gt;
import axios from &#39;axios&#39;;
import { Paginatable, PerPageable } from &#39;laravel-nova&#39;;
export default {
mixins: [
Paginatable,
PerPageable
],
data() {
return {
items: [],
initialLoading: true,
loading:false,
createButtonLabel: &#39;Create New Advert&#39;,
searchPlaceholder: &#39;Search Adverts&#39;,
singularName: &#39;Advert&#39;,
resourceName: &#39;Adverts&#39;,
};
},
mounted() {
this.getAdverts();
},
methods: {
getAdverts() {
this.initialLoading = false;
this.loading = true;
axios.get(&#39;/nova-vendor/manager/index&#39;)
.then(response =&gt; {
this.items = response.data;
this.loading = false;
})
.catch(error =&gt; {
console.log(error);
});
},
computed: {
/**
* Return the heading for the view
*/
headingTitle() {
if (this.initialLoading) {
return &#39;&amp;nbsp;&#39;
} else {
return &#39;Adverts&#39;
}
},
}
}
}
&lt;/script&gt;
&lt;style&gt;
/* Your custom styles here */
&lt;/style&gt;

I was told the following :

1 Import the Paginatable mixin at the top of the file:

import { Paginatable } from &#39;laravel-nova&#39;;

2 Add the Paginatable mixin to the mixins array:

mixins: [ Paginatable]

3 Modify the getAdverts method to use the paginate method provided by the Paginatable mixin:

getAdverts() {
this.initialLoading = false;
this.loading = true;
this.paginate(axios.get(&#39;/nova-vendor/manager/index&#39;))
.then(response =&gt; {
this.items = response.data;
this.loading = false;
})
.catch(error =&gt; {
console.log(error);
});
}

4 Add the pagination-links component to the template:

&lt;pagination-links
v-if=&quot;resourceResponse&quot;
:resource-name=&quot;resourceName&quot;
:resources=&quot;resources&quot;
:resource-response=&quot;resourceResponse&quot;
@previous=&quot;selectPreviousPage&quot;
@next=&quot;selectNextPage&quot;&gt;
&lt;/pagination-links&gt;```

But it doesn't work, I get following error in:

>WARNING in ./resources/js/pages/Tool.vue?vue&type=script&lang=js (./node_modules/babel-loader/lib/index.js??clonedRuleSet-5.use[0]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./resources/js/pages/Tool.vue?vue&type=script&lang=js) 4:11-22 export 'Paginatable' (imported as 'Paginatable') was not found in 'laravel-nova' (possible exports: CopiesToClipboard, DependentFormField, Errors, FieldValue, FormEvents, FormField, HandlesFieldAttachments, HandlesFormRequest, HandlesPanelVisibility, HandlesUploads, HandlesValidationErrors, HasCards, Localization, MetricBehavior, PreventsFormAbandonment, PreventsModalAbandonment, mapProps, useCopyValueToClipboard, useLocalization)

Any idea how to get this working properly?

答案1

得分: 0

Paginatable is no longer part of the public API in packages.js so I had to roll my own pagination using laravel-vue-pagination. I used RenderlessPagination and styled it to look exactly like the default pagination in Nova 4.

英文:

Paginatable is no longer part of the public API in packages.js so I had to roll my own pagination using laravel-vue-pagination. I used RenderlessPagination and styled it to look exactly like the default pagination in Nova 4.

huangapple
  • 本文由 发表于 2023年5月10日 22:31:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定