在表格中设置 jQuery UI 自动完成结果的格式

发布于 2024-08-31 02:16:54 字数 909 浏览 4 评论 0原文

我现在使用 http://jqueryui.com/demos/autocomplete/ 基于之前的一些推荐,它对我来说效果很好。

我希望将自动完成列表中的结果格式化为表格。现在我的结果是这样的:

Last name, first name - id number - status code

当有一个名字列表时,没有什么可以很好地排列,

Name - ID - Status
Brown, Charlie - 2 - A
Jones, Henry - 3 - I

我想以某种方式得到

Name             ID  Status
Brown, Charlie   2   A
Jones, Henry     3   I

这可能吗?我目前使用的是 json 数据源,其中有“标签”、“值”和“id”数组,即:

{"label":"Brown, Charlie - 2 - A","value":"Brown, Charlie","id":"2"}
{"label":"<span style="color: red;">Jones, Henry - 3 - I</span>","value":"Brown, Charlie","id":"2"}

标签在下拉列表中可见,值是返回到原始文本框的内容, id 是返回隐藏字段的内容。

我尝试将一些 div 混合到数组的标签部分,但没有成功。 非活动用户标签中的跨度对我来说效果很好,但我无法将跨度的宽度设置为固定宽度。

有什么我可以做的吗?

I'm now using http://jqueryui.com/demos/autocomplete/ based on some previous recommendations and it's working well for me.

I am looking to format the results in the auto-complete list in a table. Right now my results are something like:

Last name, first name - id number - status code

When there is a list of names nothing lines up nicely

Name - ID - Status
Brown, Charlie - 2 - A
Jones, Henry - 3 - I

I'd like to somehow get

Name             ID  Status
Brown, Charlie   2   A
Jones, Henry     3   I

Is this possible? I'm currently using a json datasource where I have a 'label', 'value' and 'id' array that is:

{"label":"Brown, Charlie - 2 - A","value":"Brown, Charlie","id":"2"}
{"label":"<span style="color: red;">Jones, Henry - 3 - I</span>","value":"Brown, Charlie","id":"2"}

The label is visible in the drop-down, the value is what is returned to the original text box and the id is what is return to a hidden field.

I've tried mixing in some divs to the label part of the array with no luck.
The span in the label for inactive users works fine for me but I can't set the width of a span to a fixed width.

Is there anything I can do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红焚 2024-09-07 02:16:54

您是否考虑过使用固定字体(也许是 courier),然后填充您的姓名字段?

这应该很好地排列起来。
就像这样:

$maxWidth = 30;
$nameWidth = strlen(lastname) + strlen(firstname) + 2  //don't forget the comma and space
$padding = $maxWidth - $nameWidth;

然后只需用计算出的金额填充姓氏、名字,事情就应该对齐。

Have you thought at all about using a fixed with font (courier, maybe) and then padding your name field?

That should line things up nicely.
Something like:

$maxWidth = 30;
$nameWidth = strlen(lastname) + strlen(firstname) + 2  //don't forget the comma and space
$padding = $maxWidth - $nameWidth;

Then just pad the last name, first name with the calculated amount and things should line up.

〃温暖了心ぐ 2024-09-07 02:16:54

我认为这可以帮助你,这里是js:

$(function() {
//overriding jquery-ui.autocomplete .js functions
$.ui.autocomplete.prototype._renderMenu = function(ul, items) {
  var self = this;
  //table definitions
  ul.append("<table><thead><tr><th>ID#</th><th>Name</th><th>Cool Points</th></tr></thead><tbody></tbody></table>");
  $.each( items, function( index, item ) {
    self._renderItemData(ul, ul.find("table tbody"), item );
  });
};
$.ui.autocomplete.prototype._renderItemData = function(ul,table, item) {
  return this._renderItem( table, item ).data( "ui-autocomplete-item", item );
};
$.ui.autocomplete.prototype._renderItem = function(table, item) {
  return $( "<tr class='ui-menu-item' role='presentation'></tr>" )
    .data( "item.autocomplete", item )
    .append( "<td >"+item.id+"</td>"+"<td>"+item.value+"</td>"+"<td>"+item.cp+"</td>" )
    .appendTo( table );
};
//random json values
var projects = [
    {id:1,value:"Thomas",cp:134},
    {id:65,value:"Richard",cp:1743},
    {id:235,value:"Harold",cp:7342},
    {id:78,value:"Santa Maria",cp:787},
    {id:75,value:"Gunner",cp:788},
    {id:124,value:"Shad",cp:124},
    {id:1233,value:"Aziz",cp:3544},
    {id:244,value:"Buet",cp:7847}
];
$( "#project" ).autocomplete({
    minLength: 1,
    source: projects,
    //you can write for select too
    focus: function( event, ui ) {
        //console.log(ui.item.value);
        $( "#project" ).val( ui.item.value );
        $( "#project-id" ).val( ui.item.id );
        $( "#project-description" ).html( ui.item.cp );
        return false;
    }
})
});

你可以看看这个 fiddle

这也可能有帮助小提琴

I think this can help you,here is the js:

$(function() {
//overriding jquery-ui.autocomplete .js functions
$.ui.autocomplete.prototype._renderMenu = function(ul, items) {
  var self = this;
  //table definitions
  ul.append("<table><thead><tr><th>ID#</th><th>Name</th><th>Cool Points</th></tr></thead><tbody></tbody></table>");
  $.each( items, function( index, item ) {
    self._renderItemData(ul, ul.find("table tbody"), item );
  });
};
$.ui.autocomplete.prototype._renderItemData = function(ul,table, item) {
  return this._renderItem( table, item ).data( "ui-autocomplete-item", item );
};
$.ui.autocomplete.prototype._renderItem = function(table, item) {
  return $( "<tr class='ui-menu-item' role='presentation'></tr>" )
    .data( "item.autocomplete", item )
    .append( "<td >"+item.id+"</td>"+"<td>"+item.value+"</td>"+"<td>"+item.cp+"</td>" )
    .appendTo( table );
};
//random json values
var projects = [
    {id:1,value:"Thomas",cp:134},
    {id:65,value:"Richard",cp:1743},
    {id:235,value:"Harold",cp:7342},
    {id:78,value:"Santa Maria",cp:787},
    {id:75,value:"Gunner",cp:788},
    {id:124,value:"Shad",cp:124},
    {id:1233,value:"Aziz",cp:3544},
    {id:244,value:"Buet",cp:7847}
];
$( "#project" ).autocomplete({
    minLength: 1,
    source: projects,
    //you can write for select too
    focus: function( event, ui ) {
        //console.log(ui.item.value);
        $( "#project" ).val( ui.item.value );
        $( "#project-id" ).val( ui.item.id );
        $( "#project-description" ).html( ui.item.cp );
        return false;
    }
})
});

You can check out this fiddle

this might help too fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文