Dmitri-Sintsov / djk-sample

Sample Django project for django-jinja-knockout
17 stars 4 forks source link

help/feature inlineformset real time updateing of parent model values. #8

Open bobsburgers opened 1 year ago

bobsburgers commented 1 year ago

I have created a demo where you can see the basics of what I am working on. It's under app_blue: http://127.0.0.1:8000/transaction-grid-editable/ https://github.com/bobsburgers/djk-sample/tree/blue

Feel free to close this if it's outside the scope of what you provide for the community.

I am not asking you to fix or solve this for me (unless you want to, of course).

I want to know essentially two things: First, is it an easy/correct way to implement things like this? Am I better off starting from the ground up for this form/page/view? Second, what path should I take? Do I essentially add on more to the package? Is it a template thing? https://github.com/Dmitri-Sintsov/django-jinja-knockout/

Two features I am trying to implement:

Show a live total from values in the inline formset.
Each inline model has a value field. I want to add all those fields together in real time in the main model total.

Search to add to inline formsets. Just have a search box or another way to select/add existing things rather than only create new for inline formset. I know there are some other ways to implement this, but I really want it inline creation like you already have, just with a search to add first.

I know essentially the JS that I need and the Python part, but I don't know how to connect them together on the frontend in real-time. I could do it after save on the Python side, but I want multiple mostly math things like this in real time, and I am tired of doing workarounds rather than just learning the correct modern way.

javascript


// Initialize the sub-amounts as an empty array
self.sub_amounts = ko.observableArray([]);

// Define a computed observable that calculates the total
self.total = ko.computed(function() {
    var sum = 0;

    // Loop through the sub-amounts and add them up
    ko.utils.arrayForEach(self.sub_amounts(), function(sub_amount) {
        sum += parseFloat(sub_amount.amount());
    });

    return sum;
});

PS: I tried to contact you privately about just paying you to help me, but I could not find a place. Thanks again for your great work over the years. I wish I found more ways to contribute.