Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
mk09123
Contributor II
Contributor II

how to get last item from selection array in mashup

Hello 

I am using the below code for getting selection in my mashup

let fieldData = app.field('FinancialYear').getData()
console.log(fieldData.rows)
console.log(fieldData.rows.length) // getting 0 everytime

mk09123_0-1676027342736.png

 

So the issue is whenever I run the above code I am getting 0 lengths (cause I think that code gets run before the array is found )

so what I am trying to do is I want the last value of the array (2022-2023)

But after the dom load, I am getting 0 that the issue.

any help??

Labels (1)
8 Replies
SerhanKaraer
Creator III
Creator III

Hello mk,

getdata is an async operation, so you have to wait for it to end.

Use help site:

https://help.qlik.com/en-US/sense-developer/November2022/Subsystems/APIs/Content/Sense_ClientAPIs/Ca...

You can find a lot of stuff on this forum: Integration, Extension & APIs

mk09123
Contributor II
Contributor II
Author

Hello SerhanKaraer,

Thanks for reply

As per the example which you share I have added below code in Js file.

 var fieldData = app.field('FinancialYear').getData();
		fieldData.OnData.bind( function(){
			fieldData.rows.forEach(function(row){
			console.log(row.qElemNumber)	
		})
	})
		
fieldData.field('FinancialYear').select([0,1,2],true,true)

But now I am getting below error 

mk09123_0-1676265419041.png

 

 

 

 

 

 

SerhanKaraer
Creator III
Creator III

Hi,
There seems a problem with the documentation.

It must be like this:

 var fieldData = app.field('FinancialYear').getData();
		fieldData.OnData.bind( function(){
			fieldData.rows.forEach(function(row){
			console.log(row.qElemNumber)	
		})
	})
		
app.field('FinancialYear').select([0,1,2],true,true)
mk09123
Contributor II
Contributor II
Author

Hello SerhanKaraer,

Thanks for reply

 

   var fieldData = app.field('FinancialYear').getData();
   
		fieldData.OnData.bind( function(){
		let value =[];
		fieldData.rows.forEach(function(row){		
			value.push(row.qText)
		})
		console.log([value.length -1 ])
		app.field('FinancialYear').select([value.length -1 ],true,true)
	})
	

mk09123_0-1676277763787.png

 

Now I am getting the last array value in the console and in selection as well 

But now the console and selection value getting triggered each sec (so when you remove the selection it's get added automatically)

SerhanKaraer
Creator III
Creator III

Hi,

When selection state changes on the server, the clients are updated with new selection state. This is the default behavior of QS. If you do not what to be updated, you have to unbind the listener as below:

 

var fieldData = app.field('FinancialYear').getData();

let value =[];

var listener = function() {
   fieldData.rows.forEach(function(row){		
      value.push(row.qText)
   })
   console.log([value.length -1 ])
   fieldData.OnData.unbind( listener );
}

fieldData.OnData.bind( listener )
app.field('FinancialYear').select([value.length -1 ],true,true)

 

If you want to be updated, just comment the line with unbind method.

mk09123
Contributor II
Contributor II
Author

Hello SerhanKaraer,

Try with your code, but somehow it's not working as expected 

Not getting any selection or console after adding the code 

Thanks in advance

 

 

 

SerhanKaraer
Creator III
Creator III

Just corrected parentheses and braces @mk09123 

mk09123
Contributor II
Contributor II
Author

Hi SerhanKaraer,

 

Can you please correct and share the same 

cause I tried but it's not working