admin管理员组文章数量:1332968
I have a form that on submit does not trigger an action on sveltekit server but instead I have a handleSubmit function on the svelte module for that.
<script>
export let data;
let parentObject = writable({
name: '',
childObjects: []
});
function addChildObject() {
parentObject.update(s => {
s.childObjects.push({ name: '', something: '' });
return s;
});
}
async function handleSubmit(event) {
event.preventDefault();
parentObject.subscribe(data => {
....
</script>
<form class="parent-object-form" method="POST" on:submit={handleSubmit}>
...
<button type="button" on:click={addChildObject}>
Add Child Object
</button>
<button type="submit">Submit</button>
</form>
I specifically don't have a server action because I dynamically modify the form by adding and removing fields from the object.
Whenever I submit this, I want the load
function in the +page.server.js server file to be called again, because I also need to render a list of parentObjects that I fetch from a server.
<table>
<tbody>
{#each data.parentObejct as parent}
...
{/each}
I need this list to get updated after the form is submitted. How do I do that?
Is there a way to manually trigger the load
function after the submit?
Should I manually add the new object to the list on the handleSubmit
func?
Is there a way to manually pass this dynamic data to a server action?
I have a form that on submit does not trigger an action on sveltekit server but instead I have a handleSubmit function on the svelte module for that.
<script>
export let data;
let parentObject = writable({
name: '',
childObjects: []
});
function addChildObject() {
parentObject.update(s => {
s.childObjects.push({ name: '', something: '' });
return s;
});
}
async function handleSubmit(event) {
event.preventDefault();
parentObject.subscribe(data => {
....
</script>
<form class="parent-object-form" method="POST" on:submit={handleSubmit}>
...
<button type="button" on:click={addChildObject}>
Add Child Object
</button>
<button type="submit">Submit</button>
</form>
I specifically don't have a server action because I dynamically modify the form by adding and removing fields from the object.
Whenever I submit this, I want the load
function in the +page.server.js server file to be called again, because I also need to render a list of parentObjects that I fetch from a server.
<table>
<tbody>
{#each data.parentObejct as parent}
...
{/each}
I need this list to get updated after the form is submitted. How do I do that?
Is there a way to manually trigger the load
function after the submit?
Should I manually add the new object to the list on the handleSubmit
func?
Is there a way to manually pass this dynamic data to a server action?
1 Answer
Reset to default 0A form can be as dynamic as you want, that does not mean you cannot use form actions. You also can add things to the submitted form data via the enhance
action.
If you use enhance
, that already will trigger the load
function, if you do not use it, you can manually call invalidateAll
instead.
本文标签: sveltekitCall load function after form submitStack Overflow
版权声明:本文标题:sveltekit - Call load function after form submit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742345655a2457466.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论