How to process maps (key-value) type value?

Hi!

  1. In edit mode: using the JSX and AJAX I pass the selected JSTree nodes to the page:

    require([‘jquery’, “$!services.webjars.url(‘org.xwiki.platform:xwiki-platform-tree-webjar’, ‘require-config.min.js’, {‘evaluate’: true})”], function($) {
    var hdwType = “$escapetool.javascript($request.getParameter(‘prmHdwType’))”;
    require([‘tree’], function($) {
    $(’.xtree’).on(“changed.jstree”, function (e, data) {
    var i, j, treeSelectedValue = [];
    for(i = 0, j = data.selected.length; i < j; i++) {
    var selId = data.instance.get_node(data.selected[i]).id ;
    var selText = data.instance.get_node(data.selected[i]).text;
    var SetItem = $(this).map(function() {return selId + “:” + selText}).get().join();
    treeSelectedValue.push(SetItem);
    };
    sendTreeSelectedValue(treeSelectedValue);
    });

    function sendTreeSelectedValue(value) {
    var docUrl = “$escapetool.javascript($request.getParameter(‘prmDoc’))”;
    var UrlParams = {‘xpage’: ‘plain’,‘outputSyntax’: ‘plain’, ‘hdwTypeValue’: JSON.stringify(value)};
    new Ajax.Request(docUrl, {
    parameters: UrlParams,
    onSuccess : function(response) {}
    });
    }
    });
    });

  2. On the page I receive the data from the request as map:

    $doc.set(‘hdwType’, $request.getParameterMap(“hdwTypeValue”))
    $doc.save()

  3. In view mode I want to display data as a list / table:

    #set ($valStrProc = {})
    #set ($empty = $valStrProc.put($value.replace(’"’,’’).replace(’[’,’{’).replace(’]’,’}’)))
    #foreach($item in $valStrProc.entrySet())
    |$item.key|$item.value|
    #end

But code from p.3 does not work.
Where am i wrong? How to do it correct?

Thanks…

Try $jsontool.fromString($value). See https://extensions.xwiki.org/xwiki/bin/view/Extension/Velocity+Module#HVelocityTools

1 Like

Ok, thanks, that’s a lot better.
Next question: data is stored as an array of pairs key-value and converted to string correctly, but what if I want to, for example, only display a Text without Key? How to Array convert to Map?

Terrible working solution:

#set ($valStrProc = $jsontool.fromString($value))

#foreach($item in $valStrProc)
  $item.split(":")[1].replace("'","")
#end

Is it possible to improve it?

Print

$valStrProc.class.name

and look for the corresponding JavaDoc to find what methods (API) you can use to access the data.

1 Like