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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742345655a4426488.html
评论列表(0条)