Attila
Attila

Reputation: 444

How to bind data from two tables in PhoneJs?

I have two tables:

I want to display:

My html code:

<div data-options="dxView : { name: 'nom_articoleDetails', title: 'Nom_articole' } " >
<div  data-options="dxContent : { targetPlaceholder: 'content' } " >
    <div data-bind="dxScrollView: { }">
        <h2 data-bind="text: nom_articole.denumire"></h2>
        <div class="dx-fieldset">
            <div class="dx-field">
                <div class="dx-field-label">ID</div>
                <div class="dx-field-value" data-bind="text: nom_articole.id"></div>
            </div>
            <div class="dx-field">
                <div class="dx-field-label">Denumire</div>
                <div class="dx-field-value" data-bind="text: nom_articole.denumire"></div>
            </div>
            <div class="dx-field">
                <div class="dx-field-label">U.M.</div>
                <div class="dx-field-value" data-bind="text: ????????????????"></div>
            </div>
        </div>
        <div data-options="dxContentPlaceholder : { name: 'view-footer', transition: 'none' } " ></div>
    </div>
</div>

My Javascrip code:

MobileApplication.nom_articoleDetails = function(params) {
return {
    id: params.id,
    //nom_um: new MobileApplication.nom_umViewModel(),
    nom_articole: new MobileApplication.nom_articoleViewModel(),

    handleDelete: function() {
        DevExpress.ui.dialog.confirm("Are you sure you want to delete this item?", "Delete item").then($.proxy(function (result) {
            if (result)
                this.handleConfirmDelete();
        }, this));
    },

    handleConfirmDelete: function() {
        MobileApplication.db.nom_articole.remove(params.id).done(function() {
            MobileApplication.app.navigate("nom_articole", { target: "back" });
        });
    },

    viewShown: function() {
        nom_articoleDetails = this;
        MobileApplication.db.nom_articole.byKey(params.id).done(function(data) {
            nom_articoleDetails.nom_articole.fromJS(data);
        });
    }
};

This code was generated by Devexpress multi-channel application wizard.

Upvotes: 2

Views: 854

Answers (1)

amartynov
amartynov

Reputation: 4175

From the information you gave, and provided that nom_articole and nom_um share the same key, the code would be:

viewShown: function() {
    // ... your code ...

    MobileApplication.db.nom_um.byKey(params.id).done(function(data) {
        nom_articoleDetails.nom_um.fromJS(data);
    });
}

You uncomment the nom_um member and use text: nom_um.simbol as the binding text in the view.

Update:

To load an object by foreign key, you have two options. The first is cascading fetches:

MobileApplication.db.nom_articole.byKey(19).done(function (data) { 
    nom_articoleDetails.nom_articole.fromJS(data); 

    MobileApplication.db.nom_um.byKey(data.nom_um_id).done(function(data) {
        nom_articoleDetails.nom_um.fromJS(data);
    });
}); 

The second will work if your data service is properly configured OData. In this case you may use the expand feature:

MobileApplication.db.nom_articole.byKey(19, { expand: "nom_um" }).done(function (data) { 
    // data will contain a nested object
}); 

P.S. DevExpress provides Support service, and in case you cannot find the right answer here, you can ask them and share the solution.

Upvotes: 1

Related Questions