Skip to Content

How to extract the data from JSONModel by condition ?

How to get the data from JSONModel by condition value , just the process like ABAP read table tb_1 with key = condition value or SQL Select query?

e.g:

var dataUser = [{'usr_id':'001','user_name': 'usr1','status':'A' },{'user_id':'002','user_name':'user_2','status':'D'}];

how to get the user_id = 002 data by user_name = 'user_2', like the abap read table.

can anybody give me some code example or guide me to to this? thanks.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    Posted on Jul 07, 2016 at 03:54 PM

    Hi Wei,

    First, you have to retrive the Array from your JSONModel using following method.

    var oArray = this.getView().getModel("modelName").getProperty("/rootStringValue");

    Then you have to loop through both array and JS object. In your case, the dataUser is already an Array.

    var i, name, obj, targetIndex;

    for (i=0; i < dataUser.length; i++) {

    obj = dataUser[i];

    for (name in obj) {

    if ( name == "user_name" && obj.name == "user_2")

    targetIndex = i;

    }

    }

    via this targetIndex, you can locate the right array element contains the information and you can retrieve what you want.

    var targetObj = dataUser[targetIndex];

    var id = targetIndex.user_id;


    I think this id is namely what you I want. My code is not optimized, but I think the logic is right. You can give it a try.


    Regards,

    La

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 07, 2016 at 02:23 PM

    Hi Wei,

    Depending on the control you are binding on, most likely you'll have to apply filtering.

    You can do this, on for example a table, using this following guide:

    http://www.saplearners.com/grouping-filtering-and-sorting-in-sap-m-table/

    What control are you trying to bind your data on?

    (You usually define your filters in the binding)

    Greets,

    Caspar

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.