简体   繁体   English

jQuery DataTables编辑器:“选择”字段显示选项值而不是标签

[英]jquery DataTables Editor: “select” field displays option value instead of label

I am using jquery's DataTables which is really working great. 我正在使用jquery的DataTables,它确实很好用。 Then only problem I got is, that I am facing (in non-edit-view) the value of the select-field (which is an id). 然后,我得到的唯一问题是,我面对(在非编辑视图中)select字段的值(它是一个id)。 The user of course doesn't want to see the id of course. 用户当然不希望看到ID。 Therefore I am looking for a possibility to configure that column in a way to show always the value of label property. 因此,我正在寻找一种可能以始终显示label属性值的方式配置该列的可能性。

Here a some snippets: 这里有一些片段:

$(document).ready(function() {

        var table = $('#overviewTable').DataTable({
            dom: "Tfrtip",
            ajax: "/Conroller/GetTableData",
            columns: [
                { data: "Id", className: "readOnly", visible: false },
                {
                    data: "LoanTransactionId",
                    className: "readOnly readData clickable",
                    "fnCreatedCell": function(nTd, sData, oData, iRow, iCol) {
                        $(nTd).html("<a href='#'>" + oData.LoanTransactionId + "</a>");
                    }
                },
                { data: "Id", className: "readOnly" },
                { data: "property_1", className: "readOnly" },
                { data: "Priority" },
                { data: null, className: "action readOnly", defaultContent: '<a href="#">Info</a>' }
            ],
            order: [1, 'asc'],
            tableTools: {
                sRowSelect: "os",
                sRowSelector: 'td:first-child',
                aButtons: []
            }
        });

        // data reload every 30 seconds
        setInterval(function() {
            table.ajax.reload();
        }, 30000);

        editor = new $.fn.dataTable.Editor({
            ajax: "PostTable",
            table: "#overviewTable",
            fields: [
                {
                    label: "Id",
                    name: "Id"
                },
                {
                    label: "Column 1",
                    name: "property_1"
                }, 
                {
                    label: "Priority",
                    name: "Priority",
                    type: "select",
                    options: [
                        { label: "low", value: 0 },
                        { label: "mid", id: 1 },
                        { text: "high", id: 2 }
                    ]
                }
            ]
        });

        // Inline Edit - only those who are not readOnly
        $('#overviewTable').on('click', 'tbody td:not(:first-child .readOnly)', function(e) {
            editor.inline(this, {
                submitOnBlur: true
            });
        });

How it looks in the display mode 在显示模式下的外观

显示视图中的列

How it looks in the edit mode 在编辑模式下的外观

编辑视图中的单元格

See the documentation on columns.render 请参阅关于column.render的文档

You want to modify your column options for priority 您要修改列选项的priority

Preferred Option: Your data source has a field with the priority as a string 首选选项:您的数据源具有一个优先级为字符串的字段

This is the best option, as you don't want to have two places with this business logic. 这是最好的选择,因为您不想在此业务逻辑中占据两个位置。 Keep it out of the client code. 将其保留在客户端代码之外。

Also, you will want to modify the editor as well so that the options used have been retrieved dynamically from the server to keep this business logic out of the client too. 此外,您还将需要修改编辑器,以便已从服务器动态检索使用的选项,以使该业务逻辑也不受客户端的影响。 This is left as an exercise for the reader. 这留给读者练习。

Since you don't provide details on what your data structure looks lik, I'm assuming it is an object, and it has an attribute priorityAsString so use the string option type for render . 由于您没有提供有关数据结构看起来像什么的详细信息,因此我假设它是一个对象,并且具有属性priorityAsString因此请使用renderstring选项类型。

        columns: [
            ...
            { 
              data: "Priority" ,
              render: "priorityAsString",
            },

Option 2) You write a function to map priority to string 选项2)您编写了将优先级映射到字符串的函数

Do this if you can't get the data from the server. 如果无法从服务器获取数据,请执行此操作。 But remember you will need to update many places when the priority list changes. 但是请记住,当优先级列表更改时,您将需要更新许多位置。

        columns: [
            ...
            { 
              data: "Priority" ,
              render: renderPriorityAsString,
            },
        ...

function renderPriorityAsString(priority) {
  const priorityToString = {
     0: 'low',
     1: 'med',
     2: 'high',    
  };
  return priorityToString[priority] || `${priority} does not have a lookup value`;
}
"render": function ( data, type, full ) { return label;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM