Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to work with select input with data coming from a simple relational db model
    primarykey
    data
    text
    <p>I'm using Angularjs plugged into a API which retrieves data from a mysqlk normal relational db.</p> <p>Let's say I have this simple data model in my bdd:</p> <pre><code>table car: id, type_id table type: id, label </code></pre> <p>I have a API which retrieves the data from DB to have the list of cars and the static labels from db:</p> <pre><code>http://myapi/car/ response : {[{id:1, type_id:2}, {id:2, type_id:3}]} http://myapi/carstaticlabel/ response : {[{id:1, label:convertible}, {id:2, label:limousine}, {id:3, label:pickup}]} </code></pre> <p><strong>My aim is first to display a list of cars with the type label and then, open a dialog and being able to show a form with preloaded values of the selected car:</strong></p> <p>rendered list of cars :</p> <pre><code>1 convertible 2 pickup </code></pre> <p>rendered form for edited car whose id is 1: select the type of car:</p> <pre><code>&lt;select&gt; &lt;option value="1"&gt;convertible&lt;/option&gt; &lt;option value="2" selected="selected"&gt;limousine&lt;/option&gt; &lt;option value="3"&gt;pickup&lt;/option&gt; &lt;/select&gt; </code></pre> <hr> <p>I have tried different approaches but none is elegant</p> <p><strong>Solution 1</strong></p> <p>for the list of cars change my API and make the join directly between car and type to get the list with label:</p> <pre><code>http://myapi/car/ response : {[{id:1, type_id:2, type:{[id:2, label:limousine]}}, {id:2, type_id:3, type:{[id:3, label:pickup]}}]} </code></pre> <p>then I just assign in the form : car.id and car['type']['label']</p> <p>ISSUE: each car will contain repeated information (label) >> bloated info</p> <p>for the edit form: I pass the json of selected car to the form and set default values:</p> <pre><code>&lt;p&gt;type: &lt;select ng-model="car.type" ng-options="type.label for type in type_list" required&gt;&lt;/select&gt;&lt;/p&gt; </code></pre> <p>MY ISSUE with that:</p> <p>when I submit the form, I don't get the type_id directly, I get the car.type object instead, so I need to painfully translate it into an id to post into the database. To convert I have to go into the car.type object and retrieve the object inside and then retrieve its id... very unelegant.</p> <hr> <p><strong>Solution 2</strong></p> <p>make the "join" inside angular by working with arrays.</p> <p>ISSUE: very hard to default values in the form later on</p> <hr> <p><strong>I'm lost, what is usually the best practice to achieve that simple task?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload