This is a migrated thread and some comments may be shown as answers.

Initial value with remote datasource

6 Answers 752 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Rinck
Top achievements
Rank 1
Rinck asked on 18 Jul 2013, 04:35 PM
Hi,

I just spent the last couple of hours trying to get the ComboBox to work, but no luck so far. Here's the code breakdown:
Datasource:
var event_datasource = new kendo.data.DataSource({
    transport: {
        read: {
            url: "/event/datawidget",
            dataType: "json",
            type: "POST"
        },
        parameterMap: function(data) {
            return kendo.stringify(data);
        }
    },
    schema: {
        data: "data",
        total: "total",
        model: {
            id: "event_id",
            fields: {
                event_id: { type: "number" },
                description: { type: "string" }
            }
        }
    },
    sort: { field: "description", dir: "desc" }
    pageSize: 10,
    serverFiltering: true,
    serverPaging: true
});
ComboBox:
var event_widget = $("#event_id").kendoComboBox({
    dataTextField: "description",
    dataValueField: "event_id",
    filter: "contains",
    minLength: 2,
    dataSource: event_datasource,
    placeholder: "Select an event",
    value: '99',
    text: 'Some event',
    change : function (e) {
        if (this.value() && this.selectedIndex == -1)
            this.value(null); // clears the selection when an invalid selection was made
    },
      autoBind: false
}).data('kendoComboBox');
Problem:
The initial dataset that the datasource returns does not contain value 99 (Some Event).
When I set autoBind to false then the value and text are correctly shown upon initialization.

BUT, when I simply click the little arrow on the dropdown, the data is bound, the text "Some Event" disappears, and the shown text is set to 99 (aka the value).
Even when I don't actually select another item from the combobox, it keeps showing 99.

I've tried using the datasource filter to filter the selected event id, but I cannot unset this filter afterwards (or don't know how to do this properly; everything I tried just made things worse).

Can anyone tell me what I'm doing wrong here? Also, if someone has a clean and working solution to this, I recommend adding it to the documentation because more people seem to be struggling with this (based on similar threads in this forum).

Regards,
Rinck

6 Answers, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 22 Jul 2013, 03:32 PM
Hi Rinck,

 
Could you please provide the server code that selects the records based on current request and returns the response? This would help us better understand your current scenario. 

Kind Regards,
Vladimir Iliev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Rinck
Top achievements
Rank 1
answered on 22 Jul 2013, 04:56 PM
Hi Vladimir,

I am using a heavily modified version of the datasource PHP file that is provided with the Kendo PHP wrappers. However, for simplicity's sake, I just tested it with the default datasource file, and I'm seeing the same results.

The table is a simple mysql table with, again for simplicity's sake, 2 fields:
  • event_id
  • description

The table currently contains approx. 100 records but will likely grow to a few thousand in the next few months.
Without filters, the kendo combobox simply requests 10 records from the server, sorted by description in descending order.
Now let's assume that the default value for the widget (set on page init) is:

{
    event_id: 99,
    description: "Some event"
}
But the datasource returns 10 different results (without event_id 99).

If I set the default value of the widget to 99 and set autoBind to true, I see 99 as the description/text value in the widget.
If I set autoBind to false and use the value and text properties to set the initial value/description, I see the correct text. BUT as soon as I click to open the widget, the data is bound automatically, but does not set a filter, so it receives the first 10 results only, which does not include event_id 99.

I believe this is a client side problem, not a serverside one, because when I set an initial value and then bind the data, the widget does not set a filter so the dataset returned does not contain the correct id/description pair.

Something else that I have tried is using a filter. When I set a filter manually like so:
{
    "filter": {
        "logic": "or",
        "filters":  [
            {"value": "99", "operator": "contains", "field": "event_id"}
        ]
    }
}
The only result that shows up is the default event and then when I start typing to find a different event, the filter is not removed but instead a new one is added, which doesn't render the correct results.

Any suggestion is welcome!

0
Vladimir Iliev
Telerik team
answered on 24 Jul 2013, 11:03 AM
Hi Rinck,

 
Please note that the initial value of the ComboBox should be included in the first result returned from the server in order to correctly bind it. In current case I would suggest to include additional parameter in the initial request for data(as the ComboBox doesn't populate the filter parameter in the initial request) using the data function in order to include current record in the result set. 

Kind Regards,
Vladimir Iliev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Rinck
Top achievements
Rank 1
answered on 24 Jul 2013, 11:42 AM
Hello Vladimir,

thank you for the response, however, I am wondering why this is not included by default? Sure I can add this functionality and modify the PHP Datasource provided by Kendo (PHP wrappers) to include the initial value, but not having this by default renders the default widget rather useless when using a remote datasource.

If I get around to it, I might post my own solution to this thread, but I am a little disappointed that this functionality is not included. I would really like to see this in a following release!

Maybe the json posted to the datasource should include not only the attributes take, skip, sort, etc, but also initialValue? Once the data has been bound (first time) this value can be reset to null, as it would no longer be needed.

Just my 2 cents...
0
Vladimir Iliev
Telerik team
answered on 24 Jul 2013, 01:03 PM
Hi Rinck,

 
Please note that current behavior is intended and expected  - the initial request doesn't include the filter in order to always populate the ComboBox with data and show results to the user. Current scenario is considered as a custom one and it will require custom solution, however if you believe that this behavior should be modified I would suggest to share your idea at KendoUI UserVoice to allow other users vote for it. Most voted ideas are included in next KendoUI releases.

Kind Regards,
Vladimir Iliev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Rinck
Top achievements
Rank 1
answered on 24 Jul 2013, 02:54 PM
Hi Vladimir,

although I can understand when you say this is expected on the client side, I would still expect the serverside code (PHP Wrappers) provided by Kendo to have this kind of functionality. Setting a default value for any type of widget from my point of view is an absolute basic requirement to perform CRUD operations. And since CRUD operations are the majority use-case for using Kendo widgets, I feel this should be standard functionality, not something I have to add myself, however trivial it may or may not be.

I'll consider adding it to the uservoice if I get around to it.

Regards,
Rinck
Tags
ComboBox
Asked by
Rinck
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Rinck
Top achievements
Rank 1
Share this question
or