目前我正在尝试将SlickGrid集成到聚合物元素中,并且在风格方面存在一些问题。
当我将它嵌入聚合物元素时,具有非常原始的css样式的原始SlickGrid示例看起来不同。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SlickGrid example 1: Basic grid</title>
<style>
.slick-cell, .slick-headerrow-column {
position: absolute;
border: 1px solid transparent;
border-right: 1px dotted silver;
border-bottom-color: silver;
overflow: hidden;
-o-text-overflow: ellipsis;
text-overflow: ellipsis;
vertical-align: middle;
z-index: 1;
padding: 1px 2px 2px 1px;
margin: 0;
white-space: nowrap;
cursor: default;
}
</style>
</head>
<body>
<table width="100%">
<tr>
<td valign="top" width="50%">
<div id="myGrid" style="width:600px;height:500px;"></div>
</td>
<td valign="top">
<h2>Demonstrates:</h2>
<ul>
<li>basic grid with minimal configuration</li>
</ul>
</td>
</tr>
</table>
<script src="../lib/jquery-1.7.min.js"></script>
<script src="../lib/jquery.event.drag-2.2.js"></script>
<script src="../slick.core.js"></script>
<script src="../slick.grid.js"></script>
<script>
var grid;
var columns = [
{id: "title", name: "Title", field: "title"},
{id: "duration", name: "Duration", field: "duration"},
{id: "%", name: "% Complete", field: "percentComplete"},
{id: "start", name: "Start", field: "start"},
{id: "finish", name: "Finish", field: "finish"},
{id: "effort-driven", name: "Effort Driven", field: "effortDriven"}
];
var options = {
enableCellNavigation: true,
enableColumnReorder: false
};
$(function () {
var data = [];
for (var i = 0; i < 500; i++) {
data[i] = {
title: "Task " + i,
duration: "5 days",
percentComplete: Math.round(Math.random() * 100),
start: "01/01/2009",
finish: "01/05/2009",
effortDriven: (i % 5 == 0)
};
}
grid = new Slick.Grid("#myGrid", data, columns, options);
})
</script>
</body>
</html>
截图
<link rel="import" href="bower_components/polymer/polymer.html">
<polymer-element name="polymer-slickgrid">
<template>
<style>
.slick-cell, .slick-headerrow-column {
position: absolute;
border: 1px solid transparent;
border-right: 1px dotted silver;
border-bottom-color: silver;
overflow: hidden;
-o-text-overflow: ellipsis;
text-overflow: ellipsis;
vertical-align: middle;
z-index: 1;
padding: 1px 2px 2px 1px;
margin: 0;
white-space: nowrap;
cursor: default;
}
</style>
<div id="slickGrid" style="width:600px;height:500px;"></div>
</template>
<script src="bower_components/slickgrid/lib/jquery-1.7.min.js"></script>
<script src="bower_components/slickgrid/lib/jquery.event.drag-2.2.js"></script>
<script src="bower_components/slickgrid/slick.core.js"></script>
<script src="bower_components/slickgrid/slick.grid.js"></script>
<script>
Polymer('polymer-slickgrid', {
domReady: function(){
var grid;
var columns = [
{id: "title", name: "Title", field: "title"},
{id: "duration", name: "Duration", field: "duration"},
{id: "%", name: "% Complete", field: "percentComplete"},
{id: "start", name: "Start", field: "start"},
{id: "finish", name: "Finish", field: "finish"},
{id: "effort-driven", name: "Effort Driven", field: "effortDriven"}
];
var options = {
enableCellNavigation: true,
enableColumnReorder: false
};
var self = this;
$(function () {
var data = [];
for (var i = 0; i < 500; i++) {
data[i] = {
title: "Task " + i,
duration: "5 days",
percentComplete: Math.round(Math.random() * 100),
start: "01/01/2009",
finish: "01/05/2009",
effortDriven: (i % 5 == 0)
};
}
var element = self.$.slickGrid;
var grid = new Slick.Grid(element, data, columns, options);
})
}
});
</script>
</polymer-element>
元素的用法
<html>
<head lang="en">
<script src="bower_components/platform/platform.js"></script>
<link rel="import" href="polymer-slickgrid.html">
</head>
<body>
<polymer-slickgrid></polymer-slickgrid>
</body>
</html>
截图
似乎应用了单元格样式,但在聚合物元素中使用时,行具有不同的样式信息。
没有聚合物的CSS屏幕截图
Polymer元素内的SlickGrid屏幕截图
答案 0 :(得分:1)
看起来您的<polymer-slickgrid>
元素是内联显示的。见这里:http://www.polymer-project.org/articles/styling-elements.html#element-defined-styles
简单的解决方案:
<style>
:host {
/* Note: by default elements are always display:inline. */
display: block;
}
</style>
..或者你可以添加一个&#39;块&#39;属性,如:<polymer-slickgrid block>
[更新]
看起来插件正在为文档<head>
添加内联样式,这意味着这些样式不会应用于您的元素。
作为一个hacky hack和概念证明,你可以获得这些样式并将它们内嵌到你的元素中:
var styles = [];
var styleSheets = window.document.styleSheets;
$.each(styleSheets, function(i, sheet) {
var sheet = styleSheets[i];
if (sheet.cssRules) {
$.each($.makeArray(sheet.cssRules), function(i, rule) {
if (rule.cssText && /slickgrid/.test(rule.cssText)) {
styles.push(rule.cssText);
}
})
}
});
var inlineStyles = '';
$.each(styles, function(i, style) {
inlineStyles += (style + "\n");
});
inlineStyles = '<style type="text/css">' + inlineStyles + '</style>';
$(this.$.slickGrid).before(inlineStyles);
我还没有在很多浏览器中测试过这段代码,请不要使用它,它只是一个POC。我在var grid = new Slick.Grid(element, data, columns, options);
这里的网格看起来与上面的hack一样,显示在自定义元素中(我还必须将jquery ui和slick.grid样式表添加到自定义元素中):