数据表中数组变量的精细操作 [英] Granular manipulation of array variables in Datatables

查看:20
本文介绍了数据表中数组变量的精细操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在这个 fiddle 中看到我正在寻找一种解决方案,让我在 same 数据表列中加入了几个 JSON 字段.这是我找到的解决方案:

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 found:

 function ( data, type, full ) {
      var a = (full["container-title"]);
      var b = (full["volume"]);
      var c = (full["issue"]);
      var d = (full["page"]);
      var x = ([a, b, c, d]);
      return $.map( x, function ( d, i ) {
      return d;}).join( ', ' );
    }

这会输出我想要的值,用逗号分隔.但是,我想在输出之前向 each 变量添加一些 html.举例来说,我希望在音量"之前加上音量".但是如果我试试这个

This outputs the values I want, separated by commas. However, I would like to add some html to each variable before the output. Say, for instance, I want "volume" to be preceded by "Vol.". But if I try this

var a = 'Vol.'+ (full["volume"]);

该值作为未定义"传递并且完全无法使用.还有其他路线吗?

the value is passed as "undefined" and completely unusable. Any other route?

推荐答案

您的方法确实有效,请检查此 JSFiddle (https://jsfiddle.net/annoyingmouse/0zdjy1yz/6/).您遇到的是无法找到 issue 的相关数据,因为它不在您的数据中,如果您在将其添加到您正在笑的数组之前进行检查:

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(', ');
        }
    }, ],
});

此外,您的脚本包含的顺序错误.:-D

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

这篇关于数据表中数组变量的精细操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆