Granular manipulation of array variables in Datatables

后端 未结 2 1212
后悔当初
后悔当初 2021-01-21 19:41

You can see in this fiddle that I\'m looking for a solution that lets me join a couple of JSON fields in the same Datatables column. This is the solution I foun

相关标签:
2条回答
  • 2021-01-21 19:51

    This is more elegant if you HAVE to specify which items you want

    function cFL(string) {
      return string.charAt(0).toUpperCase() + string.slice(1);
    }
    
    
    "render": function(data, type, full) {
      var x = [], y=["container-title","volume","issue","page"];
      for (var i=0;i<y.length;i++) { 
        if(full[y]) x.push(cFL(y[i])+": "+full[y]);
      }
      return x.length==0?"":x.join(", ");
    }
    
    0 讨论(0)
  • 2021-01-21 19:57

    Your approach does work, check this JSFiddle (https://jsfiddle.net/annoyingmouse/0zdjy1yz/6/). What you were running into was not being able to find the relevant data for issue as it's not in your data, if you do a check before adding it to the array you're laughing:

    $('#example').dataTable({
        "ajax": {
            "url": "https://api.myjson.com/bins/vawiz",
            "dataSrc": ""
        },
        "columns": [{
            "data": "",
            "defaultContent": null,
            "render": function(data, type, full) {
                var x = [];
                if (full["container-title"]) {
                    x.push("Title: " + full["container-title"]);
                }
                if (full["volume"]) {
                    x.push("Volumns: " + full["volume"]);
                }
                if (full["issue"]) {
                    x.push("Issue: " + full["issue"]);
                }
                if (full["page"]) {
                    x.push("Page: " + full["page"]);
                }
                return $.map(x, function(d, i) {
                    return d;
                }).join(', ');
            }
        }, ],
    });
    

    Also, your script includes were in the wrong order. :-D

    0 讨论(0)
提交回复
热议问题