简体   繁体   中英

How to bind data from two tables in PhoneJs?

I have two tables:

  • nom_articole { id(PK), denumire, nom_um_id(FK) }
  • nom_um { id(PK), simbol, denumire }

I want to display:

  • nom_articole.id
  • nom_articole.denumire
  • nom_um.simbol

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.

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
}); 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM