2

I have a table containing this kind of information enter image description here

I am using knockout js and putting all data on a array and putting it on this table like this.

self.allchildrendata = ko.observableArray();
self.viewAllUserChildrenCard = function () {

            $.ajax({
                type: 'POST',
                url: BASEURL + '/index.php/main/childrenCardInfo',
                contentType: 'application/json; charset=utf-8',
                dataType :'json'
            })
            .done(function(childrencards) {
                self.allchildrendata.removeAll();
                $.each(childrencards, function (index, childrencard) {
                self.allchildrendata.push(childrencard);
                console.log(self.allchildrendata());
                });
            })
            .fail(function(xhr, status, error) {
                alert(status);
            })
            .always(function(data){                 
            });
        };
        self.viewAllUserChildrenCard();

So next I want to click the add money button for rebecca and want to send the orig id of only rebecca so I can use it to find her in the database and add money, But i am not sure on how to send the orig_id, I tried this way.

self.giveCashtoChild = function(){
            $.ajax({
                type: 'POST',
                url: BASEURL + '/index.php/main/addUserChildrenCash' + "/" + self.allchildrendata().orig_id ,
                contentType: 'application/json; charset=utf-8'
            })
            .done(function() {

            })
            .fail(function(xhr, status, error) {
                alert(status);
            })
            .always(function(data){                 
            });
        } 

Here is the html code where I have a databind on each of the buttons so I can send the orig id .

<tbody data-bind="foreach: allchildrendata">
                    <tr>
                    <td class="text-center"><span data-bind="text : $data.children_name"></span></td> 
                    <td class="text-center"><span data-bind="text : $data.daugther_son"></span></td>
                    <td class="text-center"><span data-bind="text : $data.amount"></span> $</td>
                    <td class="text-center"><a href="#" data-bind="click : $root.giveCashtoChild"><span class=" glyphicon glyphicon-send"></span></a></td>
                    <td class="text-center"><a href="<?php echo base_url(); ?>index.php/main/takeAway"><span class=" glyphicon glyphicon-trash"></span></a></td>
                    </tr>
            </tbody>

So basically I need help to identify which family memeber I am clicking and sending that family memebers orig_id

1 Answer 1

1

Whenever you use a click binding, knockout passes the current binding's data and event.

So in your HTML:

<a href="#" data-bind="click : $root.giveCashtoChild">

It calls giveCashToChild with two arguments. Your giveCashToChild method should thus accept two arguments, of which the first will be the child to give cash to.

self.giveCashtoChild = function(data, event) {
  var currentChildId = data.orig_id;
  // the other stuff..
};
Sign up to request clarification or add additional context in comments.

6 Comments

Tried it, did not work, I posted another question similar to this one, where I fixed it like 90% but ajax request is not going... Please check it out stackoverflow.com/questions/36221510/…
Dit you also replace self.allchildrendata().orig_id with currentChildId ?
Your question was about identifying which family member is clicked by the user. Can you confirm that in my example, currentChildId holds the id of the clicked family member? Things might still go wrong, but I do expect this part to work... Any error logs?
Yup your answer works, so I am accepting it, but doesnt send the information to the controller.
Look at what I posted . you have access to all data of your child. this way every child has its own model . they are separate entity.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.