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

How to select ListView item by it's "ID" in the model

13 Answers 2170 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Ranjit
Top achievements
Rank 1
Ranjit asked on 23 Aug 2012, 05:50 PM
After reading all documentation and the posts in the forums, It seems as noone has asked or posted anything regarding this functionality.

I basically want to  "Select ListviewItem where AccountID = ID"

How is this accomplished?


13 Answers, 1 is accepted

Sort by
0
Ranjit
Top achievements
Rank 1
answered on 23 Aug 2012, 08:10 PM
I figured out a method to do this, although it's not a very clean work around.

here is the code in case anyone is interested... I am still curious as to whether this is possible without manually walking through the child collection.


function SetSelectedAccountByID(id) {
        var listView = $("#Accounts-listview").data("kendoListView");
        var children = listView.element.children();
        var index = 0;
        for (var x = 0; x < children.length; x++) {
            if (listView.element.children()[x].id == id) {
                index = x;
            };
        };
        
        // selects first list view item
        listView.select(children[index]);
    };
0
Daniel
Top achievements
Rank 1
answered on 01 Sep 2014, 08:45 AM
Why is an easy task as this one so difficult in Kendo?
This has to be a built-in function!

Regards,
Daniel
0
Dimiter Madjarov
Telerik team
answered on 01 Sep 2014, 09:12 AM
Hello Daniel,


By design the selection is performed over DOM elements. You could use the get() method of the dataSource to retrieve the exact dataItem via it's ID and then retrieve the DOM element via the uid attribute.

Let me know if I could provide further assistance.

Regards,
Dimiter Madjarov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Daniel
Top achievements
Rank 1
answered on 01 Sep 2014, 09:51 AM
Hello Dimiter,

I think this tasks are daily business for most developers. Why we are buying libraries such as Kendo? We don't want to use "Low Level API's" for basic tasks.

One more point is, that Kendo is here and there a little bit inconsistent in its API.
E.g. TreeView has a method .findByUid(). Why not ListView too!?

Why don't have both (and other datasource) controls a method like .findById() ?


Regards,
Daniel
0
Dimiter Madjarov
Telerik team
answered on 01 Sep 2014, 10:17 AM
Hello Daniel,


All widgets that internally use a dataSource can access it and invoke it's getByUid method.
E.g.
var lv = $("#listview").data("kendoListView");
lv.dataSource.getByUid(...);

If you consider that we should add more methods to the ListView API, you could post this suggestion in our UserVoice portal. If the idea is popular among our users, we will consider to implement it in future releases.

Regards,
Dimiter Madjarov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Daniel
Top achievements
Rank 1
answered on 01 Sep 2014, 12:33 PM
You are correct, but I'm looking for the respective ListView-Item (for selecting)!

Following returns the corresponding data item. I'm looking for the list-view item!
lv.dataSource.getByUid(...);

Now I do this:
var listViewItem = lv.element.children("[data-uid='" + lv.dataSource.get(id).uid + "']");
lv.select(listViewItem);

I'm missing a method like in TreeView to do at least this:

lv.select(lv.findByUid(lv.dataSource.get(id).uid));

.findByUid doesn't exist on ListView, but on TreeView (this is inconsistent)

I think this should be (built-in Kendo):

lv.select(lv.findById(id));


Regards
Daniel

PS: Be aware of using Uid and Id in my snippets
0
Dimiter Madjarov
Telerik team
answered on 01 Sep 2014, 01:09 PM
Hi Daniel,


Yes, this approach is correct for retrieving the corresponding DOM element via id and then selecting it.

Thank you for the suggestion about the findByUid method. As stated previously, I would recommend to also post it in our User Voice portal.

Regards,
Dimiter Madjarov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Peter
Top achievements
Rank 1
answered on 15 Aug 2016, 08:28 AM

The code required to select a ListView as provided here is just outrageously dumb. I'm sorry.

Please don't ask us to vote for a feature that common sense already requires.

Kendo should fix the API, as sensibly described in the posts here, of their own accord.

It is your reputation, and the quality of your product. It is not our responsibility to beg you to fix it.

0
Dimiter Madjarov
Telerik team
answered on 16 Aug 2016, 07:49 AM

Hello Peter,

The reasoning behind this approach is that the ListView selection relies only on DOM elements. If any item should be retrieved via it's ID, this should be achieved through the DataSource API.

Regards,
Dimiter Madjarov
Telerik by Progress
 
Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
 
0
Mark
Top achievements
Rank 1
Veteran
answered on 03 Aug 2017, 09:51 PM

This works for me:

// selects a list view item
function selectListViewItem(selector, id) {
    var listView = $(selector).data("kendoListView");
    var listViewItem = listView.element.children("[data-uid='" + listView.dataSource.get(id).uid + "']");
    listView.select(listViewItem);
}
0
Mark
Top achievements
Rank 1
Veteran
answered on 03 Aug 2017, 09:56 PM
The issue I keep running into is selecting an item (e.g. one just added) after refreshing the datasource. I have to select the new item in the dataBound event but don't have a clean way of passing the model's id of the new one just added to this event.
0
Milena
Telerik team
answered on 08 Aug 2017, 02:47 PM
Hello,

It is possible to select the new item as handle save event of ListView and access uid in it.
Here, you can find a sample, which demonstrates this approach.

Regards,
Milena
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Mark
Top achievements
Rank 1
Veteran
answered on 08 Aug 2017, 03:30 PM

Interesting, thanks!

-mb

Tags
ListView
Asked by
Ranjit
Top achievements
Rank 1
Answers by
Ranjit
Top achievements
Rank 1
Daniel
Top achievements
Rank 1
Dimiter Madjarov
Telerik team
Peter
Top achievements
Rank 1
Mark
Top achievements
Rank 1
Veteran
Milena
Telerik team
Share this question
or