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

Compare data in multiple selected rows?

11 Answers 723 Views
Grid
This is a migrated thread and some comments may be shown as answers.
jeff
Top achievements
Rank 1
jeff asked on 18 Feb 2015, 03:01 PM
Hi! I need to compare the data in a specific field in selected rows in order to disable/enable some external buttons. Initially, I only needed to enable the buttons based on whether rows were selected or not, so it was pretty trivial:

1.if (selected.length == 0) {
2.$('#fancyButton1, #fancyButton2').attr('disabled', 'disabled');
3.} else if (selected.length > 0) {
4.$('#fancyButton1, #fancyButton2').attr('disabled', false);
5.}


Now, I need to compare the numeric values in a specific field, and if they are all equal, THEN enable the buttons.. can someone provide an example?

11 Answers, 1 is accepted

Sort by
0
jeff
Top achievements
Rank 1
answered on 19 Feb 2015, 11:59 AM
Ok, I'm fairly sure I know what direction to head here, BUT: if I have data field called: 

#: frikkinNumber #

How do I call to that from a function?
0
jeff
Top achievements
Rank 1
answered on 19 Feb 2015, 02:00 PM
Ok, sure is lonely in here... 

Just doing it with jQuery.. but again, if I have this jquery to grab the text:

1.$('#myTable a.frikkinNumericString')).text();


How do I indicate it's from the selected row?
0
Dimiter Madjarov
Telerik team
answered on 20 Feb 2015, 07:52 AM

Hello Jeff,

You could use the dataItem method of the Grid API to retrieve the model associated with each row. Here is a sample implementation - in the change event I am checking the Freight field of all selected rows.

I wish you a great day!

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
jeff
Top achievements
Rank 1
answered on 20 Feb 2015, 01:07 PM
Hi Dimiter,
Thanks for the demo, that helped in other ways as well. Here's my problem (one of many, I assure you!) - I need to compare that set of values throughout ALL of the selected rows to each other to determine if they are all the same or not, I'm doing roughly this, assuming the dataItem in question is called 'myFancyNumber':

01.if (selected.length > 1){
02.var selectedRows = $("#myTable").data("kendoGrid").select();
03. 
04.function allValuesSame() {
05.    for (var i = 0; i < selectedRows.length; i++)
06.    {
07.        if(this[i] != this[0])
08.        return false;
09.    }
10.    return true;
11.    }
12. 
13.var myFancyNumber = this.dataItem(this.select()).myFancyNumber
14.if (allValuesSame(myFancyNumber) === true) {
15.    alert(myFancyNumber);
16.}
17. 
18.}

I don't have it; a) I haven't figured out how to distinguish the selected rows from each other and b) not 100% sure that function is correct. Thoughts?
0
jeff
Top achievements
Rank 1
answered on 20 Feb 2015, 02:23 PM
I need to compare that set of values throughout ALL of the selected rows to each other to determine if they are all the same or not, I'm doing roughly this, assuming the dataItem in question is called 'myFancyNumber':

NOTE: Actually, after posting the previous code, I realized was a non-functional mess; this is my entire 'change:' function:

01.change: function(e) {
02.    var selectedRows = this.select();
03.    var selectedDataItems = [];
04.    for (var i = 0; i < selectedRows.length; i++) {
05.      var dataItem = this.dataItem(selectedRows[i]);
06.      selectedDataItems.push(dataItem);
07.    }
08. 
09.    var selected = $.map(this.select(), function(item) {
10.       return $(item).text();
11.    });
12. 
13.function allValuesSame() {
14.    for (var i = 1; i < selectedRows.length; i++)
15.    {
16.        if(this[i] != this[0])
17.        return false;
18.    }
19.    return true;
20.    }
21. 
22.if (selected.length > 1){
23.    var selectedRows = $("#myTable").data("kendoGrid").select();
24.    var fancyNumberText = this.dataItem(this.select()).fancyNumber
25.    if (allValuesSame(fancyNumberText) === true) {
26.        alert(fancyNumberText); //just testing to see what I get
27.    }
28.    return allValuesSame(fancyNumberText);
29.    }
30. 
31.    if (selected.length == 0) {
32.    $('#fancyButton').attr('disabled', 'disabled');
33.} else if (selected.length == 1) {
34.    $('#fancyButton').attr('disabled', false);
35.} else if (selected.length > 1 && allValuesSame == true) {
36.    $('#fancyButton').attr('disabled', false);
37.}

Again, a) I haven't figured out how to distinguish the selected rows from each other and b) not 100% sure that function is correct. Thoughts?
0
jeff
Top achievements
Rank 1
answered on 20 Feb 2015, 02:26 PM
the code above is a MESS (noticed after posted), so here's my complete 'change:' function:

01.  change: function(e) {
02.    var selectedRows = this.select();
03.    var selectedDataItems = [];
04.    for (var i = 0; i < selectedRows.length; i++) {
05.      var dataItem = this.dataItem(selectedRows[i]);
06.      selectedDataItems.push(dataItem);
07.    }
08. 
09.    var selected = $.map(this.select(), function(item) {
10.return $(item).text();
11.});
12. 
13.function allValuesSame() {
14.    for (var i = 1; i < selectedRows.length; i++)
15.    {
16.        if(this[i] != this[0])
17.        return false;
18.    }
19.    return true;
20.    }
21. 
22.if (selected.length > 1){
23.    var selectedRows = $("#myTable").data("kendoGrid").select();
24.    var fancyNumberText = this.dataItem(this.select()).fancyNumber
25.    if (allValuesSame(fancyNumberText) === true) {
26.        alert(fancyNumberText); //just testing to see what I get
27.    }
28.    return allValuesSame(fancyNumberText);
29.    }
30. 
31. 
32. 
33.    if (selected.length == 0) {
34.    $('#fancyButton').attr('disabled', 'disabled');
35.} else if (selected.length == 1) {
36.    $('#fancyButton').attr('disabled', false);
37.} else if (selected.length > 1 && allValuesSame == true) {
38.    $('#fancyButton').attr('disabled', false);
39.}
40. 
41.  },

Again, I need to compare that set of values throughout ALL of the selected rows to each other to determine if they are all the same or not, I'm doing roughly this, assuming the dataItem in question is called 'myFancyNumber'... 

a) I haven't figured out how to distinguish the selected rows from each other and b) not 100% sure that function is correct. Thoughts?
0
Dimiter Madjarov
Telerik team
answered on 20 Feb 2015, 03:26 PM

Hello Jeff,

After you have retrieved the dataItems associated to the Grid rows, you could use their ids to distinguish them if this is needed in the current case.

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
jeff
Top achievements
Rank 1
answered on 20 Feb 2015, 03:29 PM
[quote]Dimiter Madjarov said:

After you have retrieved the dataItems associated to the Grid rows, you could use their ids to distinguish them if this is needed in the current case.[/quote]

Great, thanks, can you walk me through how?

0
Dimiter Madjarov
Telerik team
answered on 23 Feb 2015, 09:08 AM

Hello Jeff,

The approach for retrieving the id is the same as the one in my first post.
E.g.

selected.each(function(){
  var model = grid.dataItem(this);
  var id = model.OrderID;
});

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
jeff
Top achievements
Rank 1
answered on 24 Feb 2015, 02:53 PM
Ok, I guess I'm not articulating this well; in your example, you are determining whether a value is less than 10. This works as expected, however, in this case, I am trying to determine if that value is equal or not to that same value in other selected rows; make sense?

0
Accepted
Dimiter Madjarov
Telerik team
answered on 25 Feb 2015, 11:47 AM

Hello Jeff,

Generally this is a custom logic related to the specific scenario, but for convenience I implemented a sample solution that you could use as a base. Here is the updated example. You could use it as is and modify it further if needed by the current case.

Regards,
Dimiter Madjarov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
jeff
Top achievements
Rank 1
Answers by
jeff
Top achievements
Rank 1
Dimiter Madjarov
Telerik team
Share this question
or