简体   繁体   English

KendoUI Grid-可以动态更改字体大小以适合列宽吗?

[英]KendoUI Grid - can dynamically change font size to fit column width?

I see there are some solutions out there for dynamically setting font size to make text fit within a given size span. 我看到有一些解决方案可以动态设置字体大小,以使文本适合给定的大小范围。 Any way to automatically do this in Kendo Grid? 有什么方法可以在Kendo Grid中自动执行此操作吗? My columns seem to keep proportional sizes as the table shrinks in size (as desired), but as it gets smaller, it either wraps data making the rows taller, or it puts ellipses at the end. 当表的大小缩小时(根据需要),我的列似乎保持成比例的大小,但是随着表的变小,它要么包装数据使行变高,要么将椭圆放在末尾。 If it shrinks a column in size enough, there is just ellipses, no clue what the data really is. 如果将列的大小缩小到足够大,则只会出现椭圆,而无法知道数据的真正含义。 I'm sure if the font were made smaller, it would be more useful. 我确定如果将字体缩小,会更有用。 But I need the font larger when the grid is bigger. 但是当网格更大时,我需要更大的字体。

I suppose I could have a function that, based on the width of the whole table (or its parent) I could change the font size of all the columns (table-cell's) in the table. 我想我可以有一个功能,可以基于整个表(或其父表)的宽度来更改表中所有列(表单元格)的字体大小。 That might be acceptable. 那可能是可以接受的。 But not ideal. 但是并不理想。 I'd rather have a field with more text get a smaller font, and a field that still fits because it is very short (in some cases just one letter), there would be no real reason to shrink the font in that column. 我宁愿有一个文本较多的字段使用较小的字体,而一个仍适合的字段,因为它很短(在某些情况下只有一个字母),因此没有真正的理由在该列中缩小字体。

Perhaps someone has tried using one of the jQuery plugins to do this, I tried the textfill plugin but did not have luck with it yet. 也许有人尝试使用jQuery插件之一来执行此操作,但我尝试使用textfill插件,但还没有运气。

If nobody has done this before, if/when I find a solution I will post the answer here myself. 如果以前没有人做过,如果/当我找到解决方案时,我会在这里自己张贴答案。

What I have done in the past is have a setting for my grids that would set the 'responsive' columns. 我过去所做的是为网格设置一个设置,该设置将设置“响应”列。 Upon resize, the columns that were set would be hidden depending on the size of the grid. 调整大小后,根据网格的大小,设置的列将被隐藏。 This would be done in javascript and the columns can be hidden like so: 这将在javascript中完成,并且列可以这样隐藏:

$(window).on("resize", function () {
        //add your code here
    });

You can hide/show the columns using kendo: 您可以使用kendo隐藏/显示列:

$("#grid_name").data("kendoGrid").showColumn("ColumnName");
$("#grid_name").data("kendoGrid").hideColumn("ColumnName");

You can have a setting for your min/max window size for each column and hide/show accordingly. 您可以为每列设置最小/最大窗口大小,并相应地隐藏/显示。 Good luck. 祝好运。

The best place to handle this manually would be the dataBound event. 手动处理此问题的最佳方法是dataBound事件。 This event will be called every time the data in the grid will be updated so from there you'll be able to access the grid's column, dataItem and their corresponding html elements. 每次更新网格中的数据时都会调用此事件,因此您可以从那里访问网格的列,dataItem及其对应的html元素。 With those information, you'll be able to implement your own logic and set the CSS style for each individual element: 有了这些信息,您将能够实现自己的逻辑并为每个单独的元素设置CSS样式:

$("#YourGrid").kendoGrid({
    dataBound: function(){

        //You can use the TR element
        $.each($("#YourGrid").data("kendoGrid").dataSource.view(), function (index, viewDataItem) {

            var jqTr = $("#YourGrid").find("tbody>tr[data-uid='" + viewDataItem.uid + "']");

            if ($("#YourGrid").width() > 1000) {
                jqTr.css("font-size", "10px");
            } else {
                jqTr.css("font-size", "12px");
            }

        });

        //Or a specific TD element if you need to set a font-size per column (or for a specific cell)
        $.each($("#YourGrid").data("kendoGrid").columns, function (index, columnScan) {

            var jqTd;
            if (columnScan.locked) {
                lockCount++;
                jqTd = _this.kendoGrid().lockedContent.find("tbody>tr>td:nth-child(" + (index + 1) + ")");
            } else {
                jqTd = _this.kendoGrid().tbody.find(">tr>td:nth-child(" + (index + 1 - lockCount) + ")");
            }

            if (columnScan.width > 100) {
                jqTd.css("font-size", "10px");
            } else {
                jqTd.css("font-size", "12px");
            }

        }
    }
});

Note that the code above was made in a virtual scrolling with lockable column context but I the logic would still work for a regular grid. 请注意,以上代码是在具有可锁定列上下文的虚拟滚动中完成的,但我的逻辑仍然适用于常规网格。

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

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