简体   繁体   中英

How do I set the default value for a Dojo FilteringSelect using a JsonRest Store

I've successfully setup my FilteringSelect to query my server for items dynamically using the JsonRest store. My problem is when I try to initially set the dropdown with specific value.

&ltdiv id="ddThings"></div>
&ltscript type="text/javascript">

    dojo.ready(function () {

            var storeThings = new dojo.store.JsonRest({ target: "/Things/" });
            var ddThings = new dijit.form.FilteringSelect({
                name: "thingId",
                searchAttr: "name",
                autoComplete: true,
                value: "5",
                missingMessage: "This is required",
                placeHolder: "Select a Thing",
                store: dojo.data.ObjectStore({ objectStore: storeThings })
        }, "ddThings");

        ddThings.startup();

        //ddThings.set("value", "5");

    });

When I set value to "5" or using ddThings.set I can see the filteringselect querying my server and passing in an "id" value. I return a collection of things with the single item in it. I expected it to populated the filteringselect with the item I returned. But, instead nothing happens.

The returned JSON looks like this

[
   {
      "id":"5",
      "name":"Example"
   }
]

One interesting note is that if I set "displayedValue" to "Example" (whatever the name is) then I can see it hit the server, find a match and return it and it actually populates the dropdown with the matching item. I want this behavior but by using the value/id of the item, not the display/name of it.

When the JsonRest Store makes a request for a specific item by its id the response must be a single item instead of a collection.

{
   "id":"5",
   "name":"Example"
}

dijit.form.FilteringSelect store is expecting a JSON object that looks like this.

{ "identifier":"id"
, "label":"name"
, "items":
    [
        {"id":"9","name":"Alberta"}
      , {"id":"4","name":"New Brunswick"}
      , {"id":"11","name":"Northwest Territories"}
    ]
}

The store must implement the dojo.data.api.Identity

http://dojotoolkit.org/reference-guide/dojo/data/api/Identity.html#dojo-data-api-identity

From the Dojo documentation:

note: When using a DataStore with FilteringSelect, the store must implement the Identity API.

http://livedocs.dojotoolkit.org/dijit/form/FilteringSelect

You can use a dojo.store.DataStore as an adapter while all the widgets haven't been updated to the new dojo store api.

See http://livedocs.dojotoolkit.org/dojo/store/DataStore

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