Modifying JSON in DataSrc so I can use searchPane with arrays

Modifying JSON in DataSrc so I can use searchPane with arrays

east1999east1999 Posts: 31Questions: 11Answers: 0

Hi everyone,

I've been looking at the searchPane experiment and tried to use it with my table. The complicated part is I have some fields with arrays — each row "entry" can have multiple authors or tags. I want to use searchPane to bring up the totals for each discrete object and not just a list of arrays.

Here's a sample of my JSON:

{
        "id": "http://zotero.org/groups/###/items/#######",
        "type": "article-journal",
        "title": "Example title",
        "container-title": "Book title",
        "page": "17-40",
        "issue": "9",
        "URL": "example-url",
        "ISSN": "0000-0000",
        "author": [
            {
                "family": "Lastname",
                "given": "Firstname"
            }
        ],
        "issued": {
            "date-parts": [
                [
                    "2007"
                ]
            ]
        },
        "keyword": "Tag1, Tag2, Tag3"
    }

Where's what I've tried using DataSrc:

  "ajax": {
    "url": "index.json",
    "dataSrc": function (json) {
      var return_data = new Array();
      for(var i=0;i< json.length; i++){
        var type = json[i].type;
        var date = json[i].issued;
        var key = json[i].keyword;
        var author = json[i].author;
        var editor = json[i].editor;
        var title = json[i].title;
        if (author != null) {var authors = $.map( author, function( d, i ) {return d.given + " " + d.family;}).sort().join(", ");}
        if (editor != null) {var editors = $.each( editor, function( d, i ) {return d.given + d.family;}).sort().join(", ");}
        return_data.push({
          'title': title,
          'date': date,
          'type': type,
          'authors': authors,
          'editors': editors,
          'key': key.split(",")
        })
        }
      console.log(return_data);
      return return_data;
    }}

This at least renders the elements in both the pane and the table, but doesn't give me what I want. Furthermore, as you can see above, I've tried to split the "keyword" (tag) field to an array, but it simply doesn't show up at all.

Any clues? Thank you in advance.

This question has an accepted answers - jump to answer

Answers

  • east1999east1999 Posts: 31Questions: 11Answers: 0

    I realise now that this could be a pointless question, as DT works from the given rows, and unfolding the data in more rows will break the table. My question about the overview remains, though: is there a way to extract a list of values from a column, and sort it by frequency?

  • allanallan Posts: 62,338Questions: 1Answers: 10,228 Site admin
    Answer ✓

    Hi,

    Great question. I'm afraid that is not something that search pane currently does though. Its data binning only works on scalars at the moment, so you'd need to modify that code to detect if an array was passed in as the data or not, and then do an inner loop on that data.

    I've added that to the list of features that have been requested for SearchPane moving forward.

    Allan

  • east1999east1999 Posts: 31Questions: 11Answers: 0

    Thank you so much for the reply and your continued work!

    I really wouldn't know how exactly to modify your code. Although a finalised searchPane plugin would fit my use case perfectly, I wonder if at the moment there's any sort of shortcut — for instance, a separate library that would look at my json, count occurrences, and present a sorted list. I could then use the API to link each entry to DT searches.

  • allanallan Posts: 62,338Questions: 1Answers: 10,228 Site admin

    I'm not aware of any. I think the code for what you would is still to be written.

    Allan

This discussion has been closed.