【问题标题】:jQuery Datatables align center 1 columnjQuery Datatables 对齐中心 1 列
【发布时间】:2018-01-08 20:34:09
【问题描述】:

只想将名为“状态”的第一列居中对齐:

        $("#TransactionTable").DataTable({
            ajax: {
                url: '/Transaction/SearchMockup',
                type: 'POST',
                data: {
                    cardEndingWith: $("#ccn").val(),
                    status: $("#status").val(),
                    supplier: $("#supplier").val(),
                    fromDate: $("#fromDate").val(),
                    toDate: $("#toDate").val()
                }
            },
            columns: [
            {
                data: 'Status', render: function (data, type, row) {
                    switch (data) {
                        case 1:
                            return '<div id="circleRed"></div>'
                            break;
                        case 2:
                            return '<div id="circleGreen"></div>'
                            break;
                        case 3:
                            return '<div id="circleOrange"></div>'
                            break;
                    }
                }
            },
            { data: 'TransactionId' },
            { data: 'CreditCardNumber' },
            { data: 'Supplier' },
            { data: 'CreatedAt' },
            { data: 'Amount' }
            ]
        });

我需要添加哪些列选项来实现它?我尝试阅读所有 Datatables 文档和 google。

【问题讨论】:

    标签: jquery html css datatables


    【解决方案1】:

    您可以在 columndef 中使用您自己的主题类(引导程序)或您自己的。 喜欢

    文本右,文本左,文本中心

     'columnDefs': [
        {
            "targets": 0, // your case first column
            "className": "text-center",
            "width": "4%"
       },
       {
            "targets": 2,
            "className": "text-right",
       }
     ]
    

    【讨论】:

      【解决方案2】:

      此外,您可以将列分组以将一种样式应用于许多列,如下所示:

        columnDefs: [
          { className: 'text-right', targets: [7, 10, 11, 14, 16] },
          { className: 'text-center', targets: [5] },
        ], ...
      

      【讨论】:

        【解决方案3】:

        您还可以在每个列对象中指定一个 css 类名。

        在你的情况下:

        {
            data: 'Status', 
            render: function (data, type, row) {
                switch (data) {
                    case 1:
                         return '<div id="circleRed"></div>';
                    case 2:
                         return '<div id="circleGreen"></div>';
                    case 3:
                         return '<div id="circleOrange"></div>';
                }
            },
            className: "text-center"
        }
        

        【讨论】:

          【解决方案4】:

          你可以手动设置样式

          $("select_your_table").DataTable({
              ....
              columns: [
                  {
                      mData: "select_property_from_json",
                      render: function (data) {
                          return '<span style="display: flex; flex-flow: row nowrap; justify-content: center;">data</span>';
                      }
                  },
              ],
              ....
          });
          

          【讨论】:

            猜你喜欢
            • 2017-02-14
            • 2015-04-18
            • 2012-10-14
            • 2013-08-19
            • 2012-01-31
            • 1970-01-01
            • 2013-11-27
            • 2017-08-25
            • 2010-12-18
            相关资源
            最近更新 更多